# Date\_histogram and day buckets

**URL:** <https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838>\
**Category:** Elasticsearch\
**Created:** [October 1, 2019, 3:55pm UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838 "2019-10-01T15:55:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gturner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gturner/32/55206_2.png) [@gturner](https://discuss.elastic.co/u/gturner)\
**Post date:** [October 1, 2019, 3:55pm UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/1 "2019-10-01T15:55:51Z")

</div>

Hi,

Previously on v5 when using the following query we would only be returned the buckets that are within the range specified. We need the min buckets to be able to graph correctly.

On v7.3 the same query returns a bucket for every day since 1/1/1970. I've struggled to work out how to disregard the buckets outside the range, or even why it is now doing this.

Any help/advice would be much appreciated as the docs aren't offering up any answers.

```
{
   "query" : {
      "bool" : {
         "filter" : [
            {
               "range" : {
                  "StartField" : {
                     "gte" : 1567292400,
                     "lte" : 1569884399,
                     "format" : "epoch_second"
                  }
               }
            }
         ]
      }
   },
   "size" : 0,
   "aggs" : {
      "accountTotals" : {
         "terms" : {
            "size" : 10,
            "field" : "account"
         },
         "aggs" : {
            "periods" : {
               "date_histogram" : {
                  "field" : "StartField",
                  "interval" : "day",
                  "format" : "dd-MM-YYYY",
                  "min_doc_count": 0,
                  "extended_bounds" : {
                     "min" : "01-09-2019",
                     "max" : "30-09-2019"
                  }
               },
               "aggs" : {
                  "Totals" : {
                     "value_count" : {
                        "field" : "messageId"
                     }
                  }
               }
            }
         }
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![gturner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gturner/32/55206_2.png) [@gturner](https://discuss.elastic.co/u/gturner)\
**Post date:** [October 4, 2019, 3:02pm UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/2 "2019-10-04T15:02:41Z")

</div>

Just thought I'd also post a simpler example that shows the same issue, results all the way from 1970, outside of the bounds requested

```
{
   "size" : 0,
    "aggs" : {
        "by_day" : {
            "date_histogram" : {
                "field" : "StartField",
                "format" : "dd-MM-YYYY",
                "interval" : "day",
                  "extended_bounds" : {
                     "min" : "01-09-2019",
                     "max" : "30-09-2019"
                  }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 7, 2019, 11:16am UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/3 "2019-10-07T11:16:39Z")

</div>

You need to provide a `format` with lower case `y`s instead of upper case `Y`s.

Like this:

```auto
{
  "size": 0,
  "aggs": {
    "by_day": {
      "date_histogram": {
        "field": "@timestamp",
        "format": "dd-MM-yyyy",
        "interval": "day",
        "extended_bounds": {
          "min": "01-09-2019",
          "max": "30-09-2019"
        }
      }
    }
  }
}

```

You can find the syntax for custom date formats in [the documentation for DateTimeFormatter](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) .

---

<div class="post-metadata">

**Author:** ![gturner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gturner/32/55206_2.png) [@gturner](https://discuss.elastic.co/u/gturner)\
**Post date:** [October 9, 2019, 8:54am UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/4 "2019-10-09T08:54:46Z")

</div>

So simple and yet so easy to miss. Thank you kindly!

I need to dig deeper though as this worked correctly in V5

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [October 9, 2019, 9:10am UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/5 "2019-10-09T09:10:38Z")

</div>

Elasticsearch switched date libraries from v6 to v7. [Some date formats have changed](https://www.elastic.co/guide/en/elasticsearch/reference/7.0/breaking-changes-7.0.html#_joda_based_date_formatters_are_replaced_with_java_ones) as a result of that. This seems to be one of those changes, that caused your format to work in v5, but not in v7.

---

<div class="post-metadata">

**Author:** ![gturner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gturner/32/55206_2.png) [@gturner](https://discuss.elastic.co/u/gturner)\
**Post date:** [October 9, 2019, 9:45am UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/6 "2019-10-09T09:45:28Z")

</div>

Thank you for the info

---

<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:** [November 6, 2019, 9:45am UTC](https://discuss.elastic.co/t/date-histogram-and-day-buckets/201838/7 "2019-11-06T09:45:47Z")

</div>

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