# Range is not working as expected, I am using range from 1456866925 to 1456867040. result include data of 1456866920

**URL:** <https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908>\
**Category:** Elasticsearch\
**Created:** [August 12, 2016, 10:13am UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908 "2016-08-12T10:13:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupal](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@rupal](https://discuss.elastic.co/u/rupal)\
**Post date:** [August 12, 2016, 10:13am UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/1 "2016-08-12T10:13:06Z")

</div>

{  
"size": 0,  
"query": {  
"bool": {  
"filter": [  
{ "term": {"type": "my\_type"}},  
{"range": {"my\_field": {"gt": 1456866925, "lte": 1456867040}}},  
{"terms": {"my\_name": ["name"]}},  
{"terms": {"my\_application\_name": ["ap1"]}}  
]  
}  
},  
"aggs": {  
"group\_by\_state": {  
"terms": {  
"script": "[doc['my\_name'].value.replace(':',''),doc['my\_application\_name'].value].join(':')",  
"order": {  
"\_term": "asc"  
}  
},  
"aggs": {  
"volume\_over\_time": {  
"histogram": {  
"field": "my\_field",  
"interval": "10",  
"min\_doc\_count": 0  
},  
"aggs": {  
"chart\_field": {  
"avg": {  
"script": "(doc['temp'].value/(1024\*1024))"  
}  
}  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 12, 2016, 3:53pm UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/2 "2016-08-12T15:53:38Z")

</div>

Can you post the mappings of your index, in particular the mapping for `my_field`?

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 12, 2016, 3:56pm UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/3 "2016-08-12T15:56:40Z")

</div>

That could be because you have interval=10 and it starts at 1456866920 which is just before 1456866925 in the first 10-unit range.

---

<div class="post-metadata">

**Author:** ![rupal](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@rupal](https://discuss.elastic.co/u/rupal)\
**Post date:** [August 22, 2016, 9:05am UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/4 "2016-08-22T09:05:56Z")

</div>

The mapping for my\_field is double here.

---

<div class="post-metadata">

**Author:** ![rupal](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@rupal](https://discuss.elastic.co/u/rupal)\
**Post date:** [August 23, 2016, 4:59am UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/5 "2016-08-23T04:59:35Z")

</div>

But it should not start at 1456866920 which is just before 1456866925. Otherwise there is no point of using filter if it does not give correct output

---

<div class="post-metadata">

**Author:** ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)\
**Post date:** [August 23, 2016, 1:36pm UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/6 "2016-08-23T13:36:45Z")

</div>

The output is correct, it's just that the bucket keys are rounded to fit the interval you've specified.

With an interval of 10, 1456866925 will be "rounded" to 1456866920 to make up the key of the first bucket, then the second bucket will be 1456866930, etc

If you try with an interval of 1 or 5, then the first bucket will be 1456866925  
If you try with an interval of 2, then the first bucket will be 1456866924  
etc

---

<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 5, 2017, 10:25pm UTC](https://discuss.elastic.co/t/range-is-not-working-as-expected-i-am-using-range-from-1456866925-to-1456867040-result-include-data-of-1456866920/57908/7 "2017-07-05T22:25:54Z")

</div>


