# Ordering aggregations by terms and metric

**URL:** <https://discuss.elastic.co/t/ordering-aggregations-by-terms-and-metric/42628>\
**Category:** Elasticsearch\
**Created:** [February 24, 2016, 6:00pm UTC](https://discuss.elastic.co/t/ordering-aggregations-by-terms-and-metric/42628 "2016-02-24T18:00:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bret](https://avatars.discourse-cdn.com/v4/letter/b/8baadc/32.png) [@Bret](https://discuss.elastic.co/u/Bret)\
**Post date:** [February 24, 2016, 6:00pm UTC](https://discuss.elastic.co/t/ordering-aggregations-by-terms-and-metric/42628/1 "2016-02-24T18:00:37Z")

</div>

I'm new to Elasticsearch and have a question around ordering of term (bucket) and metric aggregations. The aggregate search is pretty straight forward. I want to aggregate the average order by State and City. I want the results ordered by the two terms (State, City) AND the average order amount.

It seems like only one order property is used when sorting the results for the City term aggregate. I can either order the city aggregate by \_term or by the metric average\_order but not both. I am running Elasticsearch 1.7.2 in my development environment.

Are there limits to the number of order fields you can specify in a term (bucket) aggregate? I apologize if this has been discussed in depth. Thanks in advance!

{  
"aggregations": {  
"group\_by\_state": {  
"terms": {  
"size": 100,  
"field": "PurchaseOrderCreated.BillingAddress.State",  
"order" : { "\_term" : "asc" }  
},  
"aggregations": {  
"group\_by\_city": {  
"terms": {  
"size": 100,  
"field": "PurchaseOrderCreated.BillingAddress.City",  
"order" : {  
"\_term" : "asc",  
"average\_order" : "desc"  
}  
},  
"aggregations": {  
"average\_order": {  
"avg": {  
"field": "PurchaseOrderCreated.OrderTotal"  
}  
}  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:13pm UTC](https://discuss.elastic.co/t/ordering-aggregations-by-terms-and-metric/42628/2 "2017-07-05T23:13:53Z")

</div>


