# Results offset with 2w interval

**URL:** <https://discuss.elastic.co/t/results-offset-with-2w-interval/77246>\
**Category:** Elasticsearch\
**Created:** [March 3, 2017, 2:53am UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246 "2017-03-03T02:53:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [March 3, 2017, 2:53am UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/1 "2017-03-03T02:53:23Z")

</div>

I am finding some strange behavior with a date histogram query with a '2w' interval. I am finding that the start date of the intervals are not corresponding with the range of my data but instead seem to be determined by some other unknown factor. At the moment it seems like perhaps the interval is off by a week but I would need to do some testing on future days to be sure. I can correct this by adding a 7d offset but I am concerned that this might not work all the time.  
I am wondering what determines the start for intervals when using an odd value like '2w'. Is this possibly some sort of defect?

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [March 3, 2017, 12:28pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/2 "2017-03-03T12:28:31Z")

</div>

The reason for this is that `1w` is interpreted specially, namely as [WeekOfWeekyear](http://joda-time.sourceforge.net/field.html#weekOfWeekyear).  
The value `2w` is just interpreted as an interval of 2_7_24_60_60\*1000 milliseconds. The start for numerics intervals is determined by the smallest value found. You can change this using the extended\_bounds mechanism, see also the [discussion here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html#search-aggregations-bucket-histogram-aggregation-extended-bounds).

---

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [March 3, 2017, 2:42pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/3 "2017-03-03T14:42:20Z")

</div>

Thanks for the response. I got hopeful that I could get some different results either by setting the interval to '14d' or by using the extended\_bounds (though I had tried that before). Unfortunately, I can't seem to get the results to start from the beginning of my timeframe. Just to be clear I am searching results over a 4 week timeframe of '2-3-2017' to '3-3-2017' and I get intervals for '1-26-2017', '2-9-2017' and '2-23-2017'. If I choose an interval of '13d' that breaks whatever annoying calculations are happening behind the scenes and I then do get the first interval to start on '2-3-2017'. Please let me know if you think what I might be doing wrong in order to get a two week interval to start on a specific day.

---

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [March 3, 2017, 3:02pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/4 "2017-03-03T15:02:54Z")

</div>

Actually I am now seeing that using some other day interval like '12d' does not really change the situation. It seems like the intervals must be pivoting off of some fixed point but I am not sure what it is. It does not seem to be the beginning of the year since 2017 starts on a Sunday and the 2 week intervals are all on Thursdays.

---

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [March 3, 2017, 3:09pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/5 "2017-03-03T15:09:40Z")

</div>

Oh I bet all intervals are calculated as multiples from the Epoch, right?

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [March 3, 2017, 3:16pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/6 "2017-03-03T15:16:05Z")

</div>

can you post the full query?

---

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [March 3, 2017, 3:59pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/7 "2017-03-03T15:59:27Z")

</div>

Not sure you really want to see the whole query but here it is:  
{  
"size": 0,  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"bool": {  
"must": [  
{  
"bool": {  
"should": [  
{  
"term": {  
"gnip.matching\_rules.tag": "conversation-wind"  
}  
},  
{  
"term": {  
"purpose.campaigns.conversation-wind.match": true  
}  
}  
]  
}  
}  
]  
}  
},  
{  
"terms": {  
"gnip.profileLocations.address.countryCode": [  
"us"  
]  
}  
},  
{  
"range": {  
"postedTime": {  
"gte": "2017-02-03",  
"lt": "2017-03-03",  
"time\_zone": "America/New\_York"  
}  
}  
}  
]  
}  
}  
},  
"aggregations": {  
"national\_trends": {  
"date\_histogram": {  
"field": "postedTime",  
"interval": "2w",  
"min\_doc\_count": 0,  
"time\_zone": "America/New\_York"  
},  
"aggregations": {  
"hashtags": {  
"terms": {  
"field": "twitter\_entities.hashtags.text",  
"size": 0  
}  
},  
"tweets": {  
"terms": {  
"field": "[object.id](http://object.id)",  
"size": 10  
},  
"aggregations": {  
"top\_link": {  
"top\_hits": {  
"\_source": {  
"includes": [  
"[object.id](http://object.id)",  
"object.link",  
"object.body",  
"body"  
]  
},  
"size": 1  
}  
}  
}  
}  
}  
},  
"states": {  
"terms": {  
"field": "gnip.profileLocations.address.region",  
"size": 52  
},  
"aggregations": {  
"intervals": {  
"date\_histogram": {  
"field": "postedTime",  
"min\_doc\_count": 0,  
"interval": "2w",  
"time\_zone": "America/New\_York"  
},  
"aggregations": {  
"hashtags": {  
"terms": {  
"field": "twitter\_entities.hashtags.text",  
"size": 0  
}  
},  
"tweets": {  
"terms": {  
"field": "[object.id](http://object.id)",  
"size": 1  
},  
"aggregations": {  
"top\_link": {  
"top\_hits": {  
"\_source": {  
"includes": [  
"[object.id](http://object.id)",  
"object.link",  
"object.body",  
"body"  
]  
},  
"size": 1  
}  
}  
}  
}  
}  
}  
}  
}  
},  
"sort": [

]  
}

---

<div class="post-metadata">

**Author:** ![jonsgreen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jonsgreen](https://discuss.elastic.co/u/jonsgreen)\
**Post date:** [March 3, 2017, 4:04pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/8 "2017-03-03T16:04:05Z")

</div>

That is without any extended bounds of course. Let me know if you want me to try adding some.

---

<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:** [March 31, 2017, 4:04pm UTC](https://discuss.elastic.co/t/results-offset-with-2w-interval/77246/9 "2017-03-31T16:04:05Z")

</div>

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