Elasticsearch:过滤具有最高字段值的结果

时间:2014-12-19 21:07:26

标签: elasticsearch

我有两个字段,文本和大小的对象:

{"text":"example text", "size":"0"}
{"text":"some text for example", "size":"1"}
{"text":"other text for example", "size":"1"}
{"text":"example with different size", "size":"2"}
{"text":"just an example", "size":"2"}

例如,我使用match查询“示例”,因此所有对象都将匹配。接下来,我想过滤那些具有最大size字段值的字符串,在这种情况下为2。

最大值应仅在匹配的字段上进行。因此,如果我搜索的“文本”仅匹配大小为1的前两行,则应按size == 1过滤对象。

我可以通过两个查询获得它,首先获得最大值,第二个过滤,但是可以在一个查询中执行此操作吗?

1 个答案:

答案 0 :(得分:0)

我能想到的唯一选择是按您的尺寸字段sort数据:

{
  "sort" : [
    { "size" : {"order" : "desc"}}
],
"query" : {
    ...
    }
}

否则 - 是的,您需要按照您的描述进行查询。