Limiting aggreation to the top X hits in elasticsearch Limiting aggreation to the top X hits in elasticsearch elasticsearch elasticsearch

Limiting aggreation to the top X hits in elasticsearch


You are looking for Sampler Aggregation.

I have a similar answer explained here

Optionally, you can use the field or script and max_docs_per_value settings to control the maximum number of documents collected on any one shard which share a common value.


If you are using an ElasticSearch cluster with version > 1.3, you can use top_hits aggregation by nesting it in your aggregation, ordering on the field you want and set the size parameter to X.

The related documentation can be found here.


I need to limit the aggregation to the top N hits

With nested aggregations, your top bucket can represent those N hits, with nested aggregations operating on that bucket. I would try a filter aggregation for the top level aggregation.

The tricky part is to make use the of _score somehow in the filter and to limit it exactly to N entries... There is a limit filter that works per shard, but I don't think it would work in this context.