# Sort aggregations by sum of sub aggregations?

**URL:** <https://discuss.elastic.co/t/sort-aggregations-by-sum-of-sub-aggregations/204329>\
**Category:** Elasticsearch\
**Created:** [October 20, 2019, 1:59am UTC](https://discuss.elastic.co/t/sort-aggregations-by-sum-of-sub-aggregations/204329 "2019-10-20T01:59:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casieowen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casieowen/32/8734_2.png) [@casieowen](https://discuss.elastic.co/u/casieowen)\
**Post date:** [October 20, 2019, 1:59am UTC](https://discuss.elastic.co/t/sort-aggregations-by-sum-of-sub-aggregations/204329/1 "2019-10-20T01:59:40Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/a/fabfb7b302d023d0f39e4b2a1fef2a46f932a9bd.png)

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.

Query:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/f/cf42f1340b1401422853c8eeb7556c65a1927d7c.png)

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [October 20, 2019, 9:24am UTC](https://discuss.elastic.co/t/sort-aggregations-by-sum-of-sub-aggregations/204329/2 "2019-10-20T09:24:29Z")

</div>

You can try [Bucket Sort Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html)

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

---

<div class="post-metadata">

**Author:** ![casieowen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/casieowen/32/8734_2.png) [@casieowen](https://discuss.elastic.co/u/casieowen)\
**Post date:** [October 20, 2019, 3:35pm UTC](https://discuss.elastic.co/t/sort-aggregations-by-sum-of-sub-aggregations/204329/3 "2019-10-20T15:35:11Z")

</div>

> [@ylasri](#):
>
> "aggs": { "total\_sales": { "sum": { "field": "price" } }

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:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/d/0d9e6299c009a20d6563cdf2af716ef96d20b255.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/8/98ed75df46c19b087ea648bdd5aab5351b36186d.png)

---

<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:** [November 17, 2019, 3:35pm UTC](https://discuss.elastic.co/t/sort-aggregations-by-sum-of-sub-aggregations/204329/4 "2019-11-17T15:35:28Z")

</div>

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