Sorting through average aggregation

I have a nested aggregate query. First, I group the items by phone, then by carrier, and then by their average prices. I want the average pricing to be in descending order. For example, I want my documents to look like this:

.iPhone 6

...Verizon

......$250

...T-Mobile

......$235

...Sprint

........$220

Here's my current query:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "market": "American"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "Purchase": {
              "gte": 20170530,
              "lte": 20170602
            }
          }
        }
      ]
    }
  },

  "aggs": {
    "group_by_phone": {
      "terms": {
        "field": "phone",
        "order": {
          "_term": "asc"
        },
        "size": 200
      },
      "aggs": {
        "group_by_carrier": {
          "terms": {
            "field": "carrier"
          },
          "aggs": {
            "group_by_avg": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    },
    "group_by_carriers": {
      "terms": {
        "field": "carriers",
        "order": {
          "_term": "asc"
        },
        "size": 200
      }
    }
  }
}

How do I put the pricing in descending order?

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