ElasticSearch学习笔记之二十六 嵌套桶聚合

ElasticSearch学习笔记之二十六 嵌套桶聚合

Nested Aggregation(嵌套聚合)

Nested Aggregation是一个可以对嵌套对象文档进行聚合的单分组聚合。

举例来说 我们有一个产品的索引,每个产品有着不同的经销商和不同的价格. 映射如下:

PUT /index
{
  "mappings": {
    "product" : {
        "properties" : {
            "resellers" : { 
                "type" : "nested",
                "properties" : {
                    "name" : { "type" : "text" },
                    "price" : { "type" : "double" }
                }
            }
        }
    }
  }
}

产品根对象有一个嵌套数组对象resellers。
下面的聚合会返回产品的最低价格:

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

如你所见, nested aggregation需要定义嵌套对象在顶级对象的路径。我们可以在嵌入对象上定义任何类型的聚合。

响应如下:

{
  ...
  "aggregations": {
    "resellers": {
      "doc_count": 0,
      "min_price": {
        "value": 350
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_43430036/article/details/83349221
今日推荐