====== ElasticSearch Query ======
===== From/Limit =====
* 쿼리 결과 ''from/limit'' 지정
GET /_search
{
"from" : 0, "size" : 10,
"query" : {
"term" : { "user" : "kimchy" }
}
}
// 혹은
GET /_search?from=0&size=10
===== null 체크 =====
* [[https://www.elastic.co/guide/en/elasticsearch/guide/current/_dealing_with_null_values.html|Dealing with Null Values | Elasticsearch: The Definitive Guide [2.x] | Elastic]]
* 값의 존재(''IS NOT NULL'') 체크
GET /my_index/posts/_search
{
"query" : {
"constant_score" : {
"filter" : {
"exists" : { "field" : "필드이름" }
}
}
}
}
* 값이 존재하지 않음(''IS NULL'') 체크(
GET /my_index/posts/_search
{
"query" : {
"constant_score" : {
"filter": {
"missing" : { "field" : "필드이름" }
}
}
}
}
===== 범위 (Range) =====
* [[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html|Range Query]]
GET _search
{
"query": {
"range" : {
"age" : {
"gte" : 10, // "gt"
"lte" : 20, // "lt"
"boost" : 2.0
}
}
}
}
* ''gte'' : Greater-than or equal to
* ''gt'' : Greater-than
* ''lte'' : Less-than or equal to
* ''lt'' : Less-than
* ''boost'' : Sets the boost value of the query, defaults to 1.0
===== Distinct Field =====
* [[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html|Terms Aggregation]]
* ''product'' 필드값으로 distinct 하고 결과를 5개까지 출력, 이때 검색결과는 출력하지 않고 통계 결과만출력
{
"size": 0, // 검색결과 출력 안하게
"aggs" : {
"products" : {
"terms" : {
"field" : "product",
"size" : 5
}
}
}
}
* 정렬하기
{
"aggs" : {
"genres" : {
"terms" : {
"field" : "genre",
"order" : { "_count" : "asc" } // asc/desc
}
}
}
}
* ''_term'' : 키로 정렬
* ''_count'' : 키의 존재 갯수로 정렬
* 필터와 함께 적용[[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html|Filter Aggregation]]
{
"size": 0,
"aggs" : {
"t_shirts" : {
"filter" : { "term": { "type": "t-shirt" } },
"aggs" : {
"avg_price" : { "avg" : { "field" : "price" } }
}
}
}
}