5

I am little bit confused with ElasticSearch Query DSL's query context and filter context. I have 2 below queries. Both queries return same result, first one evaluate score and second one does not. Which one is more appropriate ?

1st Query :-

 curl -XGET 'localhost:9200/xxx/yyy/_search?pretty' -d'
 {
   "query": {
     "bool": {
       "must": {
         "terms": { "mcc" : ["5045","5499"]}
       },
       "must_not":{
         "term":{"maximum_flag":false}
       },                          
       "filter": {
         "geo_distance": {
           "distance": "500",
           "location": "40.959334, 29.082142"
         }                                   
       }
     }
   }
 }'

2nd Query :-

 curl -XGET 'localhost:9200/xxx/yyy/_search?pretty' -d'
 {
   "query": {
     "bool" : {
       "filter": [
         {"term":{"maximum_flag":true}},
         {"terms": { "mcc" : ["5045","5499"]}}
       ],
       "filter": {
         "geo_distance": {
            "distance": "500",
            "location": "40.959334, 29.082142"
         }                                   
       }
     }
   }
 }'

Thanks,

Antonio Val
  • 3,200
  • 1
  • 14
  • 27
Shivam
  • 73
  • 1
  • 5

2 Answers2

4

In the official guide you have a good explanation:

Query context

A query clause used in query context answers the question “How well does this document match this query clause?” Besides deciding whether or not the document matches, the query clause also calculates a _score representing how well the document matches, relative to other documents.

Query context is in effect whenever a query clause is passed to a query parameter, such as the query parameter in the search API.

Filter context

In filter context, a query clause answers the question “Does this document match this query clause?” The answer is a simple Yes or No — no scores are calculated. Filter context is mostly used for filtering structured data, e.g.

Does this timestamp fall into the range 2015 to 2016? Is the status field set to "published"? Frequently used filters will be cached automatically by Elasticsearch, to speed up performance.

Filter context is in effect whenever a query clause is passed to a filter parameter, such as the filter or must_not parameters in the bool query, the filter parameter in the constant_score query, or the filter aggregation.

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-filter-context.html

About your case, we would need more information, but taking into account you are looking for exact values, a filter would suit it better.

Antonio Val
  • 3,200
  • 1
  • 14
  • 27
0

The first query is evaluating score because your are using "term" here directly inside without wrapping it inside "filter" so by default "term" written directly inside query run in Query context format which result in calculating score. But in the case of second query you "term" inside "filter" which change it's context from Query Context to filter Context . And in the case of filter no score is calculated (by default _score 1 is allocated to all matching documents). You can find more details about queries behavior in this article https://towardsdatascience.com/deep-dive-into-querying-elasticsearch-filter-vs-query-full-text-search-b861b06bd4c0