Query DSL (Domain Specific Language)
Nosql 에 적재된 document 들을 RDBMS 에서 우리가 자주 썻던것처럼 Query 질의를 해야하는데 필요한 기능을 제공하주는 역활을 합니다.
Query DSL 진행하기전
Query Context 와 Filter Context 차이점에 대해서 아래 링크를 확인하시면됩니다.
번역기 돌리면 대충 이해가능합니다.
Query context
"쿼리절이 문서와 일치하는가?" 라는 질문에 답하며 _score 를 통해 문서가 다른문서와 비교하여 Query와 얼마나 일치하는지를 계산합니다.
Filter context
"쿼리가 문서와 일치하는가?" 라는 질문에 답하지만 _score는 계산하지 않습니다. 대부분 필터링을 위해 사용합니다.
https://www.elastic.co/guide/en/elasticsearch/reference/6.7/query-filter-context.html
Query and filter context | Elasticsearch Reference [6.7] | Elastic
Use query clauses in query context for conditions which should affect the score of matching documents (i.e. how well does the document match), and use all other query clauses in filter context.
www.elastic.co
curl "localhost:9200/sales-records/_search?pretty" -H "Content-Type:application/json" -d '
{
"query": {
"bool": {
"must": [
{"match": {"message": "africa"}}
],
"filter": [
{"term": {"item_type": "clothes"}}
]}
},
"size": 2
}'
“query”는 query context에서 동작합니다. “bool” 과 “must”는 query context에서 사용됩니다. 얼마나 일치하는 지를 평가하는데 사용됩니다.
“filter는 filter context에서 동작하며 “term”은 filter context에서 사용됩니다. 일치하는 문서는 걸러내지만 점수에는 영향을 주지 않습니다.
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : 1.2925828,
"hits" : [
{
"_index" : "sales-records",
"_type" : "_doc",
"_id" : "Dn9aY2oBml718jk9tcNd",
"_score" : 1.2925828,
"_source" : {
"total_profit" : 37674.72,
"sales_channel" : "Online",
"order_id" : 653758466,
"units_sold" : 513,
"tags" : [
"_dateparsefailure"
],
"total_revenue" : 56060.64,
"unit_cost" : 35.84,
"country" : "South Africa",
"order_priority" : "M",
"ship_date" : "3/16/2013",
"unit_price" : 109.28,
"total_cost" : 18385.92,
"item_type" : "Clothes",
"region" : "Sub-Saharan Africa",
"@version" : "1",
"host" : "c5ceb9b17956",
"order_date" : "2/21/2013",
"path" : "/config-dir/1500000 Sales Records.csv",
"message" : "Sub-Saharan Africa,South Africa,Clothes,Online,M,2/21/2013,653758466,3/16/2013,513,109.28,35.84,56060.64,18385.92,37674.72\r",
"@timestamp" : "2019-04-28T09:50:49.700Z"
}
},
{
"_index" : "sales-records",
"_type" : "_doc",
"_id" : "nn9aY2oBml718jk9tcOa",
"_score" : 1.2925828,
"_source" : {
"total_profit" : 28054.08,
"sales_channel" : "Online",
"order_id" : 669640576,
"units_sold" : 382,
"tags" : [
"_dateparsefailure"
],
"total_revenue" : 41744.96,
"unit_cost" : 35.84,
"country" : "South Africa",
"order_priority" : "C",
"ship_date" : "11/21/2015",
"unit_price" : 109.28,
"total_cost" : 13690.88,
"item_type" : "Clothes",
"region" : "Sub-Saharan Africa",
"@version" : "1",
"host" : "c5ceb9b17956",
"order_date" : "11/17/2015",
"path" : "/config-dir/1500000 Sales Records.csv",
"message" : "Sub-Saharan Africa,South Africa,Clothes,Online,C,11/17/2015,669640576,11/21/2015,382,109.28,35.84,41744.96,13690.88,28054.08\r",
"@timestamp" : "2019-04-28T09:50:49.789Z"
}
}
]
}
}
정확한 검색이 아닌 유사검색(문자 검색)과 같은 것은 query context를 사용하고 정확한 검색을 원할때는 filter context를 사용하면 될 것 같습니다.
'ELK > ElasticSearch' 카테고리의 다른 글
엘라스틱서치(Elasticsearch),Logstash,Kibana 6.5.4 버전 Redis Server Client 설치 입문 시작하기 1편 (2) | 2020.06.29 |
---|---|
ElasticSearch SendMail 발송 batch 용도 Scroll API 구현하기 (0) | 2020.05.13 |
ElasticSearch _source / doc_value / stored_fields 3가지 차이점 (0) | 2020.05.12 |
ElasticSearch Aggregations aggs composite afterkey Spring 집계 활용방법 (4) | 2020.05.12 |
ElasticSearch kibana DevTool Springboot Java 사용방법 (0) | 2020.04.29 |