# Date histogram aggregation returns a wrong number of buckets and wrong start date of each bucket

**URL:** <https://discuss.elastic.co/t/date-histogram-aggregation-returns-a-wrong-number-of-buckets-and-wrong-start-date-of-each-bucket/289324>\
**Category:** Elasticsearch\
**Created:** [November 16, 2021, 12:42pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-returns-a-wrong-number-of-buckets-and-wrong-start-date-of-each-bucket/289324 "2021-11-16T12:42:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Helmi\_Akermi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helmi_akermi/32/77342_2.png) [@Helmi\_Akermi](https://discuss.elastic.co/u/Helmi_Akermi)\
**Post date:** [November 16, 2021, 12:42pm UTC](https://discuss.elastic.co/t/date-histogram-aggregation-returns-a-wrong-number-of-buckets-and-wrong-start-date-of-each-bucket/289324/1 "2021-11-16T12:42:26Z")

</div>

Elasticsearch version is 7  
Code :

```auto
GET /my_alias/_search
{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": 1636416000000,
              "lte": 1636848000000
            }
          }
        },
        {
          "match": {
            "operation": "login"
          }
        }
      ]
    }
  },
  "aggs": {
    "aggregation_result": {
      "date_histogram": {
        "field": "timestamp",
        "interval": "2d",
        "format": "yyyy-MM-dd",
        "time_zone": "Africa/Tunis",
        "min_doc_count": 0,
        "extended_bounds": {
          "min": 1636498800000,
          "max": 1636844399000
        }
      },
      "aggs": {
        "aggregation_result_value": {
          "cardinality": {
            "field": "userId"
          }
        }
      }
    }
  }
}

```

Result :

```auto
{
  "hits": [
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8453085678178432399",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8453085678178432399",
        "userId": "1",
        "operation": "login",
        "timestamp": "1636719695659",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8453254355987689861",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8453254355987689861",
        "userId": "4",
        "operation": "login",
        "timestamp": "1636719691660",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144073151995993313",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144073151995993313",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727021610",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144080449228313206",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144080449228313206",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727021437",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144046325176597637",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144046325176597637",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727022246",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8143967025773352557",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8143967025773352557",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727024126",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144093061207871865",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144093061207871865",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727021138",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144068638678893726",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144068638678893726",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727021717",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144195264640884008",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144195264640884008",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727018715",
        "status": "0",
        "isAnalytics": "true"
      }
    },
    {
      "_index": "analytics_2021-11-12",
      "_type": "_doc",
      "_id": "8144232805315824498",
      "_score": 1.5407403,
      "_source": {
        "duration": "0",
        "spaceId": "0",
        "subModule": "login",
        "module": "portal",
        "errorMessage": null,
        "errorCode": "0",
        "id": "8144232805315824498",
        "userId": "6",
        "operation": "login",
        "timestamp": "1636727017825",
        "status": "0",
        "isAnalytics": "true"
      }
    }
  ],
  "aggregations": {
    "aggregation_result": {
      "buckets": [
        {
          "key_as_string": "2021-11-09",
          "key": 1636412400000,
          "doc_count": 0,
          "aggregation_result_value": {
            "value": 0
          }
        },
        {
          "key_as_string": "2021-11-11",
          "key": 1636585200000,
          "doc_count": 477,
          "aggregation_result_value": {
            "value": 4
          }
        },
        {
          "key_as_string": "2021-11-13",
          "key": 1636758000000,
          "doc_count": 0,
          "aggregation_result_value": {
            "value": 0
          }
        }
      ]
    }
  }
}

```

i'm using histogram\_date aggregation of **2d** interval and a min and max range date of 4days (from 10 November to 13) **4 days**

i receive 3 buckets instead of two and with wrong start date of each one beacuse i supposed to receive two buckets because 4 days / 2 days = 2.  
Is this expected??

---

<div class="post-metadata">

**Author:** ![Helmi\_Akermi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/helmi_akermi/32/77342_2.png) [@Helmi\_Akermi](https://discuss.elastic.co/u/Helmi_Akermi)\
**Post date:** [November 18, 2021, 9:47am UTC](https://discuss.elastic.co/t/date-histogram-aggregation-returns-a-wrong-number-of-buckets-and-wrong-start-date-of-each-bucket/289324/2 "2021-11-18T09:47:08Z")

</div>

any help ??

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 18, 2021, 10:04am UTC](https://discuss.elastic.co/t/date-histogram-aggregation-returns-a-wrong-number-of-buckets-and-wrong-start-date-of-each-bucket/289324/3 "2021-11-18T10:04:06Z")

</div>

I did not check in details, but you should make sure that the dates you are sending to Elasticsearch  
like `1636727017825 ` are UTC.  
If you are not using a proper date format with the timezone, then Elasticsearch considers the date as UTC based.

If it's not exactly the problem here, could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

Please replace the ms since epoch dates by a readable date so we can better understand what is happening.

A full reproduction script is something anyone can **copy and paste in Kibana dev console** , click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<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:** [December 16, 2021, 10:05am UTC](https://discuss.elastic.co/t/date-histogram-aggregation-returns-a-wrong-number-of-buckets-and-wrong-start-date-of-each-bucket/289324/4 "2021-12-16T10:05:04Z")

</div>

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