Sort aggregations by sum of sub aggregations?

The query at the bottom of this thread returns data in this shape, ordered by the key value asc:

Is there a way for me to sort the aggregations by the sum of the user_count values per key? I want the data to stay in the same shape, but just change the sorting.


You can try Bucket Sort Aggregation

here is an example to get the top 3 sales buckets with highest sum ...

POST /sales/_search
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "calendar_interval" : "month"
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                "sales_bucket_sort": {
                    "bucket_sort": {
                        "sort": [
                          {"total_sales": {"order": "desc"}}
                        "size": 3

Thanks for the reply! It looks like this would work except that the cluster is currently using version 5.2.2 and it looks like bucket sort aggregation isn't available in that version. I think I have the query formatted correctly (shown below) but am getting:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.