Search code examples
elasticsearchdatetime-formatelasticsearch-mappingelasticsearch-date

Elasticsearch custom date time format incl. ordinal numbers


I need to define the format for the date field in my index

{
  "mappings": {
    "properties": {
      "date": {
        "type": "date",
        "format": "???"
      }
    }
  }
}

to cover values like February 10th 2021, 23:59:58.556.

I tried MMMM DD YYYY, HH:mm:ss.SSS but it doesn't work.


Solution

  • Go with the following:

    {
      "mappings": {
        "properties": {
          "date": {
            "type": "date",
            "format": "MMMM dd['st']['nd']['rd']['th'] yyyy', 'HH:mm:ss.SSS"
          }
        }
      }
    }
    

    [] denotes optional parts and '' denotes literal parts. So the pattern says that the number of the day may be followed by st, nd, rd or th.

    The ', ' token is needed to cover the comma + whitespace separating the date from the time.