弹性搜索选择要在聚合中返回的字段

时间:2016-02-22 13:57:16

标签: elasticsearch

如何选择在聚合存储桶中返回哪些字段?

我的数据中有很多字段,如

Document:{
"contenthash": "0a12ac12ac12ac12ac12ac12"
"time": "01:01:01"
"Content": "hello"
}

当我在内容哈希上聚合时,我得到的桶只包含内容哈希而不包含其他字段。我希望能够做的是返回与该内容哈希相关的所有字段。所以我有:

"buckets": {
"key"  :{ "0a12ac12ac12ac12ac12ac12":
"time" : "01:01:01"
"Content" : "hello"}

我知道我可以进行子聚合来获取内容哈希下的数据,但有更简单的方法吗?

1 个答案:

答案 0 :(得分:1)

我相信你要找的是top hits aggregation

如果您将聚合设计为这样:

{
  "aggs": {
    "byHash" : {
      "terms": {
        "field" : "contenthash"
      },
      "aggs": {
        "top": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

然后,您会看到,对于每个唯一 contenthash ,在 contenthash 存储桶中汇总的最相关的源文档。

相关问题