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

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

    {
       "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"
                   }
                }
             }
          }
       }
    }

Check out the sampler aggregation - it's the top N per shard but that might give you enough of what you want

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