2

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.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
user13145920
  • 179
  • 1
  • 9

2 Answers2

2

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.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
1

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

Tom Elias
  • 751
  • 6
  • 15
  • that not the correct format `failed to parse date field [February 10th 2021 , 23:59:58.556] with format [yyyy-MM-dd HH:mm:ss]` – user13145920 Feb 11 '21 at 14:41
  • ah you mean you want the exact format that fits your input? check the 2nd link, i'll look through it as well. – Tom Elias Feb 11 '21 at 14:42
  • Yes I need the format for `February 10th 2021 , 23:59:58.556` I didn't find an answer in your link – user13145920 Feb 11 '21 at 14:44
  • the closest thing i see is RFC_1123_DATE_TIME: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#RFC_1123_DATE_TIME which the documentation says is supported. – Tom Elias Feb 11 '21 at 14:45
  • But how can I write that in format of `mm-yyyy` ? I can't put `RFC_1123_DATE_TIME` into format – user13145920 Feb 11 '21 at 14:46