Sorting reverse nested aggregations

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.

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?

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

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

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

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, it may be relevant.

1 Like