# Sorting Followed by Aggregation

**URL:** <https://discuss.elastic.co/t/sorting-followed-by-aggregation/152937>\
**Category:** Elasticsearch\
**Created:** [October 18, 2018, 6:08am UTC](https://discuss.elastic.co/t/sorting-followed-by-aggregation/152937 "2018-10-18T06:08:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vnitdeepak](https://avatars.discourse-cdn.com/v4/letter/v/9fc29f/32.png) [@vnitdeepak](https://discuss.elastic.co/u/vnitdeepak)\
**Post date:** [October 18, 2018, 6:08am UTC](https://discuss.elastic.co/t/sorting-followed-by-aggregation/152937/1 "2018-10-18T06:08:11Z")

</div>

I want ten distinct values from category field but I don't want random distinct values. The ten distinct values must be latest (uploadedDate field sort desc).

---

<div class="post-metadata">

**Author:** ![klof](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/klof/32/31590_2.png) [@klof](https://discuss.elastic.co/u/klof)\
**Post date:** [October 18, 2018, 12:54pm UTC](https://discuss.elastic.co/t/sorting-followed-by-aggregation/152937/2 "2018-10-18T12:54:16Z")

</div>

By default, the aggregations are sorted by doc\_count descendant, it's not random.  
But maybe to achieve what you want, you could use the **field collapsing** feature.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)

So, you can still sort by uploadedDate field, and collapse on the category field.

---

<div class="post-metadata">

**Author:** ![vnitdeepak](https://avatars.discourse-cdn.com/v4/letter/v/9fc29f/32.png) [@vnitdeepak](https://discuss.elastic.co/u/vnitdeepak)\
**Post date:** [October 22, 2018, 4:58am UTC](https://discuss.elastic.co/t/sorting-followed-by-aggregation/152937/3 "2018-10-22T04:58:59Z")

</div>

Klof, Thank You for the solution.

---

<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 19, 2018, 4:59am UTC](https://discuss.elastic.co/t/sorting-followed-by-aggregation/152937/4 "2018-11-19T04:59:00Z")

</div>

Closed on November 19, 2018, 4:59am UTC

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