Finding sum of average sub aggregations

2019-09-14 06:12发布

问题:

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:

"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?

回答1:

Your group_by_sum aggregation needs to be written like this:

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