查询与过滤器 - 执行顺序 [英] Queries vs Filters - Order of execution

查看:123
本文介绍了查询与过滤器 - 执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这个问题,我的一位同事让我怀疑:



在过滤的查询中,过滤器应用于何时?在执行查询之前还是之后?结果缓存何时?



如果预先应用过滤器,在过滤器中复制查询部分不是一件好事吗?
如果之后应用了过滤器,那么我无法理解被缓存的内容。

解决方案

幸运的是,ES提供两种类型的过滤器供您使用:

  {
查询:{
字段:{title:Catch-22}
},
过滤器:{
term:{year:1961}
}



{
查询:{
已过滤:{
查询:{
:{title:Catch-22}
},
过滤器:{
term:{year:1961}
}
}
}
}

在第一种情况下,应用过滤器查询查找到的所有文档。在第二种情况下,文档在查询运行之前被过滤。这样会产生更好的表现。



引用自: http://www.packtpub.com/elasticsearch-server-for-fast-scalable-flexible-search-solution/book



关于缓存,我不知道过滤器的缓存机制。
我的猜测是:
第一种情况是,由于过滤器与查询返回的一组结果相反,因此该缓存对于此返回集是特定的。
第二种情况,首先应用过滤器,为您检查的索引存储缓存,因此,该缓存更可重用,因为它不依赖于查询的内容,而是更大的内存成本和查询第一次(在生成缓存之前)的时间。


I've read this question and a colleague of mine made me doubt:

In a filtered query, when is the filter applied ? Before or after executing the query ? When is the result cached ?

If the filter is applied beforehand, wouldn't it be a a good thing to duplicate the query part in the filters ? If the filter is applied afterward, then i'm having trouble understanding what is cached.

解决方案

Luckily, ES provides two types of filters for you to work with:

{
  "query" : {
    "field" : { "title" : "Catch-22" }
  },
  "filter" : {
    "term" : { "year" : 1961 }
  }
}


{
  "query": {
    "filtered" : {
      "query" : {
        "field" : { "title" : "Catch-22" }
      },
      "filter" : {
        "term" : { "year" : 1961 }
      }
    }
  }
}

In the first case, filters are applied to all documents found by the query. In the second case, the documents are filtered before the query runs. This yields better performance.

Quoted from: http://www.packtpub.com/elasticsearch-server-for-fast-scalable-flexible-search-solution/book

About cache, I'm not sure about cache mechanism of filters. My guessing would be: First case, since the filter is against a set of results returned by query, the cache is kind of specific for this return set. Second case, the filter is applied first, the cache is stored for the indices you checked against, thus, this cache is more reusable because it does not rely on the content of the query, but at larger memory cost and query time for first time(before the cache is generated).

这篇关于查询与过滤器 - 执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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