Aggregation Sorting


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

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.


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