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?

You need to put the group_by_rating aggregation i side the aggs object which is under group_by_carrier. Like this:

{  
   "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"
                    }
                 }
               }
            }
         }
      }
   }
}

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