# Disable order over Terms Aggregation

**URL:** <https://discuss.elastic.co/t/disable-order-over-terms-aggregation/130829>\
**Category:** Elasticsearch\
**Created:** [May 7, 2018, 11:46am UTC](https://discuss.elastic.co/t/disable-order-over-terms-aggregation/130829 "2018-05-07T11:46:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [May 7, 2018, 11:46am UTC](https://discuss.elastic.co/t/disable-order-over-terms-aggregation/130829/1 "2018-05-07T11:46:47Z")

</div>

Hi,

I have been experimenting out performance of different Elasticsearch queries. I intend to fetch all the unique values for a particular field. As the number of unique values increase, the time for performing Terms aggregation increase.

So I decided to split out the results into a number of partitions based on the results from cardinality query. This improved the performance of ES queries. I feel that the query can be further optimised if I somehow disable the `order` in which buckets are returned. Currently, I see that even within the partition the aggregations are sorted on the basis of doc\_count (default behaviour). For my use case I don't require any of the sorting, is there a way to disable sorting for Terms Aggregation?

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [May 10, 2018, 9:58am UTC](https://discuss.elastic.co/t/disable-order-over-terms-aggregation/130829/2 "2018-05-10T09:58:28Z")

</div>

I believe the default order is based on doc\_count, and there isn't any document order similar to one we use in normal queries `"sort": ["_doc"]`.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 10, 2018, 3:40pm UTC](https://discuss.elastic.co/t/disable-order-over-terms-aggregation/130829/3 "2018-05-10T15:40:23Z")

</div>

Terms aggregation isn't really designed for returning "all" or very large sets of terms. It's optimized for the top-n scenario.

If you don't need top-n (e.g. don't care about order, and just want all the terms), you can use the newish `composite` aggregation: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html)

This is a new type of agg that basically "scrolls" over the aggregation results in a memory-friendly manner, allowing you to paginate through the returned buckets and retrieve all the results without killing your cluster. The tradeoff is that there is no sorting available... which is fine for your use-case 🙂

---

<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:** [June 7, 2018, 3:40pm UTC](https://discuss.elastic.co/t/disable-order-over-terms-aggregation/130829/4 "2018-06-07T15:40:26Z")

</div>

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