title: ElasticSearch sorting term aggregation buckets
body: <p>I am using term aggregation to get distinct records which mean each bucket will end up having one set of record but i could end up having a 5000 buckets total. what I want is those bucket results to be sorted by one of the properties of the document I am returning. my query looks: </p>
  
  <pre><code>                var response = await
                 _elasticClient.SearchAsync&lt;MyObject&gt;(
                     s =&gt; s
                         .Index("myIndex")
                         .Type("myDocType")
                         .Size(0)
                         .Aggregations(a =&gt; a
                             .Terms("distinct_by_code", t =&gt; t
                             .Field("code.keyword")                                                                                      
                                .Aggregations(
                                    aa =&gt; aa.TopHits("distinct_hits", ds =&gt; ds                                                        
                                          .Source(src =&gt; src.Excludes(
                                                      ex =&gt; ex Field(f =&gt; f.Description)))
                                          .Size(1)
                                    )
                                )
                                .Size(5000)
                                //sort the buckets by price
                             )));
  </code></pre>
  
  <p>First of, each bucket could return more than one hits but i am only keeping one because I am selecting distinct by "code.keyword". The challenge is sorting all the hits in each buckets by a certain field so that a bucket with highest price will come first or I can pass a field I want it be sorted on.</p>
  
  <p>Thanks</p>