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

Elasticsearch version is 7
Code :

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 :

{
  "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??

any help ??

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. 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.

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