Roopendra
Roopendra

Reputation: 7776

Custom date format in elasticsearch mapping

I am trying to index data with date format Tue May 14 17:06:01 PDT 2013. As described in Elasticsearch Date Format document I need to use custom date format. I refer to DateTimeFormat document and respective format is E M d H:m:s z Y.

I am able to create mapping but when I am trying to index data its giving me error.

Mapping:-

{
  "tweet": {
    "properties": {
      "user": {
        "type": "string",
        "index": "not_analyzed"
      },
      "message": {
        "type": "string",
        "null_value": "na"
      },
      "postDate": {
        "type": "date",
        "format": "E M d H:m:s z Y"
      },
      "priority": {
        "type": "integer"
      },
      "rank": {
        "type": "float"
      }
    }
  }
}

Index Document:-

curl -XPUT 'http://localhost:9200/tweets/tweet/1' -d '{
        "user" : "kimchy",
        "message" : "This is a tweet!",
        "postDate" : "Tue May 14 17:06:01 PDT 2013",
        "priority" : 4,
        "rank" : 12.3
}'

Error:-

{"error":"MapperParsingException[failed to parse [postDate]]; 
nested: MapperParsingException[failed to parse date field [Tue May 14 17:06:01 PDT 2013],
tried both date format [E M d H:m:s z Y], and timestamp number with locale []];
nested: IllegalArgumentException[Invalid format: \"Tue May 14 17:06:01 PDT 2013\"
is malformed at \"May 14 17:06:01 PDT 2013\"]; ","status":400}

Any Suggestion?

Upvotes: 7

Views: 7998

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

For Month use three 'M's. Quote from the API docs:

Month: 3 or over, use text, otherwise use number.

So the correct mapping for the input you provided should be:

    "postDate": {
      "type": "date",
      "format": "E MMM d H:m:s z Y"
    }

Upvotes: 7

Related Questions