Ordering aggregations by terms and metric


#1

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"
}
}
}
}
}
}
}
}


(system) #2