Troy
Troy

Reputation: 1839

Parse date string in elasticsearch using custom joda format string

Trying to figure out why this joda custom format is causing an error. I'm trying to match this date string:

Wed May 23 2018 13:45:04 GMT-0700 (Pacific Daylight Time)

with this joda custom format string:

E MMM dd yyyy HH:mm:ss z (zzzz)||epoch_millis

I'm doing this in the dev console to test a mapping that uses the format. Elasticsearch doesn't like it:

PUT /twitter
{}

PUT /twitter/_mapping/_doc
{
    "properties": {
        "TxnDate": {
            "type": "date",
            "format": "E MMM dd yyyy HH:mm:ss z (zzzz)||epoch_millis"
        }
    }
}

Elasticsearch is returning:

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Incomplete parser array"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Incomplete parser array"
    },
    "status": 400
}

Upvotes: 0

Views: 363

Answers (1)

Val
Val

Reputation: 217554

In order to get the mapping to save, the correct format to be used is this one, i.e. you need to escape GMT- and the parenthesis.

E MMM dd yyyy HH:mm:ss 'GMT'Z '('ZZZZ')'||epoch_millis

However, this is not the end of the story, unfortunately... You'll then get a parsing error at indexing time when saving your document with a date such as Wed May 23 2018 13:45:04 GMT-0700 (Pacific Daylight Time). The problem here is that Joda time doesn't parse timezones as "explained" in their documentation:

Zone names: Time zone names ('z') cannot be parsed.

So your only option is to remove the timezone in parenthesis before indexing your document and the pattern E MMM dd yyyy HH:mm:ss 'GMT'Z||epoch_millis will work fine. The timezone in parenthesis is useless anyway.

On another note, you should take the habit of storing all your dates in the GMT timezone, but that's another story.

Upvotes: 1

Related Questions