# How does the date\_histogram aggregation choose its buckets? Is this tunable?

**URL:** https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290
**Category:** Elasticsearch
**Created:** [October 16, 2014, 4:03pm UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290 "2014-10-16T16:03:44Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Michael\_Herold](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_herold/32/1176_2.png) [@Michael\_Herold](https://discuss.elastic.co/u/Michael_Herold)
#### Post date: [October 16, 2014, 4:03pm UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290/1 "2014-10-16T16:03:44Z")

</div>

I'm trying to use elasticsearch to give me 30-day statistics for a given  
collection of models (pertinent fields are a date in _created\_at_ and an  
integer in _value_). Currently, I have this query/aggregation:

{  
"query": {  
"match\_all": {}  
},  
"aggregations": {  
"date\_histogram": {  
"field": "created\_at",  
"interval": "30d",  
"min\_doc\_count": 0,  
"extended\_bounds": {  
"min": 1381881600000, // Dynamically generated for 365 days ago  
(This is 2013-10-16 00:00:00 +0000)  
"max": 1413503999000 // Dynamically generated for end of today  
(This is 2014-10-16 23:59:59 +0000)  
}  
},  
"aggregations": {  
"stats": {  
"extended\_stats": {  
"field": "value"  
}  
}  
}  
}  
}

It's working as expected, except for one thing: the buckets don't line up  
as expected. For some reason, _the last bucket always starts on 2014-10-07  
00:00:00 +0000, regardless of what data is in elasticsearch_. I have tried  
this aggregation on a bunch of different date ranges, including:

- 1 model instance per day for the past 30 days
- 1 model instance per day for the past 365 days
- 1 model instance total, for a _created\_at_ of 2014-09-30
- 1 model instance total, for a _created\_at_ of 2014-10-15
- 1 model instance total, for a _created\_at_ of 2014-10-16
- 1 model instance total, for a _created\_at_ of 2014-10-31

I have also tried to adjust the _extended bounds_, which doesn't shift the  
bucket dates at all.

The result is that the last bucket is always giving a date of 2014-10-07.  
This throws off the statistics because the last bucket isn't a full 30 days  
of material, whereas the rest of buckets are.

_My questions:_

\*- Why are the buckets always pivoting around October 7th? \*My expectation  
is that it pivots around 30 days prior to _extend\_bounds["max"]_.  
_- Is there a way to tune this?_

Thank you in advance for any help you can give.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [October 16, 2014, 11:56pm UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290/2 "2014-10-16T23:56:32Z")

</div>

Hi Michael,

Histogram aggregations return buckets that are a multiple of the interval,  
you are getting this weird offset because not all months have exactly 30  
days. Setting "interval" to "month" should fix the issue?

On Thu, Oct 16, 2014 at 6:03 PM, Michael Herold [michael.j.herold@gmail.com](mailto:michael.j.herold@gmail.com)  
wrote:

> I'm trying to use elasticsearch to give me 30-day statistics for a given  
> collection of models (pertinent fields are a date in _created\_at_ and an  
> integer in _value_). Currently, I have this query/aggregation:
> 
> {  
> "query": {  
> "match\_all": {}  
> },  
> "aggregations": {  
> "date\_histogram": {  
> "field": "created\_at",  
> "interval": "30d",  
> "min\_doc\_count": 0,  
> "extended\_bounds": {  
> "min": 1381881600000, // Dynamically generated for 365 days ago  
> (This is 2013-10-16 00:00:00 +0000)  
> "max": 1413503999000 // Dynamically generated for end of today  
> (This is 2014-10-16 23:59:59 +0000)  
> }  
> },  
> "aggregations": {  
> "stats": {  
> "extended\_stats": {  
> "field": "value"  
> }  
> }  
> }  
> }  
> }
> 
> It's working as expected, except for one thing: the buckets don't line up  
> as expected. For some reason, _the last bucket always starts on  
> 2014-10-07 00:00:00 +0000, regardless of what data is in elasticsearch_.  
> I have tried this aggregation on a bunch of different date ranges,  
> including:
> 
> - 1 model instance per day for the past 30 days
> - 1 model instance per day for the past 365 days
> - 1 model instance total, for a _created\_at_ of 2014-09-30
> - 1 model instance total, for a _created\_at_ of 2014-10-15
> - 1 model instance total, for a _created\_at_ of 2014-10-16
> - 1 model instance total, for a _created\_at_ of 2014-10-31
> 
> I have also tried to adjust the _extended bounds_, which doesn't shift  
> the bucket dates at all.
> 
> The result is that the last bucket is always giving a date of 2014-10-07.  
> This throws off the statistics because the last bucket isn't a full 30 days  
> of material, whereas the rest of buckets are.
> 
> _My questions:_
> 
> \*- Why are the buckets always pivoting around October 7th? \*My  
> expectation is that it pivots around 30 days prior to  
> _extend\_bounds["max"]_.  
> _- Is there a way to tune this?_
> 
> Thank you in advance for any help you can give.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5STokXP\_R\_DMKbFeijxC-vsc6Y2-AOrZGHbon7AwLqFg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j5STokXP_R_DMKbFeijxC-vsc6Y2-AOrZGHbon7AwLqFg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Michael\_Herold](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_herold/32/1176_2.png) [@Michael\_Herold](https://discuss.elastic.co/u/Michael_Herold)
#### Post date: [October 17, 2014, 12:20am UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290/3 "2014-10-17T00:20:21Z")

</div>

Hi Adrien,

Thank you for the reply. I actually want 30 day buckets, not one month  
buckets, for the calculation I'm doing. I would understand the weird offset  
if I was using months as a unit since they are of variable length. However,  
a day is always 1000 \* 60 \* 60 \* 24 milliseconds, so why would that cause  
an offset that is the 7th of the month?

Thank you,  
Michael

On Thursday, October 16, 2014 6:56:39 PM UTC-5, Adrien Grand wrote:

> Hi Michael,
> 
> Histogram aggregations return buckets that are a multiple of the interval,  
> you are getting this weird offset because not all months have exactly 30  
> days. Setting "interval" to "month" should fix the issue?
> 
> On Thu, Oct 16, 2014 at 6:03 PM, Michael Herold \<[michael....@gmail.com](mailto:michael....@gmail.com)  
> \<javascript:\>\> wrote:
> 
> > I'm trying to use elasticsearch to give me 30-day statistics for a given  
> > collection of models (pertinent fields are a date in _created\_at_ and an  
> > integer in _value_). Currently, I have this query/aggregation:
> > 
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "aggregations": {  
> > "date\_histogram": {  
> > "field": "created\_at",  
> > "interval": "30d",  
> > "min\_doc\_count": 0,  
> > "extended\_bounds": {  
> > "min": 1381881600000, // Dynamically generated for 365 days ago  
> > (This is 2013-10-16 00:00:00 +0000)  
> > "max": 1413503999000 // Dynamically generated for end of today  
> > (This is 2014-10-16 23:59:59 +0000)  
> > }  
> > },  
> > "aggregations": {  
> > "stats": {  
> > "extended\_stats": {  
> > "field": "value"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > It's working as expected, except for one thing: the buckets don't line up  
> > as expected. For some reason, _the last bucket always starts on  
> > 2014-10-07 00:00:00 +0000, regardless of what data is in elasticsearch_.  
> > I have tried this aggregation on a bunch of different date ranges,  
> > including:
> > 
> > - 1 model instance per day for the past 30 days
> > - 1 model instance per day for the past 365 days
> > - 1 model instance total, for a _created\_at_ of 2014-09-30
> > - 1 model instance total, for a _created\_at_ of 2014-10-15
> > - 1 model instance total, for a _created\_at_ of 2014-10-16
> > - 1 model instance total, for a _created\_at_ of 2014-10-31
> > 
> > I have also tried to adjust the _extended bounds_, which doesn't shift  
> > the bucket dates at all.
> > 
> > The result is that the last bucket is always giving a date of 2014-10-07.  
> > This throws off the statistics because the last bucket isn't a full 30 days  
> > of material, whereas the rest of buckets are.
> > 
> > _My questions:_
> > 
> > \*- Why are the buckets always pivoting around October 7th? \*My  
> > expectation is that it pivots around 30 days prior to  
> > _extend\_bounds["max"]_.  
> > _- Is there a way to tune this?_
> > 
> > Thank you in advance for any help you can give.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [October 19, 2014, 9:44pm UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290/4 "2014-10-19T21:44:13Z")

</div>

Hi Michael,

The thing is that buckets are not computed based on the current date and  
going backwards, but based on January 1st 1970 (called Epoch) which is a  
common origin of time for computers. So the first bucket would start on  
January 1st 1970, then the second on January 31st, ... and if you keep on  
doing it until October 2014, the bucket would start on the 7th (I think?).

I believe you could make it work the way that you expect by using the  
pre\_offset and post\_offset options of the date histogram aggregation:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Fri, Oct 17, 2014 at 2:20 AM, Michael Herold [michael.j.herold@gmail.com](mailto:michael.j.herold@gmail.com)  
wrote:

> Hi Adrien,
> 
> Thank you for the reply. I actually want 30 day buckets, not one month  
> buckets, for the calculation I'm doing. I would understand the weird offset  
> if I was using months as a unit since they are of variable length. However,  
> a day is always 1000 \* 60 \* 60 \* 24 milliseconds, so why would that cause  
> an offset that is the 7th of the month?
> 
> Thank you,  
> Michael
> 
> On Thursday, October 16, 2014 6:56:39 PM UTC-5, Adrien Grand wrote:
> 
> > Hi Michael,
> > 
> > Histogram aggregations return buckets that are a multiple of the  
> > interval, you are getting this weird offset because not all months have  
> > exactly 30 days. Setting "interval" to "month" should fix the issue?
> > 
> > On Thu, Oct 16, 2014 at 6:03 PM, Michael Herold [michael....@gmail.com](mailto:michael....@gmail.com)  
> > wrote:
> > 
> > > I'm trying to use elasticsearch to give me 30-day statistics for a given  
> > > collection of models (pertinent fields are a date in _created\_at_ and  
> > > an integer in _value_). Currently, I have this query/aggregation:
> > > 
> > > {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "aggregations": {  
> > > "date\_histogram": {  
> > > "field": "created\_at",  
> > > "interval": "30d",  
> > > "min\_doc\_count": 0,  
> > > "extended\_bounds": {  
> > > "min": 1381881600000, // Dynamically generated for 365 days  
> > > ago (This is 2013-10-16 00:00:00 +0000)  
> > > "max": 1413503999000 // Dynamically generated for end of  
> > > today (This is 2014-10-16 23:59:59 +0000)  
> > > }  
> > > },  
> > > "aggregations": {  
> > > "stats": {  
> > > "extended\_stats": {  
> > > "field": "value"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > It's working as expected, except for one thing: the buckets don't line  
> > > up as expected. For some reason, _the last bucket always starts on  
> > > 2014-10-07 00:00:00 +0000, regardless of what data is in elasticsearch_.  
> > > I have tried this aggregation on a bunch of different date ranges,  
> > > including:
> > > 
> > > - 1 model instance per day for the past 30 days
> > > - 1 model instance per day for the past 365 days
> > > - 1 model instance total, for a _created\_at_ of 2014-09-30
> > > - 1 model instance total, for a _created\_at_ of 2014-10-15
> > > - 1 model instance total, for a _created\_at_ of 2014-10-16
> > > - 1 model instance total, for a _created\_at_ of 2014-10-31
> > > 
> > > I have also tried to adjust the _extended bounds_, which doesn't shift  
> > > the bucket dates at all.
> > > 
> > > The result is that the last bucket is always giving a date of  
> > > 2014-10-07. This throws off the statistics because the last bucket isn't a  
> > > full 30 days of material, whereas the rest of buckets are.
> > > 
> > > _My questions:_
> > > 
> > > \*- Why are the buckets always pivoting around October 7th? \*My  
> > > expectation is that it pivots around 30 days prior to  
> > > _extend\_bounds["max"]_.  
> > > _- Is there a way to tune this?_
> > > 
> > > Thank you in advance for any help you can give.
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%  
> > > [40googlegroups.com](http://40googlegroups.com)  
> > > [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > .  
> > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > 
> > --  
> > Adrien Grand
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j64XOCocGbJj\_mN8vanhcdZok7bSAxK0wE1ZqHhL\_96Fw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j64XOCocGbJj_mN8vanhcdZok7bSAxK0wE1ZqHhL_96Fw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Michael\_Herold](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michael_herold/32/1176_2.png) [@Michael\_Herold](https://discuss.elastic.co/u/Michael_Herold)
#### Post date: [October 20, 2014, 2:35pm UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290/5 "2014-10-20T14:35:45Z")

</div>

Hi Adrien,

Thanks! The fact that the buckets start calculating from the UNIX epoch is  
what I didn't understand. The fact that it always landed on October 7th --  
which seems like an arbitrary date -- confused me. I did some quick  
calculations and you're right; midnight on October 7th, 2014, is 545  
30-day-buckets from the UNIX epoch. Huzzah!

I think you're right about the pre\_offset and post\_offset. I should be able  
to calculate the needed offset(s) to get the effect that I want.

Thank you for taking the time to explain this to me. I appreciate it!

Best,  
Michael

On Sun, Oct 19, 2014 at 4:44 PM, Adrien Grand \<  
[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:

> Hi Michael,
> 
> The thing is that buckets are not computed based on the current date and  
> going backwards, but based on January 1st 1970 (called Epoch) which is a  
> common origin of time for computers. So the first bucket would start on  
> January 1st 1970, then the second on January 31st, ... and if you keep on  
> doing it until October 2014, the bucket would start on the 7th (I think?).
> 
> I believe you could make it work the way that you expect by using the  
> pre\_offset and post\_offset options of the date histogram aggregation:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#_pre_post_offset)
> 
> On Fri, Oct 17, 2014 at 2:20 AM, Michael Herold \<  
> [michael.j.herold@gmail.com](mailto:michael.j.herold@gmail.com)\> wrote:
> 
> > Hi Adrien,
> > 
> > Thank you for the reply. I actually want 30 day buckets, not one month  
> > buckets, for the calculation I'm doing. I would understand the weird offset  
> > if I was using months as a unit since they are of variable length. However,  
> > a day is always 1000 \* 60 \* 60 \* 24 milliseconds, so why would that cause  
> > an offset that is the 7th of the month?
> > 
> > Thank you,  
> > Michael
> > 
> > On Thursday, October 16, 2014 6:56:39 PM UTC-5, Adrien Grand wrote:
> > 
> > > Hi Michael,
> > > 
> > > Histogram aggregations return buckets that are a multiple of the  
> > > interval, you are getting this weird offset because not all months have  
> > > exactly 30 days. Setting "interval" to "month" should fix the issue?
> > > 
> > > On Thu, Oct 16, 2014 at 6:03 PM, Michael Herold [michael....@gmail.com](mailto:michael....@gmail.com)  
> > > wrote:
> > > 
> > > > I'm trying to use elasticsearch to give me 30-day statistics for a  
> > > > given collection of models (pertinent fields are a date in _created\_at_  
> > > > and an integer in _value_). Currently, I have this query/aggregation:
> > > > 
> > > > {  
> > > > "query": {  
> > > > "match\_all": {}  
> > > > },  
> > > > "aggregations": {  
> > > > "date\_histogram": {  
> > > > "field": "created\_at",  
> > > > "interval": "30d",  
> > > > "min\_doc\_count": 0,  
> > > > "extended\_bounds": {  
> > > > "min": 1381881600000, // Dynamically generated for 365 days  
> > > > ago (This is 2013-10-16 00:00:00 +0000)  
> > > > "max": 1413503999000 // Dynamically generated for end of  
> > > > today (This is 2014-10-16 23:59:59 +0000)  
> > > > }  
> > > > },  
> > > > "aggregations": {  
> > > > "stats": {  
> > > > "extended\_stats": {  
> > > > "field": "value"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > It's working as expected, except for one thing: the buckets don't line  
> > > > up as expected. For some reason, _the last bucket always starts on  
> > > > 2014-10-07 00:00:00 +0000, regardless of what data is in elasticsearch_.  
> > > > I have tried this aggregation on a bunch of different date ranges,  
> > > > including:
> > > > 
> > > > - 1 model instance per day for the past 30 days
> > > > - 1 model instance per day for the past 365 days
> > > > - 1 model instance total, for a _created\_at_ of 2014-09-30
> > > > - 1 model instance total, for a _created\_at_ of 2014-10-15
> > > > - 1 model instance total, for a _created\_at_ of 2014-10-16
> > > > - 1 model instance total, for a _created\_at_ of 2014-10-31
> > > > 
> > > > I have also tried to adjust the _extended bounds_, which doesn't shift  
> > > > the bucket dates at all.
> > > > 
> > > > The result is that the last bucket is always giving a date of  
> > > > 2014-10-07. This throws off the statistics because the last bucket isn't a  
> > > > full 30 days of material, whereas the rest of buckets are.
> > > > 
> > > > _My questions:_
> > > > 
> > > > \*- Why are the buckets always pivoting around October 7th? \*My  
> > > > expectation is that it pivots around 30 days prior to  
> > > > _extend\_bounds["max"]_.  
> > > > _- Is there a way to tune this?_
> > > > 
> > > > Thank you in advance for any help you can give.
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > > To view this discussion on the web visit [https://groups.google.com/d/](https://groups.google.com/d/)  
> > > > msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%  
> > > > [40googlegroups.com](http://40googlegroups.com)  
> > > > [https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/87fe5659-50c5-4870-8139-12a680b94c9e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> > > 
> > > --  
> > > Adrien Grand
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/621c4eb3-ebb0-4447-bb5f-1741a10dfb71%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .
> > 
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/gz\_M\_cy4g\_Q/unsubscribe](https://groups.google.com/d/topic/elasticsearch/gz_M_cy4g_Q/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j64XOCocGbJj\_mN8vanhcdZok7bSAxK0wE1ZqHhL\_96Fw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j64XOCocGbJj_mN8vanhcdZok7bSAxK0wE1ZqHhL_96Fw%40mail.gmail.com)  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j64XOCocGbJj\_mN8vanhcdZok7bSAxK0wE1ZqHhL\_96Fw%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j64XOCocGbJj_mN8vanhcdZok7bSAxK0wE1ZqHhL_96Fw%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CACEqm-FCCL07FrZdEr3Asypoz\_PJCbyNm2F\_PcbKp2b8RSbOSA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CACEqm-FCCL07FrZdEr3Asypoz_PJCbyNm2F_PcbKp2b8RSbOSA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [July 6, 2017, 12:54am UTC](https://discuss.elastic.co/t/how-does-the-date-histogram-aggregation-choose-its-buckets-is-this-tunable/20290/6 "2017-07-06T00:54:59Z")

</div>


