弹性搜索筛选查询与筛选器 [英] Elasticsearch Filtered query vs Filter

查看:98
本文介绍了弹性搜索筛选查询与筛选器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过滤器中的查询和过滤器以及根目录的查询和过滤器是否有区别?例如



案例1:

  {
query:{
filtered:{
query:{
term:{title:kitchen3}
},
过滤器:{
term:{price:1000}
}
}
}
}
案例2:

  {
查询:{
term:{title:kitchen3}
},
filter {
term:{price:1000}
}
}

我发现这个讨论 http:/ /elasticsearch-users.115913.n3.nabble.com/Filtered-query-vs-using-filter-outside-td3960119.html ,但引用的网址为404,对我来说解释有点太简洁了。 / p>

请教或给任何文件,指出之间的区别ese,谢谢。

解决方案

差异与性能有关。顶层的过滤器总是在查询后执行。这意味着对所有文档执行查询,对所有文档等进行分数计算,仅排除不匹配过滤器的文档。



使用过滤查询是ES的优化计算的可能性,例如首先执行过滤器,然后在有限的文档集上执行查询,节省测试与过滤器不符合查询的文档的时间,以及如果它们匹配查询,则计算它们的分数。



如果您使用相同的过滤器执行多个查询,那么还有更多优点:过滤器可能会被缓存,甚至进一步提高每个查询的性能。这适用于您的示例:默认情况下,term过滤器被缓存。



您还可以显式控制过滤查询的执行(请参阅文档)以优化它对于您的特定用例。


Is there any difference between "query and filter in filtered" and "query and filter on the root"? for example

Case 1:

{
  "query":{
    "filtered":{
      "query":{
        "term":{"title":"kitchen3"}
      },
      "filter":{
        "term":{"price":1000}
      }
    }
  }
}

Case 2:

{
  "query":{
    "term":{"title":"kitchen3"}
  },
  "filter":{
    "term":{"price":1000}
  }
}

I found this discussion http://elasticsearch-users.115913.n3.nabble.com/Filtered-query-vs-using-filter-outside-td3960119.html, but referenced URL is 404 and the explanation is a bit too concise for me.

Please teach or give any document which is pointing the difference between these, thank you.

解决方案

The difference is related to performance. "filter" on top level is always executed after the query. This means the query is executed on all documents, score is computed for all documents etc. - and only then documents not matching filter are excluded.

With "filtered" query there is a possibility that ES will optimize this computation, e.g. first executing the filter, then executing query on a limited set of documents, saving time on testing the documents that don't match the filter against the query, and on computing scores for them if they do match the query.

If you are performing multiple queries with same filter, then there are even more advantages: the filter may be cached, improving performance of each query even further. This applies to your example: "term" filters are cached by default.

You also can explicitly control the execution of "filtered" query (see the documentation) to optimize it for your particular use case.

这篇关于弹性搜索筛选查询与筛选器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆