# CompositeAggregation.aggregateAfter usage

**URL:** <https://discuss.elastic.co/t/compositeaggregation-aggregateafter-usage/323196>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 15, 2023, 11:56am UTC](https://discuss.elastic.co/t/compositeaggregation-aggregateafter-usage/323196 "2023-01-15T11:56:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmkhitaryan](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@hmkhitaryan](https://discuss.elastic.co/u/hmkhitaryan)\
**Post date:** [January 15, 2023, 11:56am UTC](https://discuss.elastic.co/t/compositeaggregation-aggregateafter-usage/323196/1 "2023-01-15T11:56:33Z")

</div>

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.

```auto
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))
                        .size(bucketListInfo.getTopResultsCount());
                searchSourceBuilder
                        .from(from)
                        .size(size)
                        .aggregation(aggregationBuilder);
            }
            final SearchRequest searchRequest = new SearchRequest(bucketListInfo.getIndexName())
                    .source(searchSourceBuilder);
            try {
                final SearchResponse response = restHighLevelClient.search(searchRequest, 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;
            }
        }

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 12, 2023, 11:56am UTC](https://discuss.elastic.co/t/compositeaggregation-aggregateafter-usage/323196/2 "2023-02-12T11:56:58Z")

</div>

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