Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting aggreation to the top X hits in elasticsearch

ElasticSearch builds the aggregation results based on all the hits of the query independently of the from and size parameters. This is what we want in most cases, but I have a particular case in which I need to limit the aggregation to the top N hits. The limits filter is not suitable as it does not fetch the best N items but only the first X matching the query (per shard) independently of their score.

Is there any way to build a query whose hit count has an upper limit N in order to be able to build an aggregation limited to those top N results? And if so how?

Subsidiary question: Limiting the score of matching documents could be an alternative even though in my case I would require a fixed bound. Does the min_score parameter affect aggregation?

like image 547
b_habegger Avatar asked Aug 21 '14 15:08

b_habegger


1 Answers

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.

like image 99
Rahul Avatar answered Oct 02 '22 17:10

Rahul