Formatting of dates returned by query

The Elasticsearch documentation about the date data type says:

Multiple formats can be specified by separating them with || as a separator. Each format will be tried in turn until a matching format is found. The first format will be used to convert the milliseconds-since-the-epoch value back into a string.

https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html#multiple-date-formats

If I do the following:

PUT date_mapping_test
{
  "mappings": {
    "date_test": {
      "properties": {
        "date": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

PUT date_mapping_test/date_test/1
{
  "date": 1507804756000
}

PUT date_mapping_test/date_test/2
{
  "date": "2017-10-11"
}

PUT date_mapping_test/date_test/3
{
  "date": "2017-10-11 11:41:00"
}

GET date_mapping_test/date_test/_mapping

GET date_mapping_test/date_test/_search

Then I get the following response showing the date field values in the format that they were indexed.

  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "date_mapping_test",
        "_type": "date_test",
        "_id": "2",
        "_score": 1,
        "_source": {
          "date": "2017-10-11"
        }
      },
      {
        "_index": "date_mapping_test",
        "_type": "date_test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "date": 1507804756000
        }
      },
      {
        "_index": "date_mapping_test",
        "_type": "date_test",
        "_id": "3",
        "_score": 1,
        "_source": {
          "date": "2017-10-11 11:41:00"
        }
      }
    ]
  }

Whereas the documentation quoted above implies the dates should be formatted according to the first format in the list in the mapping, i.e. "yyyy-MM-dd HH:mm:ss".

Am I doing something wrong or misunderstanding the documentation, or is this a bug?

I am using Elasticsearch 5.5.1 on Windows.

May it's unclear.

Elasticsearch never modifies the JSON document you sent. It's stored in _source field as is.

But if you store specifically the field date and then in _search ask for stored_field date instead of default _source you will get the converted value.

Thanks for the explanation, that makes sense.

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