Search code examples
datetimeelasticsearchcurlelasticsearch-7

Index mapping having date format, using curl throws parsing exception


Trying to specify format (in index mapping) for the date field in ES 7.6. Any of these are not accepted:

        "createdAt" : {
          "type" : "date",
          "format": "yyyy-MM-dd'''T'''HH:mm:ss.SSSZZ"
        },
        "createdAt" : {
          "type" : "date",
          "format": "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"
        },

Error is always the same:

"type" : "illegal_argument_exception", "reason" : "Invalid format: [yyyy-MM-ddTHH:mm:ss.SSSZZ]: Unknown pattern letter: T",

Here is full example to reproduce:

curl -X DELETE "localhost:9200/example?pretty"
curl -X PUT   "localhost:9200/example/_mappings?pretty" -H 'Content-Type: application/json' -d' {
      "dynamic": false,
      "properties" : {
        "name" : {
          "type" : "text"
        },
        "createdAt" : {
          "type" : "date",
          "format" : "yyyyMMdd'T'HHmmss.SSSZ"
        }
      }
}'

Solution

  • You can check various supported date format on https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html:

    The correct format for your date format is below

    "format" : "yyyyMMdd'T'HHmmss.SSSZ" (no - in between yyyyMMdd)

    I just created an index with below format, so that you can try yourself:

    {
      "mappings": {
        "properties": {
          "date": {
            "type": "date" ,
            "format" : "yyyyMMdd'T'HHmmss.SSSZ" --> notice there is no `-` in yyyyymmdd
          }
        }
      }
    }
    

    EDIT:- As per the latest update from OP, he is using the curl command to create the indices, hence he needs to escape the apostrophe('') present in the date T field.

    Proper curl command would like below:

    curl -X PUT "localhost:9500/example/_mappings?pretty" -H 'Content-Type: application/json' -d' {
          "dynamic": false,
          "properties" : {
            "name" : {
              "type" : "text"
            },
            "createdAt" : {
              "type" : "date",
              "format" : "yyyyMMdd'\''T'\''HHmmss.SSSZ" --> notice escape `T`
            }
          }
    }'
    

    Which gives proper output in curl:

    {
      "acknowledged" : true
    }