求平均子聚合的总和

问题描述:

我想得到一个子聚合的总和.例如,我先按智能手机分组,然后按运营商分组,然后找到该特定智能手机的每个运营商的平均价格.我想获取每部智能手机的所有运营商的平均价格之和.所以本质上,我想要这样的东西:

I'd like to get the sum of a sub aggregation. For example, I'm grouping by smartphones, then by carrier, and then I'm finding the average price of each carrier for that particular smartphone. I'd like to get the sum of the average prices for all carriers for each smartphone. So essentially, I want something like this:

{
  "aggs": {
    "group_by_smartphones": {
      "terms": {
        "field": "smartphone",
        "order": {
          "_term": "asc"
        },
        "size": 200
      },
      "aggs": {
        "group_by_carrier": {
          "terms": {
            "field": "carrier",
            "order": {
              "group_by_avg": "desc"
            }
          },
          "aggs": {
            "group_by_avg": {
              "avg": {
                "field": "price"
              }
            }
          }
        },
        "group_by_sum": {
          "sum_bucket": {
            "field": "group_by_smartphones>group_by_carrier>group_by_avg"
          }
        }
      }
    }
  }
}

当我尝试执行此查询时,出现错误消息:

When I try doing this query, I get an error saying:

类型":"parsing_exception",原因":意外令牌VALUE_STRING [group_by_sum]中的[field]",

"type": "parsing_exception", "reason": "Unexpected token VALUE_STRING [field] in [group_by_sum]",

因此本质上,我想获取特定智能手机的所有运营商平均值的总和.有办法吗?

So essentially I want to get the sum of the averages of all carriers for a particular smartphone. Is there a way to get this?

您的group_by_sum聚合需要这样写:

    "group_by_sum": {
      "sum_bucket": {
        "buckets_path": "group_by_carrier>group_by_avg"
      }
    }