CompositeAggregation.aggregateAfter usage

Hello everyone.
this is my code, where I want to gain pagination ability, for not getting the whole bucket list and not to overload RAM memory. But anyway, it's unclear to me:

  1. what is this actually 'afterKey' for, and what is the use case. Yeah I understand that here, as a key of that 'aggregateAfter' map should be set the field, on which it's going to be aggregated the results, but what about the value of that map, what to set there, this is what I don't understand at all. Please have a look on this code, and introduce the changes that should be done here, in order the pagination works for me.
  2. Am I thinking correct way, that here the response(searchResponse) should contain only the paginated results, or I should do more from here to gain that miracle.
public BucketList getListOfBucket(final BucketListInfo bucketListInfo, int from, int size) {
            final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            for (final String aggrField : bucketListInfo.getAggrFieldList()) {
                CompositeAggregationBuilder aggregationBuilder = AggregationBuilders
                        .composite(aggrField, List.of(new TermsValuesSourceBuilder(aggrField).field(aggrField)))
                        .aggregateAfter(Map.of(aggrField, aggrField))
            final SearchRequest searchRequest = new SearchRequest(bucketListInfo.getIndexName())
            try {
                final SearchResponse response =, RequestOptions.DEFAULT);
// here with the response all buckets are coming, instead of only the specified amount(as pagination 'size', and 'from') of buckets to come
                return extractBucketsFromResponse(bucketListInfo, response);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return null;

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