💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Nested Aggregation(嵌套聚合) 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html) 译文链接 : [http://www.apache.wiki/display/Elasticsearch](http://www.apache.wiki/display/Elasticsearch)(修改该链接为 **ApacheCN** 对应的译文链接) 贡献者 : @于永超,[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) ## 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 } } } } ```