Finding sum of average sub aggregations Ask Question


(Sam) #1

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?


(Paul McMahon) #2

Looks like your 'group_by_sum' agg is already a child of 'group_by_smartphones'... Also, the sum_bucket expects 'buckets_path' rather than 'field'... So your pipeline sum can just be:

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

(Sam) #4

Could you please give me the full query because i'm still getting an error. The error is:

Unexpected token VALUE_STRING [field] in [group_by_sum]


(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.