ElasticSearch-如何在查询结果上应用过滤器以限制具有特定值的文档 [英] ElasticSearch - How can i apply a filter over the results of the query to limit the document that have a certain value
问题描述
我对弹性搜索有疑问,但是我不确定从哪里开始搜索或应该使用google搜索哪种精确操作.
I have a question regarding elastic search but I am not sure where to start searching or which precise operation I should search for using google.
假设我有一个包含数据的文档,并且其中一个字段是" the_best
",(这是布尔值).事情是(当前),超过48个结果(通过有效的查询给出),我希望将15个文档的 the_best
字段设置为true.
Let say I have a document with data and one of its fields is "the_best
" (which is a boolean). The thing is (currently), over 48 results (given by a working query), I have like 15 documents returned with the_best
field set to true.
现在,我希望将结果限制为设置为 true
的2个最大文档.所以现在,它(elasticsearch)现在应该返回35个结果(如果我们盯着上面的故事):
Now, I would like to limit this by only 2 maximum documents set to true
over the results. So now, it (elasticsearch) should now return 35 results (if we stik at the story above):
- 基数(在48个结果中):[15 the_best = true,33 the_best = false]
- 预期(最多2个
the_best = true
):我应该获得35个结果[ 2 the_best = true ,33 the_best = false])
- Base (out of 48results): [15 the_best=true, 33 the_best=false]
- Expected (with max 2
the_best=true
): I should get 35 results [2 the_best=true, 33 the_best=false])
有什么主意吗?:)
推荐答案
一种方法是使用使用m_Search可以组合多个查询
using m_Search you can combine multiple queries
GET <index>/_msearch
{}
{"query":{"term":{"the_best":true}},"from":0,"size":2}
{}
{"query":{"term":{"the_best":false}},"from":0,"size":15}
如果要在单个搜索中进行操作,则可以使用聚合(性能会降低)
If you want to do it in single search aggregation can be used(will be less performant)