ElasticSearch-如何对查询结果应用过滤器来限制具有特定值的文档



我有一个关于弹性搜索的问题,但我不确定从哪里开始搜索,也不确定应该使用谷歌搜索哪个精确的操作。

假设我有一个包含数据的文档,其中一个字段是";CCD_ 1";(这是一个布尔值(。问题是(目前(,超过48个结果(由一个工作查询给出(,我有大约15个文档返回,the_best字段设置为true。

现在,我想限制这一点,在结果上最多只将2个文档设置为true。所以现在,它(弹性搜索(现在应该返回35个结果(如果我们继续上面的故事(:

  • 基数(共48个结果(:[15 the_best=true,33 the_best=false]
  • 预期(最多2个the_best=true(:我应该得到35个结果[2 the_best=true,33 the_best=false](

知道吗?:(

一种方法是使用m_search

使用m_Search可以组合多个查询

GET <index>/_msearch
{}
{"query":{"term":{"the_best":true}},"from":0,"size":2}
{}
{"query":{"term":{"the_best":false}},"from":0,"size":15}

如果你想在单一搜索聚合中做到这一点,可以使用(性能会较差(

我使用了过滤器聚合和顶部聚合

{
"size": 0, 
"aggs": {
"true": {
"filter": {
"term": {
"the_best": true
}
},
"aggs": {
"docs": {
"top_hits": {
"size": 2
}
}
}
},
"false": {
"filter": {
"term": {
"the_best": false
}
},
"aggs": {
"docs": {
"top_hits": {
"size": 10
}
}
}
}
}
}

最新更新