Elasticsearch custom date time format incl. ordinal numbers

Viewed 486

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.

2 Answers

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.

look here: Documentation

for example:

PUT 
{
  "mappings": {
    "properties": {
      "my_special_date_field": {
        "type":   "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

a list of all the built-in formats: built-in formats

Related