原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html

译文链接 : http://www.apache.wiki/display/Elasticsearch(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : @于永超,ApacheCNApache中文网

Nested Aggregation

一个特殊的single bucket(单桶)聚合,可以聚合嵌套的文档

例如,假设我们有一个产品的索引,并且每个产品都包含一个分销商列表——每个产品都有自己的价格。映射可能是:

  1. {
  2. ...
  3. "product" : {
  4. "properties" : {
  5. "resellers" : { 1
  6. "type" : "nested",
  7. "properties" : {
  8. "name" : { "type" : "text" },
  9. "price" : { "type" : "double" }
  10. }
  11. }
  12. }
  13. }
  14. }

#1 resellers是一个在product对象下保存嵌套文档的数组。

以下汇总将返回可以购买的最低价格产品:

{
    "query" : {
        "match" : { "name" : "led tv" }
    },
    "aggs" : {
        "resellers" : {
            "nested" : {
                "path" : "resellers"
            },
            "aggs" : {
                "min_price" : { "min" : { "field" : "resellers.price" } }
            }
        }
    }
}

如上所述,嵌套聚合需要顶层文档中嵌套文档的路径。 然后可以在这些嵌套文档上定义任何类型的聚合。

响应结果:

{
    "aggregations": {
        "resellers": {
            "min_price": {
                "value" : 350
            }
        }
    }
}