是否有Group BY函数用于通过弹性搜索查询查找结果?

时间:2019-06-03 09:12:11

标签: node.js elasticsearch

我尝试通过弹性搜索来整合分组依据。但是我没有得到正确的答案。请支持我解决此问题。索引数据为

data = [
       { "fruit":"apple", "taste":5, "timestamp":100},
       { "fruit":"pear", "taste":5, "timestamp":110},
       { "fruit":"apple", "taste":4, "timestamp":200},
       { "fruit":"pear", "taste":8, "timestamp":90},
       { "fruit":"banana", "taste":5, "timestamp":100}]`
My query is, 
`myQuery = {"query": {
      "match_all": {}
    },
    "aggs": {
      "group_by_fruit": {
        "terms": {
          "field": "fruit.keyword"
        },
      }
    }
    }

它显示输出中的所有5个数据。实际上,我只需要获得3条记录。预期结果是

[
   { "fruit":"apple", "taste":4, "timestamp":200},
   { "fruit":"pear", "taste":8, "timestamp":90},
   { "fruit":"banana", "taste":5, "timestamp":100}]

1 个答案:

答案 0 :(得分:0)

如果要获取唯一水果字段具有时间戳值最大的文档,则应使用top_hits聚合。

{
    "query": {
      "match_all": {}
    },
    "size": 0,
    "aggs": {
        "top_tags": {
            "terms": {
                "field": "fruit.keyword",
                "size": <MAX_NUMBER_OF_DISTINCT_FRUITS>
            },
            "aggs": {
                "group_by_fruit": {
                    "top_hits": {
                        "sort": [
                            {
                                "timestamp": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "size" : 1
                    }
                }
            }
        }
    }
}