Top Hits Aggregation: Paging

Is it possible apply paging on the Top Hits Aggregation?

I quote from the reference documentation:

The top hits returned in each bucket are the result of running a light mini-query based on the original main query. The mini-query supports the usual features that you would expect from search such as highlighting and pagination.

Using the Java client, I have found a setFrom and setSize, but this probably only applies to the Top Hits and not the number of buckets found. We can set the size of an aggregation, but not the from.

1 Like

The top_hits agg does support from and size, but that is paginate the search hits in a bucket and not to paginate the buckets that result from the terms or other bucket aggs.

There is an issue open in GH fit this feature request: https://github.com/elastic/elasticsearch/issues/4915

But it is tricky to implement efficiently, so it is unknown if and when it will be available in ES. The best way to emulate the support for paginating relatively efficiently is by using the client side work around using include and exclude described in this issue.