# Terms aggs and then sort all data

**URL:** <https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118>\
**Category:** Elasticsearch\
**Created:** [December 26, 2018, 9:30am UTC](https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118 "2018-12-26T09:30:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhang\_chao](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zhang\_chao](https://discuss.elastic.co/u/zhang_chao)\
**Post date:** [December 26, 2018, 9:30am UTC](https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118/1 "2018-12-26T09:30:35Z")

</div>

In my case :

1. I use terms aggs to group my data, and I need to set the size \>\>\> cardinality count (need to group all data for sorting)
2. I use top-hits and size = 1 so I get the first one in every bucket
3. bucket-sort to sort the final all buckets

the problem is the data size is about 10 million, so the terms aggs is very slow  
then i tryed composite aggs ,but it can not sort all the buckets data

I want to group first ,and then get first one in every bucket ,and finally get the sorting buckets

can u help me with my case ? thanks a lot

---

<div class="post-metadata">

**Author:** ![giladgal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giladgal/32/79615_2.png) [@giladgal](https://discuss.elastic.co/u/giladgal)\
**Post date:** [December 27, 2018, 6:48pm UTC](https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118/2 "2018-12-27T18:48:33Z")

</div>

Hi,

Can you please provide some additional information to help us answer:

1. Are you aggregating from multiple fields or from a single field?
2. Are you working on keyword or on a tokenised field?
3. Are you looking to sort the buckets by the number of records containing the term or by alphabetic order based on the first record in the bucket?
4. How many buckets do you expect in the result set (order of magnitude)?

It may also help if you can provide a data sample and the query you use.

Thanks,

Gilad

---

<div class="post-metadata">

**Author:** ![zhang\_chao](https://avatars.discourse-cdn.com/v4/letter/z/59ef9b/32.png) [@zhang\_chao](https://discuss.elastic.co/u/zhang_chao)\
**Post date:** [December 28, 2018, 9:51am UTC](https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118/3 "2018-12-28T09:51:50Z")

</div>

thx for reply,

1. only one field I want to use term aggs on
2. It's a keyword type

about 1 to 2 million data i have to sort after terms aggs

e.g.

POST test\_search\_index/doc/\_bulk  
{"index":{"\_id":"1"}}  
{"name":"Michell","class":"A","age":26,"scoreA":99,"scoreB":100,"scoreC" : 99}  
{"index":{"\_id":"2"}}  
{"name":"Job","class":"B","age":23,"scoreA":98,"scoreB":23,"scoreC" : 23}  
{"index":{"\_id":"3"}}  
{"name":"mata","class":"C","age":21,"scoreA":97,"scoreB":44,"scoreC" : 54}  
{"index":{"\_id":"4"}}  
{"name":"Bob","class":"C","age":20,"scoreA":97,"scoreB":55,"scoreC" : 65}

PUT test\_search\_index/\_mapping/doc  
{  
"doc": {  
"properties": {  
"class": {  
"type": "text",  
"fielddata": true  
}  
}  
}  
}

GET test\_search\_index/\_search

GET \_cat/indices

A . terms aggs -\> group by class , size = all (10000000)  
B . top hits -\> order by desc scoreA,scoreB,scoreC  
C . bucket sort -\> top hits and then order by desc scoreA,scoreB

and here is my query :

GET test\_search\_index/\_search  
{  
"size": 0,  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"class": {  
"terms": {  
"field": "class",  
"size": 5000000  
},  
"aggs": {  
"SortGroup": {  
"bucket\_sort": {  
"sort": [  
{  
"maxScoreA": "desc"  
},  
{  
"maxScoreB": "desc"  
}  
],  
"from": 0,  
"size": 60  
}  
},  
"maxScoreA": {  
"max": {  
"field": "scoreA"  
}  
},  
"maxScoreB": {  
"max": {  
"field": "scoreB"  
}  
},  
"SortWithinGroup": {  
"top\_hits": {  
"sort": [  
{  
"scoreA": "desc"  
},  
{  
"scoreB": "desc"  
},  
{  
"scoreC": "desc"  
}  
],  
"\_source": {  
"includes": [  
"class",  
"name",  
"scoreA",  
"scoreB",  
"scoreC"  
]  
},  
"size": 1  
}  
}  
}  
},  
"count": {  
"cardinality": {  
"field": "goodsSn",  
"precision\_threshold": "40000"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![giladgal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/giladgal/32/79615_2.png) [@giladgal](https://discuss.elastic.co/u/giladgal)\
**Post date:** [January 3, 2019, 3:29pm UTC](https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118/4 "2019-01-03T15:29:02Z")

</div>

Hi,

Thank you for providing these clarifications. Why not sort using the Order within Composite Aggregation? See: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#\_order](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_order) . Please clarify if I'm misunderstanding the need.

Best regards,

Gilad

---

<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:** [January 31, 2019, 3:29pm UTC](https://discuss.elastic.co/t/terms-aggs-and-then-sort-all-data/162118/5 "2019-01-31T15:29:11Z")

</div>

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