Limit aggreation to top X hit in elasticsearch

ElasticSearch generates aggregation results based on all query hits, regardless of the from and size parameters. This is what we want in most cases, but I have a special case when I need to restrict aggregation to vertices N. The restriction filter is not suitable, because it does not extract the best N elements, but only the first X that matches the query (for the splinter) independently from their assessment.

Is there a way to build a query whose hit counter has an upper limit of N to be able to build aggregation limited to these results in upper N? And if so, how?

Auxiliary question: Limiting the evaluation of matching documents can be an alternative, although in my case I would require a fixed binding. Does min_score affect aggregation?

+6
source share
4 answers

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

Relevant documentation can be found here .

0
source

I need to limit aggregation to the top N strokes

With nested aggregates, your top bucket can represent these N hits with nested aggregates running on this bucket. I would try filter aggregation for top level aggregation.

The hard part is to somehow use the _score filter in the filter and limit it to exactly N entries ... There is a limit that works for every splinter, but I don't think it will work in that context.

0
source

Now for this purpose you can now use Sampler Aggregation . Please note that it is only available with Elastic 2.0.

0
source

You are looking for sampler aggregation .

I have a similar answer explained here

If you wish, you can use the field or script and the max_docs_per_value settings to control the maximum number of documents collected on any single fragment that has a common value.

0
source

Source: https://habr.com/ru/post/974162/


All Articles