# Sorting based on distinct counts

**URL:** <https://discuss.elastic.co/t/sorting-based-on-distinct-counts/58739>\
**Category:** Elasticsearch\
**Created:** [August 23, 2016, 8:20pm UTC](https://discuss.elastic.co/t/sorting-based-on-distinct-counts/58739 "2016-08-23T20:20:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sundar\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sundar_v/32/6928_2.png) [@Sundar\_V](https://discuss.elastic.co/u/Sundar_V)\
**Post date:** [August 23, 2016, 8:20pm UTC](https://discuss.elastic.co/t/sorting-based-on-distinct-counts/58739/1 "2016-08-23T20:20:11Z")

</div>

Using cardinality aggs, I'm able to get the distinct count of elements.

Instead of aggs, we want to get the list of matching elements using query. And this query, we would like to get it sorted using the distinct count. I looked into the function score query, but using that I'm not able to get the distinct count of a particular field.

How can I do this - to calculate the score based on the distinct count.

---

<div class="post-metadata">

**Author:** ![DiscussBuster](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/discussbuster/32/11042_2.png) [@DiscussBuster](https://discuss.elastic.co/u/DiscussBuster)\
**Post date:** [August 24, 2016, 1:28am UTC](https://discuss.elastic.co/t/sorting-based-on-distinct-counts/58739/2 "2016-08-24T01:28:24Z")

</div>

I don't know any way for you to achieve this. Why aren't aggregations a method for you to apply?

---

<div class="post-metadata">

**Author:** ![Sundar\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sundar_v/32/6928_2.png) [@Sundar\_V](https://discuss.elastic.co/u/Sundar_V)\
**Post date:** [August 24, 2016, 4:03pm UTC](https://discuss.elastic.co/t/sorting-based-on-distinct-counts/58739/3 "2016-08-24T16:03:48Z")

</div>

@DiscussBuster we want to show the list of documents sorted by the most users who have seen that error. There is a parent child relationship and we want to count the users in the child type and then find the parent types. Hence, I assume this won't work with aggregations.

---

<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:** [July 5, 2017, 10:25pm UTC](https://discuss.elastic.co/t/sorting-based-on-distinct-counts/58739/4 "2017-07-05T22:25:28Z")

</div>


