# Sorting, Paginating the aggregated data

**URL:** <https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456>\
**Category:** Elasticsearch\
**Created:** [July 9, 2020, 6:29am UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456 "2020-07-09T06:29:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 6:29am UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/1 "2020-07-09T06:29:52Z")

</div>

I need to aggregate the data on the basis of two fields (composite aggregation), sort all the data and then perform pagination. Is is possible using elastic search?

If Yes, Can you please share an example.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 9, 2020, 8:01am UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/2 "2020-07-09T08:01:34Z")

</div>

It's one of those "it depends" answers.  
This [wizard](https://plnkr.co/edit/eZr7r3KZW02AxNKAHCQa?p=preview&preview) will help run through the typical questions that can lead to different answers

---

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 8:24am UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/3 "2020-07-09T08:24:42Z")

</div>

Problem that I am facing is when I am using Composite aggregation, I am not able to sort the buckets on sub-aggregation results. It is only sorting the returned buckets. Is there any way to sort all the buckets of composite aggregation using sub-aggregation field.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 9, 2020, 8:45am UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/4 "2020-07-09T08:45:15Z")

</div>

No, composite aggs are sorted on the choice of key, not the value of some sub aggregation. Hence the question line in the wizard ![Plunker_-_Simple_AngularJS_decision_tree_wizard](https://us1.discourse-cdn.com/elastic/original/3X/a/a/aa3efa33feec4001bffcd4ab21be87c91dc54572.png)

---

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 12:22pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/5 "2020-07-09T12:22:40Z")

</div>

This is suggesting terms aggregation. In terms aggregation, I am facing issue with sorting and pagination when aggregating on the basis of two fields. Following is the JSON example:

```auto
{
  "size": 0,
  "aggs": {
  	"group_by_product" : {
  		"terms" : {
  			"field" : "product.keyword"
  		},
  		"aggs" : {
  			"group_by_brand" : {
  	    	    "terms" : {
  		    	    "field" : "brand"
  		        },
				"aggs" : {
					"totalProducts" : {
						"sum" : {
							"field" : "total"
						}
					}
				}
  			}
  		}
  	}
  }
}

```

I have to sort all the products on the basis of "totalProducts" and perform pagination. Is there any way to achieve that?

If Yes, can you please provide me the example.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 9, 2020, 12:43pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/6 "2020-07-09T12:43:08Z")

</div>

I understand what a person's name and age might be but I don't understand what the "record" field represents.  
Can I check what the business problem is you are trying to solve?

---

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 12:55pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/7 "2020-07-09T12:55:02Z")

</div>

I have also done some correction in the above example. Actually my business problem is

1. I have to group the data on the basis of two fields.
2. Call different aggregation functions (like sum, avg, max, etc.) as shown in above example.
3. Sort the data on the basis of aggregation function field ("totalProducts" as per the above example).
4. Provide pagination for the returned data.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 9, 2020, 12:59pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/8 "2020-07-09T12:59:39Z")

</div>

> [@ankitagarwal9156](#):
>
> I have to group the data on the basis of two fields.

You can use a [script](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-script) in a terms aggregation to combine two document values into a single key.

> [@ankitagarwal9156](#):
>
> Sort the data on the basis of aggregation

See the [order](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order) parameter for the terms agg.

See the warnings in the wizard if you have many shards and many unique keys to sort. There are a number of settings that can help address any accuracy issues

---

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 1:47pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/9 "2020-07-09T13:47:16Z")

</div>

So script helps us to group by on the basis of two fields (like "group by product, brand" in SQL)?

---

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 2:03pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/10 "2020-07-09T14:03:07Z")

</div>

I tried following request:

```auto
{
  "size": 0,
  "aggs": {
  	"groupBy" : {
            "terms" : {
                "script" : {
                    "source": "[doc['product.keyword'].value, doc['brand'].value].join('#')",
                    "lang": "painless"
                },
                "size": 200
            },
	        "aggs": {
	                "totalProducts": {
	                   "sum" : {
								"field" : "total"
							}
	                },
					"sorting" : {
				  		"bucket_sort" :{
				  			"sort" : [
				  				{
				  					"totalProducts" : {
				  						"order" : "desc"
				  					}
				  				}
				  				]
				  		}
				  	}
	        }
        }
  	}
  
}

```

Still the sorting is performed only on the returned buckets instead of all the buckets.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 9, 2020, 2:08pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/11 "2020-07-09T14:08:07Z")

</div>

> [@Mark\_Harwood](#):
>
> See the [order](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order) parameter for the terms agg.

☝☝☝☝

---

<div class="post-metadata">

**Author:** ![ankitagarwal9156](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@ankitagarwal9156](https://discuss.elastic.co/u/ankitagarwal9156)\
**Post date:** [July 9, 2020, 2:16pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/12 "2020-07-09T14:16:52Z")

</div>

Yes, sorting worked correctly now.  
Last two questions:

1. Can we sort on multiple fields at a time?
2. Do we support pagination in terms aggregation?

---

<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:** [August 6, 2020, 2:17pm UTC](https://discuss.elastic.co/t/sorting-paginating-the-aggregated-data/240456/13 "2020-08-06T14:17:00Z")

</div>

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