# Sort aggregation based on TopHits (ie top 10) average score

**URL:** <https://discuss.elastic.co/t/sort-aggregation-based-on-tophits-ie-top-10-average-score/271358>\
**Category:** Elasticsearch\
**Created:** [April 27, 2021, 10:22am UTC](https://discuss.elastic.co/t/sort-aggregation-based-on-tophits-ie-top-10-average-score/271358 "2021-04-27T10:22:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JF2018](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@JF2018](https://discuss.elastic.co/u/JF2018)\
**Post date:** [April 27, 2021, 10:22am UTC](https://discuss.elastic.co/t/sort-aggregation-based-on-tophits-ie-top-10-average-score/271358/1 "2021-04-27T10:22:27Z")

</div>

Hi

I wish to be able to sort my buckets from a termAggregation, based on the scores of the top N documents in each bucket.  
I know how to sort by min/max/avg/sum score for all documents in a bucket (See example I further down), but I wish to sort by avg/sum of only top N documents in each bucket.

I have tried to used topHits aggregation, but can get it working, since it is not a bucketAggregation, and thus I can not make nested aggregations with it.

Will appreciate any help, guiding me in a direction of doing this.

Thanks  
Jens

```auto
    {
       "size": 0,
       "query": {
          "nested": {
             "path": "<somePath>",
             "query": {
                "term": {
                   "<someField>": {
                      "value": "1808439"
                   }
                }
             }
          }
       },
       "aggs": {
          "agg_name": {
             "terms": {
                "field": "<someField>",
                "order": {
                   "sum_score": "desc"
                }, 
                "size": "3"
             },
             "aggs": {
                "sum_score": {
                   "sum": {
                      "script": "_score"
                   }
                }
             }
          }
       }
    }

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 27, 2021, 10:31am UTC](https://discuss.elastic.co/t/sort-aggregation-based-on-tophits-ie-top-10-average-score/271358/2 "2021-04-27T10:31:02Z")

</div>

Check out the [sampler](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html) aggregation - it's the top N per shard but that might give you enough of what you want

---

<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:** [May 25, 2021, 10:31am UTC](https://discuss.elastic.co/t/sort-aggregation-based-on-tophits-ie-top-10-average-score/271358/3 "2021-05-25T10:31:29Z")

</div>

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