# Sorting reverse nested aggregations

**URL:** <https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190>\
**Category:** Elasticsearch\
**Created:** [July 11, 2016, 12:42pm UTC](https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190 "2016-07-11T12:42:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drjz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drjz/32/972_2.png) [@drjz](https://discuss.elastic.co/u/drjz)\
**Post date:** [July 11, 2016, 12:42pm UTC](https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190/1 "2016-07-11T12:42:21Z")

</div>

Hi all,

I would like to sort a list of **nested aggregations** by their document count (and not terms count). This can be done using a **reverse nested** aggregation.

More than a year ago I've asked this question on the old mailing list, see here [Reverse Nested Aggregation Sort](http://discuss.elastic.co/t/reverse-nested-aggregation-sort/22831).

There never came an answer. Perhaps new insights can be gained now.

Assume I have as example this response (snippet) for a reverse nested aggregation:

> "aggregations": {  
> "values": {  
> "doc\_count": 350627,  
> "values\_filter": {  
> "doc\_count": 79325,  
> "values": {  
> "doc\_count\_error\_upper\_bound": 153,  
> "sum\_other\_doc\_count": 33654,  
> "buckets": [  
> {  
> "key": "Alpha",  
> "doc\_count": 395,  
> "values\_docCount": {  
> "doc\_count": 381  
> }  
> },  
> {  
> "key": "Beta",  
> "doc\_count": 366,  
> "values\_docCount": {  
> "doc\_count": 352  
> }  
> },  
> {  
> "key": "Gamma",  
> "doc\_count": 335,  
> "values\_docCount": {  
> "doc\_count": 331  
> }  
> },

What is the best way to sort this list in Elasticsearch based on the "values\_docCount.doc\_count"? Is script-based sorting the recommended approach?

---

<div class="post-metadata">

**Author:** ![drjz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/drjz/32/972_2.png) [@drjz](https://discuss.elastic.co/u/drjz)\
**Post date:** [July 11, 2016, 1:29pm UTC](https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190/2 "2016-07-11T13:29:58Z")

</div>

I've found out that you can do this easily now with deep metrics, see [https://www.elastic.co/guide/en/elasticsearch/guide/current/\_sorting\_based\_on\_deep\_metrics.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/_sorting_based_on_deep_metrics.html)

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 11, 2016, 3:01pm UTC](https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190/3 "2016-07-11T15:01:45Z")

</div>

Beat me to the answer 🙂 Yep, that's exactly how you'd do it!

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 11, 2016, 3:08pm UTC](https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190/4 "2016-07-11T15:08:10Z")

</div>

Just a note: @colings86 directed me to an outstanding issue where `"nested"` aggregations don't sort quite correctly: [https://github.com/elastic/elasticsearch/issues/16838#issuecomment-190210463](https://github.com/elastic/elasticsearch/issues/16838#issuecomment-190210463)

When you say "nested", if you just mean a multi-layer, embedded aggregation, you can ignore that issue. But if you mean the [Nested Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html), it may be relevant.

---

<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, 10:36pm UTC](https://discuss.elastic.co/t/sorting-reverse-nested-aggregations/55190/5 "2017-07-05T22:36:33Z")

</div>


