# Composite aggregation returns after\_key even when there are no more buckets

**URL:** <https://discuss.elastic.co/t/composite-aggregation-returns-after-key-even-when-there-are-no-more-buckets/338606>\
**Category:** Elasticsearch\
**Created:** [July 17, 2023, 10:00pm UTC](https://discuss.elastic.co/t/composite-aggregation-returns-after-key-even-when-there-are-no-more-buckets/338606 "2023-07-17T22:00:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdhowie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cdhowie/32/123558_2.png) [@cdhowie](https://discuss.elastic.co/u/cdhowie)\
**Post date:** [July 17, 2023, 10:00pm UTC](https://discuss.elastic.co/t/composite-aggregation-returns-after-key-even-when-there-are-no-more-buckets/338606/1 "2023-07-17T22:00:35Z")

</div>

I've been working on a query that performs a composite aggregation with a large number of buckets. When I set the aggregation size to 50,000, all buckets fit in the response. However, `after_key` is still present in the response.

Sending a second query with `after` set to this value takes about the same amount of time as the first query (10 seconds or so) but returns no buckets and no `after_key`, which wastes time and resources doing nothing.

So I have two questions about this:

1. Is this intended behavior, or is this a bug? I would expect `after_key` to not be present if there are no more buckets, but the composite aggregation documentation doesn't specify the circumstances under which `after_key` is present.
2. If the number of buckets is less than the aggregation `size`, is it guaranteed that there are no more buckets? If it is guaranteed then I can simply end pagination in that case.

I am using Elasticsearch 8.8.2.

---

<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:** [August 14, 2023, 10:01pm UTC](https://discuss.elastic.co/t/composite-aggregation-returns-after-key-even-when-there-are-no-more-buckets/338606/2 "2023-08-14T22:01:23Z")

</div>

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