Aggregate/count fields on only first X documents


I have a use-case were I need count the number of docs in the top X documents of my search query that have values in certain fields - taking into consideration different sort orders. E.g. "The X first documents with the given sort order contains 45 models and 70 categories".

E.g. if my search give 25000 hits, I need to be able to present how many of the 5000 first documents that has a value in the fields "modelType" and "modelCategory". Currently I'm fetching the 5000 first documents in a separate query and doing a manual count for the required fields - which is as you can imagine quite slow.

Have anyone a good solution for this case?

I have looked at the Sampler aggregation, which on paper seems to be able to solve my problem - but it is a no-go as it do not seem to consider sorting. Top-hits neither seem to solve the case.

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