如何在Elastic中将字段值聚合到数组中

时间:2017-07-31 07:49:27

标签: elasticsearch aggregate

在ElasticSearch 5.x索引中有很多文档:

{
  "price": 5.1, 
  "property": 5,
  "key": "some-key1"
},
{
  "price": 7.1, 
  "property": 5,
  "key": "some-key2"
}

priceproperty是数字,key在{Elastic}中有keyword类型。

我可以轻松地将它们聚合为一个字段price,按property字段分组。

但我想在按属性字段分组的数组中汇总key值。

以下是一个例子:

{
  "aggs" : {
    "by_property" : {
      "terms": { "field": "property" }
      "aggs": {
        "sum_by_property": {
          "sum" : { "field" : "price" } 
        }
      }
    }
  }
}

这将返回按字段price

分组的字段property的总和

如何修改查询以使其在数组中返回key值?按字段property分组:

{
  "key": 5,
  "sum_by_property": { "value": 12.2 },
  "keys_array": { "value": ["some-key1", "some-key2"] } //how to get this?
}

0 个答案:

没有答案
相关问题