# Aggregation Sorting

**URL:** <https://forum.opensearch.org/t/aggregation-sorting/10059>\
**Category:** OpenDistro\
**Tags:** troubleshoot\
**Created:** [June 24, 2022, 9:34pm UTC](https://forum.opensearch.org/t/aggregation-sorting/10059 "2022-06-24T21:34:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielG](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/danielg/32/2543_2.png) [@DanielG](https://forum.opensearch.org/u/DanielG)\
**Post date:** [June 24, 2022, 9:34pm UTC](https://forum.opensearch.org/t/aggregation-sorting/10059/1 "2022-06-24T21:34:24Z")

</div>

Hello!

I’m doing an aggregation and I want to sort the aggregation by the relevance. The documentation says the aggregation default sort is by the max count. However, I would like to sort the aggregate by the relevance ‘\_score’ so that the aggs are in the same order as the query results. How can I do this please?

Great tool by the way. The learning curve is a little steep, however. lol

---

<div class="post-metadata">

**Author:** ![DanielG](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/danielg/32/2543_2.png) [@DanielG](https://forum.opensearch.org/u/DanielG)\
**Post date:** [June 25, 2022, 1:14pm UTC](https://forum.opensearch.org/t/aggregation-sorting/10059/2 "2022-06-25T13:14:39Z")

</div>

I’m using OpenSearch v1 in AWS.

I don’t have the exact solution I want. However, I found one the comes close.

I really wanted the aggs to reflect the hits that are returned instead of being different. For example, the hits result have an ordered “\_score” value for each result, and then ordered accordingly . But the aggs is ordered by how many accordance are found in the data.

I wanted to return hits as well as aggs, in the same order. Not possible. There is a workaround that comes close however. If anyone has a better solution to order the hits and aggs the same, please let me know.

The results are close but I’d like to be exact.

Cheers

```auto
...
    "aggs": {
      "suggestions": {
        "terms": {
          "size": 0,
          "field": "suggestion.raw",
          "order": {
            "score": "desc"
          }
        },
        "aggs": {
          "score": {
            "max": {
              "script": "_score"
            }
          }
        }
      }
    }

```
