简体   繁体   English

Elasticsearch查询上下文与过滤器上下文

[英]Elasticsearch query context vs filter context

I am little bit confused with ElasticSearch Query DSL's query context and filter context. 我对ElasticSearch Query DSL的查询上下文和过滤器上下文有点困惑。 I have 2 below queries. 我有2个以下查询。 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, 谢谢,

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. 查询上下文中使用的查询子句回答了问题“此文档与此查询子句的匹配程度如何?”除了确定文档是否匹配之外,查询子句还计算一个_score,表示文档相对于其他文档的匹配程度。

Query context is in effect whenever a query clause is passed to a query parameter, such as the query parameter in the search API. 只要将查询子句传递给查询参数(例如搜索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, eg 过滤器上下文主要用于过滤结构化数据,例如

Does this timestamp fall into the range 2015 to 2016? 这个时间戳是否属于2015年至2016年的范围? Is the status field set to "published"? 状态字段是否设置为“已发布”? Frequently used filters will be cached automatically by Elasticsearch, to speed up performance. Elasticsearch会自动缓存经常使用的过滤器,以加快性能。

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. 只要将查询子句传递给过滤器参数(例如bool查询中的过滤器或must_not参数,constant_score查询中的过滤器参数或过滤器聚合),过滤器上下文就会生效。

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-filter-context.html 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. 关于您的情况,我们需要更多信息,但考虑到您正在寻找准确的值,过滤器会更好地适应它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM