# Date Histogram Aggregation with predefined number of buckets possible?

**URL:** https://discuss.elastic.co/t/date-histogram-aggregation-with-predefined-number-of-buckets-possible/281880
**Category:** Elasticsearch
**Created:** [August 18, 2021, 9:48pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-predefined-number-of-buckets-possible/281880 "2021-08-18T21:48:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![johnrodey](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@johnrodey](https://discuss.elastic.co/u/johnrodey)
#### Post date: [August 18, 2021, 9:48pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-predefined-number-of-buckets-possible/281880/1 "2021-08-18T21:48:00Z")

</div>

I would like to get an aggregation to build a histogram off of a date field, but I would like to predefine the **EXACT** number of buckets to use. So for example if I have data indexed for June 1st and I ask for 10 buckets I would get something like:  
2021-06-01\_00-00-00-000 : 188,  
2021-06-01\_02-24-00-000 : 253,  
2021-06-01\_04-48-00-000 : 77,  
2021-06-01\_07-12-00-000 : 123,  
2021-06-01\_09-36-00-000 : 98,  
2021-06-01\_12-00-00-000 : 87,  
2021-06-01\_14-24-00-000 : 122,  
2021-06-01\_16-48-00-000 : 116,  
2021-06-01\_19-12-00-000 : 142,  
2021-06-01\_21-36-00-000 : 121

I know that the Auto Date Histogram is close to this however it doesn't guarantee exactly 10 buckets, it just tries to get as close as it can without going over. I know my example above is simple and most often my bucket times will be much less round of numbers than above.

Worst case I think I can get the first query result and last query result, then build a custom range aggregation, would be nice if there was something in the API to do this, so figured I would throw it out there for feedback.

Thanks, Any help is greatly appreciated!

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [August 25, 2021, 10:03am UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-predefined-number-of-buckets-possible/281880/2 "2021-08-25T10:03:22Z")

</div>

How about the [auto interval date histogram aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-aggregations-bucket-autodatehistogram-aggregation.html)?

---

<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 22, 2021, 10:03am UTC](https://discuss.elastic.co/t/date-histogram-aggregation-with-predefined-number-of-buckets-possible/281880/3 "2021-09-22T10:03:58Z")

</div>

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