# Composite aggregation after\_key

**URL:** <https://discuss.elastic.co/t/composite-aggregation-after-key/245789>\
**Category:** Elasticsearch\
**Created:** [August 20, 2020, 3:14pm UTC](https://discuss.elastic.co/t/composite-aggregation-after-key/245789 "2020-08-20T15:14:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_Pofuk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robert_pofuk/32/74247_2.png) [@Robert\_Pofuk](https://discuss.elastic.co/u/Robert_Pofuk)\
**Post date:** [August 20, 2020, 3:14pm UTC](https://discuss.elastic.co/t/composite-aggregation-after-key/245789/1 "2020-08-20T15:14:45Z")

</div>

Hi all!

I have question. If I use histogram composite aggregation with setting size.  
For example I have integer range. Each number bucket in aggregation represents 1 day (Using integer for date is not important).

If I use `size` and say I want to get 10 buckets in result. Can I reliably use `after_key` to select date of my agregation?

Because if I filter by specific date range I will also get buckets outside of filtered range because some documents will be there since histogram is created on interval field (Same is true for date histogram on `data_range` field type).

I my idea is to use combination of `after_key` to set real start date and `size` to pick how many days of result I want.

What worries me is from [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html):

> The `after_key` is **usually** the key to the last bucket returned in the response, but that isn’t guaranteed. Always use the returned `after_key` instead of derriving it from the buckets.

Will I miss some buckets if is say:  
`after_key: 10 size: 5`  
`after_key: 15 size: 5`

Or maybe better question will be, what are the cases when last returned bucket does not match next after\_key?

This would be usefull also for Kibana to display time correctly, but also some buckets may represent 30 years of range and only 1 month is usually queried. So if I can paginate properly I can save a lot.

---

<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:** [September 17, 2020, 3:14pm UTC](https://discuss.elastic.co/t/composite-aggregation-after-key/245789/2 "2020-09-17T15:14:57Z")

</div>

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