user13145920
user13145920

Reputation: 189

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.

Upvotes: 2

Views: 989

Answers (2)

Joe - Check out my books
Joe - Check out my books

Reputation: 16895

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.

Upvotes: 2

Tom Elias
Tom Elias

Reputation: 782

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

Upvotes: 1

Related Questions