# Date Histogram empty buckets and number of buckets

**URL:** <https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972>\
**Category:** Elasticsearch\
**Created:** [September 9, 2015, 10:41pm UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972 "2015-09-09T22:41:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilya\_Anisimov](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@Ilya\_Anisimov](https://discuss.elastic.co/u/Ilya_Anisimov)\
**Post date:** [September 9, 2015, 10:41pm UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972/1 "2015-09-09T22:41:32Z")

</div>

Hi Guys,

I'm trying to use Elasticsearch 1.4.4 and Aggregation framework to generate data for graphs. I have a strange problem: when I specify interval "day" (query and extended\_bounds set to Aug 3rd 2015 to Sep 14th 2015) it generates empty buckets but the first bucket is Sep 1st 2015. Why is that? Can you help me to understand and find a workaround to generate empty buckets for the rest of the date range?

Please find example Request/Response and Mapping below

Thanks a lot!  
Ilya

**Request:**  
{  
"size" : 0,  
"query" : {  
"filtered" : {  
"query" : {  
"match" : {  
"application" : {  
"query" : "MGU",  
"type" : "boolean"  
}  
}  
},  
"filter" : {  
"range" : {  
"timestamp" : {  
"from" : 1438585200000,  
"to" : 1442214000000,  
"include\_lower" : true,  
"include\_upper" : true  
}  
}  
}  
}  
},  
"aggregations" : {  
"by\_interval" : {  
"date\_histogram" : {  
"field" : "timestamp",  
"interval" : "1d",  
"min\_doc\_count" : 0,  
"order" : {  
"\_key" : "asc"  
},  
"extended\_bounds" : {  
"min" : 1438585200000,  
"max" : 1442214000000  
}  
}  
}  
}  
}

**Response:**  
{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 10228,  
"max\_score" : 0.0,  
"hits" : []  
},  
"aggregations" : {  
"by\_interval" : {  
"buckets" : [ {  
"key\_as\_string" : "2015-09-01T00:00:00.000Z",  
"key" : 1441065600000,  
"doc\_count" : 388  
}, {  
"key\_as\_string" : "2015-09-02T00:00:00.000Z",  
"key" : 1441152000000,  
"doc\_count" : 105  
}, {  
"key\_as\_string" : "2015-09-03T00:00:00.000Z",  
"key" : 1441238400000,  
"doc\_count" : 1766  
}, {  
"key\_as\_string" : "2015-09-04T00:00:00.000Z",  
"key" : 1441324800000,  
"doc\_count" : 7653  
}, {  
"key\_as\_string" : "2015-09-05T00:00:00.000Z",  
"key" : 1441411200000,  
"doc\_count" : 0  
}, {  
"key\_as\_string" : "2015-09-06T00:00:00.000Z",  
"key" : 1441497600000,  
"doc\_count" : 0  
}, {  
"key\_as\_string" : "2015-09-07T00:00:00.000Z",  
"key" : 1441584000000,  
"doc\_count" : 0  
}, {  
"key\_as\_string" : "2015-09-08T00:00:00.000Z",  
"key" : 1441670400000,  
"doc\_count" : 9  
}, {  
"key\_as\_string" : "2015-09-09T00:00:00.000Z",  
"key" : 1441756800000,  
"doc\_count" : 307  
} ]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Ilya\_Anisimov](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@Ilya\_Anisimov](https://discuss.elastic.co/u/Ilya_Anisimov)\
**Post date:** [September 9, 2015, 10:42pm UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972/2 "2015-09-09T22:42:21Z")

</div>

**Mapping**  
{  
"graylog2\_0" : {  
"aliases" : {  
"graylog2\_deflector" : { }  
},  
"mappings" : {  
"message" : {  
"dynamic\_templates" : [ {  
"store\_generic" : {  
"mapping" : {  
"index" : "not\_analyzed"  
},  
"match" : "\*"  
}  
} ],  
"\_source" : {  
"compress" : true  
},  
"properties" : {  
"api" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"appVersion" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"application" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"data" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"dbname" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"deviceModel" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"environment" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"errorMessage" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"full\_message" : {  
"type" : "string",  
"analyzer" : "standard"  
},  
"gl2\_remote\_ip" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"gl2\_remote\_port" : {  
"type" : "long"  
},  
"gl2\_source\_input" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"gl2\_source\_node" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"level" : {  
"type" : "long"  
},  
"message" : {  
"type" : "string",  
"analyzer" : "standard"  
},  
"osName" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"osVersion" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"responseCode" : {  
"type" : "long"  
},  
"size" : {  
"type" : "long",  
"store" : true  
},  
"source" : {  
"type" : "string",  
"analyzer" : "analyzer\_keyword"  
},  
"sourceCountry" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"sourceIp" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"table" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"timestamp" : {  
"type" : "date",  
"format" : "yyyy-MM-dd HH:mm:ss.SSS"  
},  
"userid" : {  
"type" : "string",  
"index" : "not\_analyzed"  
},  
"version" : {  
"type" : "string",  
"index" : "not\_analyzed"  
}  
}  
}  
},  
"settings" : {  
"index" : {  
"uuid" : "vp\_op6-CSoumPI8jEWTpjQ",  
"analysis" : {  
"analyzer" : {  
"analyzer\_keyword" : {  
"filter" : "lowercase",  
"tokenizer" : "keyword"  
}  
}  
},  
"number\_of\_replicas" : "0",  
"number\_of\_shards" : "4",  
"version" : {  
"created" : "901399"  
}  
}  
},  
"warmers" : { }  
}  
}

---

<div class="post-metadata">

**Author:** ![remram](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remram/32/4077_2.png) [@remram](https://discuss.elastic.co/u/remram)\
**Post date:** [September 10, 2015, 7:06am UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972/3 "2015-09-10T07:06:55Z")

</div>

Hi,  
I've never used the date histogram aggregation but i have some question to your use case:

Why are you not using the same format like your mapping during your query?  
Please take a look at:

1. [Date range aggregation | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html#date-format-pattern)
2. [DateTimeFormat (Joda-Time 2.12.5 API)](http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html)

> [@Ilya\_Anisimov](#):
>
> "timestamp" : { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss.SSS" },

> [@Ilya\_Anisimov](#):
>
> "min" : 1438585200000, "max" : 1442214000000

Why are you using order here? does it make sense for you? I think the date\_histogram will return you the data in proper order!

> [@Ilya\_Anisimov](#):
>
> "order" : { "\_key" : "asc" },

---

<div class="post-metadata">

**Author:** ![Ilya\_Anisimov](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@Ilya\_Anisimov](https://discuss.elastic.co/u/Ilya_Anisimov)\
**Post date:** [September 10, 2015, 4:25pm UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972/4 "2015-09-10T16:25:09Z")

</div>

Thank you for your reply remram. I'm using UNIX epoch milliseconds because it works well and easy on the front end (no string formatting etc) Anyways using different date-time format doesn't solve my particular problem (just tested) 🙂 Do you have other suggestions?

I'm especially looking for help from Elasticsearch development team 🙂

---

<div class="post-metadata">

**Author:** ![Ilya\_Anisimov](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@Ilya\_Anisimov](https://discuss.elastic.co/u/Ilya_Anisimov)\
**Post date:** [September 14, 2015, 5:37pm UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972/5 "2015-09-14T17:37:39Z")

</div>

Any ideas?

---

<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, 11:50pm UTC](https://discuss.elastic.co/t/date-histogram-empty-buckets-and-number-of-buckets/28972/6 "2017-07-05T23:50:26Z")

</div>


