当前位置:   article > 正文

ElasticSearch 实战:嵌套字段的聚合操作_es嵌套对象聚合查询

es嵌套对象聚合查询

Elasticsearch中,如果您的文档结构包含嵌套对象(nested fields),直接对这些嵌套字段进行聚合可能会得到不准确的结果,因为Elasticsearch默认会扁平化处理嵌套对象。要正确地对嵌套字段进行聚合操作,您需要使用Elasticsearch提供的nested聚合类型。以下是一个实战示例,说明如何对嵌套字段进行聚合:

假设您有一个名为orders的索引,其中包含购买订单数据,每个订单文档结构如下:

{
  "customer_id": "123",
  "order_date": "2022-0¼-01",
  "products": [
    {
      "product_id": "P001",
      "quantity": 5,
      "price_per_unit": 10.99
    },
    {
      "product_id": "P002",
      "quantity": 3,
      "price_per_unit": 19.99
    }
  ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这个例子中,products是一个嵌套类型的字段,因为它包含了多个具有相同结构的子对象。现在,您想按customer_id分组,然后计算每个客户购买的每个产品的总销售额。

步骤如下:

  1. 定义嵌套类型
    在创建索引时,确保已将products字段标记为nested类型。例如:

    PUT orders
    {
      "mappings": {
        "properties": {
          "customer_id": {
            "type": "keyword"
          },
          "order_date": {
            "type": "date"
          },
          "products": {
            "type": "nested",  // 关键在这里
            "properties": {
              "product_id": {
                "type": "keyword"
              },
              "quantity": {
                "type": "long"
              },
              "price_per_unit": {
                "type": "float"
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  2. 执行嵌套聚合
    使用nested聚合来处理嵌套字段,并在嵌套上下文中执行所需的度量聚合(如sum):

    GET orders/_search
    {
      "size": 0,  // 只返回聚合结果
      "aggs": {
        "customers": {
          "terms": {
            "field": "customer_id.keyword"
          },
          "aggs": {
            "products": {
              "nested": {
                "path": "products"
              },
              "aggs": {
                "sales_per_product": {
                  "terms": {
                    "field": "products.product_id.keyword"
                  },
                  "aggs": {
                    "total_sales": {
                      "sum": {
                        "field": "products.quantity",
                        "script": {
                          "source": "doc['products.price_per_unit'].value * doc['products.quantity'].value"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

在这个查询中:

  • customers聚合使用termscustomer_id进行分组。
  • products聚合是一个nested聚合,指定了嵌套路径为products,这样Elasticsearch就会知道要对哪个嵌套字段进行操作。
  • sales_per_product聚合进一步按products.product_id进行分组。
  • 最后,total_sales聚合使用sum聚合器计算每个产品在每个订单中的销售额(即quantity乘以price_per_unit)。

查询结果将展示每个客户的ID,以及他们购买的每个产品的总销售额。这就是对Elasticsearch中嵌套字段进行聚合操作的一个实战示例。根据实际业务需求,您可以调整聚合结构和参数,以实现更复杂的分析。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/463451
推荐阅读
相关标签
  

闽ICP备14008679号