之前的话,排序,是按照每个bucket的doc_count降序来排的
    但是假如说,我们现在统计出来每个颜色的电视的销售额,需要按照销售额降序排序????

    1. GET /tvs/_search
    2. {
    3. "size": 0,
    4. "aggs": {
    5. "group_by_color": {
    6. "terms": {
    7. "field": "color"
    8. },
    9. "aggs": {
    10. "avg_price": {
    11. "avg": {
    12. "field": "price"
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 8,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "group_by_color": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "红色",
              "doc_count": 4,
              "avg_price": {
                "value": 3250
              }
            },
            {
              "key": "绿色",
              "doc_count": 2,
              "avg_price": {
                "value": 2100
              }
            },
            {
              "key": "蓝色",
              "doc_count": 2,
              "avg_price": {
                "value": 2000
              }
            }
          ]
        }
      }
    }
    
    GET /tvs/_search 
    {
      "size": 0,
      "aggs": {
        "group_by_color": {
          "terms": {
            "field": "color",
            "order": {
              "avg_price": "asc"
            }
          },
          "aggs": {
            "avg_price": {
              "avg": {
                "field": "price"
              }
            }
          }
        }
      }
    }