user2896120
user2896120

Reputation: 3282

Getting multiple average aggregations

I have a query where I'm grouping by smartphone and then by carrier. Inside these nested aggregations, I want to find the avg price, rating, and speed of each carrier for that particular smartphone. This is my current query:

{
  "aggs": {
    "group_by_smartphone": {
      "terms": {
        "field": "smartphone"
      },
      "aggs": {
        "group_by_carrier": {
          "terms": {
            "field": "carrier"
          },
          "aggs": {
            "group_by_avg_price": {
              "avg": {
                "field": "price"
              }
            }
          },

            "group_by_rating":{
              "avg":{
                "field": "rating"
              }
            }

        }
      }
    }
  }
}

With this current query, I'm getting an error saying:

"type": "parsing_exception", "reason": "Found two aggregation type definitions in [group_by_carrier]: [terms] and [group_by_rating]",

How can I fix this in order to display the correct results of each average?

Upvotes: 0

Views: 730

Answers (1)

Val
Val

Reputation: 217344

Try this instead:

{
  "aggs": {
    "group_by_smartphone": {
      "terms": {
        "field": "smartphone"
      },
      "aggs": {
        "group_by_carrier": {
          "terms": {
            "field": "carrier"
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            },
            "avg_rating":{
              "avg":{
                "field": "rating"
              }
            },
            "avg_speed":{
              "avg":{
                "field": "speed"
              }
            }
          }    
        }
      }
    }
  }
}

All sub-aggregations need to be located inside the same aggs structure.

Upvotes: 1

Related Questions