# How to sort based on sum of multiple field value counts

**URL:** <https://discuss.elastic.co/t/how-to-sort-based-on-sum-of-multiple-field-value-counts/145602>\
**Category:** Elasticsearch\
**Created:** [August 22, 2018, 6:44pm UTC](https://discuss.elastic.co/t/how-to-sort-based-on-sum-of-multiple-field-value-counts/145602 "2018-08-22T18:44:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elpaisa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/elpaisa/32/27732_2.png) [@elpaisa](https://discuss.elastic.co/u/elpaisa)\
**Post date:** [August 22, 2018, 6:44pm UTC](https://discuss.elastic.co/t/how-to-sort-based-on-sum-of-multiple-field-value-counts/145602/1 "2018-08-22T18:44:07Z")

</div>

Hi, I have a use case for sorting documents based on the sum of counts of values for an specific field, lets say i have a field `rating`, and the search query gives 20 docs with `rating:High`, and 30 with `rating:Critical`, for a total of 50, then i have 10 with `rating:High` and 5 with `rating:Critical`, for a total of 15, the one with sum of 50 would go first and the one with sum 15 will go next if sort descending, i want to be able to sort search results using the sum of these value counts and do it ascending and descending depending on these counts.

Thanks

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [August 22, 2018, 7:03pm UTC](https://discuss.elastic.co/t/how-to-sort-based-on-sum-of-multiple-field-value-counts/145602/2 "2018-08-22T19:03:01Z")

</div>

Elasticsearch have [script sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting) for if you have to do complex work with the document to produce some value on which to sort. If that is what you are doing then I'd go for that.

I can't 100% tell what you are asking for though so script sorting may not be what you are after.

---

<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:** [September 19, 2018, 7:03pm UTC](https://discuss.elastic.co/t/how-to-sort-based-on-sum-of-multiple-field-value-counts/145602/3 "2018-09-19T19:03:05Z")

</div>

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