# Unexpected behaviour of date histogram aggregation

**URL:** <https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111>\
**Category:** Elasticsearch\
**Created:** [May 17, 2017, 1:27pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111 "2017-05-17T13:27:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferran.munoz](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@ferran.munoz](https://discuss.elastic.co/u/ferran.munoz)\
**Post date:** [May 17, 2017, 1:27pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111/1 "2017-05-17T13:27:17Z")

</div>

Hello,

I am experiencing an unexpected behaviour (imho) of date histogram aggregation when both offset and extended\_bounds options are used.

Query using both offset and extended\_bounds that returns unexpected results:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": "2017-04-04T02:30:00",
              "lte": "2017-04-05T02:29:59",
              "time_zone": "Europe/Berlin"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "time_aggregation": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "day",
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2017-04-04T02:30:00",
          "max": "2017-04-05T02:29:59"
        },
        "offset": "+150m",
        "time_zone": "Europe/Berlin"
      },
      "aggs": {}
    }
  }
}

```

Reponse:

```auto
{
  "took": 39,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 77906,
    "max_score": 0,
    "hits": [

    ]
  },
  "aggregations": {
    "time_aggregation": {
      "buckets": [
        {
          "key_as_string": "2017-04-04T00:00:00.000+02:00",
          "key": 1491256800000,
          "doc_count": 0
        },
        {
          "key_as_string": "2017-04-04T02:30:00.000+02:00",
          "key": 1491265800000,
          "doc_count": 77906
        },
        {
          "key_as_string": "2017-04-05T00:00:00.000+02:00",
          "key": 1491343200000,
          "doc_count": 0
        }
      ]
    }
  }
}

```

It is unexpected because I expect only one bucket (the one with key "2017-04-04T02:30:00.000+02:00").

If I use only offset without extended\_bounds:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": "2017-04-04T02:30:00",
              "lte": "2017-04-05T02:29:59",
              "time_zone": "Europe/Berlin"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "time_aggregation": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "day",
        "min_doc_count": 0,
        "offset": "+150m",
        "time_zone": "Europe/Berlin"
      },
      "aggs": {}
    }
  }
}

```

I get the expected response:

```auto
{
  "took": 41,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 77906,
    "max_score": 0,
    "hits": [

    ]
  },
  "aggregations": {
    "time_aggregation": {
      "buckets": [
        {
          "key_as_string": "2017-04-04T02:30:00.000+02:00",
          "key": 1491265800000,
          "doc_count": 77906
        }
      ]
    }
  }
}

```

And if I use only extended\_bounds without offset:

```auto
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": "2017-04-04T00:00:00",
              "lte": "2017-04-04T23:59:59",
              "time_zone": "Europe/Berlin"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "time_aggregation": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "day",
        "min_doc_count": 0,
        "extended_bounds": {
          "min": "2017-04-04T00:00:00",
          "max": "2017-04-04T23:59:59"
        },
        "time_zone": "Europe/Berlin"
      },
      "aggs": {}
    }
  }
}

```

I get the expected response:

```auto
{
  "took": 432,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 68107,
    "max_score": 0,
    "hits": [

    ]
  },
  "aggregations": {
    "time_aggregation": {
      "buckets": [
        {
          "key_as_string": "2017-04-04T00:00:00.000+02:00",
          "key": 1491256800000,
          "doc_count": 68107
        }
      ]
    }
  }
}

```

I need to use both options because I want empty buckets if there are not documents in the interval.  
I am doing anything wrong?

Thanks!

---

<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:** [May 17, 2017, 2:05pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111/2 "2017-05-17T14:05:08Z")

</div>

I think you may be running into this bug: [https://github.com/elastic/elasticsearch/issues/23776](https://github.com/elastic/elasticsearch/issues/23776) which should be fixed with: [https://github.com/elastic/elasticsearch/pull/23789](https://github.com/elastic/elasticsearch/pull/23789)

---

<div class="post-metadata">

**Author:** ![ferran.munoz](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@ferran.munoz](https://discuss.elastic.co/u/ferran.munoz)\
**Post date:** [May 17, 2017, 4:09pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111/3 "2017-05-17T16:09:50Z")

</div>

You are right. I will wait for version 5.4.2 to be released. In the meanwhile, I will filter the response to drop the unexpected time buckets.

Thanks!

---

<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:** [May 17, 2017, 4:20pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111/4 "2017-05-17T16:20:58Z")

</div>

No problem, happy to help! Sorry that you ran into this particular bug ☹

---

<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:** [June 14, 2017, 4:21pm UTC](https://discuss.elastic.co/t/unexpected-behaviour-of-date-histogram-aggregation/86111/5 "2017-06-14T16:21:25Z")

</div>

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