【问题标题】:ElasticSearch Aggregation on nested field with bucketing on parent id嵌套字段上的 ElasticSearch 聚合与父 ID 上的分桶
【发布时间】:2017-07-20 05:02:46
【问题描述】:

以下是我的文档结构

'Order': {
    u'properties': {
        u'order_id': {u'type': u'integer'},
        'Product': {
            u'properties': {
                u'product_id': {u'type': u'integer'},                
                u'product_category': {'type': 'text'},                
            },
            u'type': u'nested'
        }
    }
}

文档1

"Order": {
    "order_id": "1",
    "Product": [
        {
            "product_id": "1", 
            "product_category": "category_1"
        }, 
        {
            "product_id": "2", 
            "product_category": "category_2"
        },
        {
            "product_id": "3", 
            "product_category": "category_2"
        },
    ] 
}

文档2

"Order": {

    "order_id": "2",
    "Product": [
        {
            "product_id": "4", 
            "product_category": "category_1"
        }, 
        {
            "product_id": "1", 
            "product_category": "category_1"
        },
        {
            "product_id": "2", 
            "product_category": "category_2"
        },
    ] 
}

我想得到以下输出

"aggregations": {
    "Order": [
        {
            "order_id": "1"                
            "category_counts": [
                {
                    "category_1": 1
                },
                {
                    "category_2": 2
                },
            ]
        }, 
        {
            "order_id": "1"                
            "category_counts": [
                {
                    "category_1": 2
                },
                {
                    "category_2": 1
                },
            ]
        }, 
    ]
}

我尝试使用嵌套聚合

"aggs": {
    "Product-nested": {
        "nested": {
            "path": "Product"
        }
        "aggs": {
            "category_counts": {
                "terms": {
                    "field": "Product.product_category"
                }
            }
        }, 
    }
}

它不会为每个订单提供输出,而是为所有订单提供组合输出

{
    "Product-nested": {
        "category_counts": [
            "category_1": 3,
            "category_2": 3
        ]
    }
}

我有两个问题:

  • 如何在上述情况下获得所需的输出?
  • 如果我有一个数组而不是单个 product_category 怎么办? product_categories 那么我们将如何在此实现相同的目标 场景?

我正在使用弹性搜索 >= 5.0

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    我有一个想法,但我认为它不是最好的..

    您可以在“order_id”字段上进行术语聚合,然后在“Product.product_category”上进行子嵌套聚合。

    类似这样的:

    {

    “聚合”:{

    "all-order-id": {
      "terms": {
        "field": "order_id",
        "size": 10
      },
      "aggs": {
        "Product-nested": {
          "nested": {
            "path": "Product"
          },
          "aggs": {
            "all-products-in-order-id": {
              "terms": {
                "field": "Product.product_category"
              }
            }
          }
        }
      }
    }
    

    } }

    抱歉,它的锁有点乱,我对这个答案编辑器不太好

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 2017-09-13
      • 2020-12-21
      • 2018-10-05
      • 1970-01-01
      • 2015-07-30
      • 2016-01-07
      • 1970-01-01
      相关资源
      最近更新 更多