# Secondary sort on aggregation buckets

**URL:** <https://discuss.elastic.co/t/secondary-sort-on-aggregation-buckets/18303>\
**Category:** Elasticsearch\
**Created:** [June 24, 2014, 5:17pm UTC](https://discuss.elastic.co/t/secondary-sort-on-aggregation-buckets/18303 "2014-06-24T17:17:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 24, 2014, 5:17pm UTC](https://discuss.elastic.co/t/secondary-sort-on-aggregation-buckets/18303/1 "2014-06-24T17:17:08Z")

</div>

I started investigating switching from facets to aggregations in order to  
have access to some of the new features aggregations offer.

One of them is the ability to sort on a sub-aggregation metric, which is  
working well, but the buckets that are tied come back in a random order.  
Since it is not possible to sort the buckets of a stats aggregation (the  
sub aggregation), there is no explicit secondary order. Is there any way to  
setup the sub aggregations to be returned in order? Default order (count)  
is fine.

The gist of the aggregations is

"aggs": {  
"myagg": {  
"terms": {  
"field": "myfield",  
"order": {  
"maxotherfield": "desc"  
},  
"size": 100  
},  
"aggs": {  
"maxotherfield": {  
"max": {  
"field": "someotherfield"  
}  
}  
}  
}  
}

Haven't looked at the code much, but there is definitely no direct support  
for secondary sorts.

Cheers,

Ivan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDr39Fys73U%3Daz1ry4k2JRWMJb7QmFJvQBAXrE8DYGixg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDr39Fys73U%3Daz1ry4k2JRWMJb7QmFJvQBAXrE8DYGixg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:20am UTC](https://discuss.elastic.co/t/secondary-sort-on-aggregation-buckets/18303/2 "2017-07-06T01:20:06Z")

</div>


