> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/362tong-ju540828-bucketaggregations/qian-tao-ju-540828-nested-aggregation.md).

# 嵌套聚合(Nested Aggregation)

一个特殊的single bucket（单桶）聚合，可以聚合嵌套的文档

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

```
{
    ...

    "product" : {
        "properties" : {
            "resellers" : { ＃1
                "type" : "nested",
                "properties" : {
                    "name" : { "type" : "text" },
                    "price" : { "type" : "double" }
                }
            }
        }
    }
}
```

＃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
            }
        }
    }
}
```
