Inconsistent date_histogram "key_as_string" when "format" is set

Hello,

I have an issue with date_histogram on Elastic 7.9.2. The "key_as_string" does not seems to be correct.

I have this index:

    PUT https://localhost:9200/test -d '
    {
       "mappings":{
          "properties":{
             "date":{
                "type":"date",
                "format":"yyyy-MM-dd"
             }
          }
       }
    }'

Those data:

    POST https://localhost:9200/test/_doc/1 -d '{"date": "2022-02-28"}'
    POST https://localhost:9200/test/_doc/2 -d '{"date": "2022-06-28"}'
    POST https://localhost:9200/test/_doc/3 -d '{"date": "2022-10-28"}'

    POST https://localhost:9200/test/_doc/4 -d '{"date": "2021-02-28"}'
    POST https://localhost:9200/test/_doc/5 -d '{"date": "2021-06-28"}'
    POST https://localhost:9200/test/_doc/6 -d '{"date": "2021-10-28"}'

    POST https://localhost:9200/test/_doc/7 -d '{"date": "2020-02-28"}'
    POST https://localhost:9200/test/_doc/8 -d '{"date": "2020-06-28"}'
    POST https://localhost:9200/test/_doc/9 -d '{"date": "2020-10-28"}'

This query:

    {
      "from": 0,
      "size": 0,
      "query": {
        "match_all": {}
      },
      "aggs": {
        "date": {
          "date_histogram": {
            "field": "date",
            "format": "YYYY-MM-dd",
            "calendar_interval": "1y"
          }
        }
      }
    }

Elasticsearch is returning the hereafter result.

     "aggregations": {
       "date": {
         "buckets": [
           {
             "key_as_string": "2020-01-01",
             "key": 1577836800000,
             "doc_count": 3}
    ,       {
             "key_as_string": "2020-01-01",
             "key": 1609459200000,
             "doc_count": 3}
    ,       {
             "key_as_string": "2021-01-01",
             "key": 1640995200000,
             "doc_count": 3}]}}

"key_as_string" is wrong whereas the key is correct. Without setting the "format" in the aggregation, the "key_as_string" is correct.

What could be the issue?

I do the mistake to use YYYY instead of yyyy :slight_smile:

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