title: How can i sort aggregations buckets by metric results?
body: <p>Base on the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order" rel="nofollow">official document</a>, it seems like can only sort on the single-bucket aggregation.
  How can i sort the results by 'avg' in the number of buckets aggregation?</p>
  
  <p>I added 'order' in 'result_1' still not working.
  It shows error below:</p>
  
  <pre><code>TransportError(500, u'search_phase_execution_exception')
  Invalid terms aggregation order path [reverse&gt;condi_range&gt;statistic_score.avg]. 
  Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end. 
  Sub-path [reverse&gt;condi_range] points to non single-bucket aggregation')
  </code></pre>
  
  <p>The key point is that i have to limit the range on 'float_field', and the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/master/search-aggregations-bucket-range-aggregation.html" rel="nofollow">range aggregation</a> is another buckets aggregation. </p>
  
  <p>Does anyone have any idea?</p>
  
  <p>Thanks!</p>
  
  <p>Code Example:</p>
  
  <pre><code>{
    "aggs": {
      "condi_nested": {
        "aggs": {
          "filtered": {
            "filter": {
              "bool": {}
            },
            "aggs": {
              "result_1": {
                "terms": {
                  "field": "Foo",
                  "size": 100000000,
                  "order": {
                    "reverse&gt;condi_range&gt;statistic_score.avg": "desc"
                  }
                },
                "aggs": {
                  "reverse": {
                    "aggs": {
                      "condi_range": {
                        "range": {
                          "ranges": [
                            {
                              "to": 100,
                              "from": 0
                            }
                          ],
                          "field": "float_field"
                        },
                        "aggs": {
                          "percentile_score": {
                            "percentiles": {
                              "field": "float_field"
                            }
                          },
                          "statistic_score": {
                            "extended_stats": {
                              "field": "float_field"
                            }
                          }
                        }
                      }
                    },
                    "reverse_nested": {}
                  }
                }
              }
            }
          }
        },
        "nested": {
          "path": "nested_path_of_Foo"
        }
      }
    }
  }
  </code></pre>