# Pagination on composite aggregation

**URL:** <https://discuss.elastic.co/t/pagination-on-composite-aggregation/322667>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [January 7, 2023, 3:25pm UTC](https://discuss.elastic.co/t/pagination-on-composite-aggregation/322667 "2023-01-07T15:25:38Z")\
**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 7, 2023, 3:25pm UTC](https://discuss.elastic.co/t/pagination-on-composite-aggregation/322667/1 "2023-01-07T15:25:38Z")

</div>

Hi. I'm having such kind of problem on defining composite aggregation for paginating the results.

```auto
CompositeAggregationBuilder aggregationBuilder = AggregationBuilders
                    .composite(aggrField, List.of(new TermsValuesSourceBuilder(aggrField).field(aggrField)))
                    .aggregateAfter(Map.of("keyword", aggrField))
                    .size(bucketListInfo.getTopResultsCount());
            searchSourceBuilder
                    .from(paginatedRequestInfo.from())
                    .size(paginatedRequestInfo.size())
                    .aggregation(aggregationBuilder);

final SearchRequest searchRequest = new SearchRequest(bucketListInfo.getIndexName())
                .source(searchSourceBuilder);
        try {
            final SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            return extractBucketsFromResponse1(bucketListInfo, response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }

```

and this is the [extractBucketsFromResponse1] method definition.

```auto
    private static BucketList extractBucketsFromResponse1(final BucketListInfo bucketListInfo, final SearchResponse response) {
        final BucketList bucketList = new BucketList();
        for (String aggrField : bucketListInfo.getAggrFieldList()) {
            ParsedComposite terms = response.getAggregations().get(aggrField);
            bucketList.getParsedBuckets().add(terms.getBuckets());
        }
        return bucketList;
    }

```

And when getting the results this way, the pagination doesn't work, it gives the wrong result.  
Can anybody please help me on this issue.

---

<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 4, 2023, 3:25pm UTC](https://discuss.elastic.co/t/pagination-on-composite-aggregation/322667/2 "2023-02-04T15:25:39Z")

</div>

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