Elasticsearch: comparing dates (painless script)

scth picture scth · Jun 14, 2018 · Viewed 7.1k times · Source

My mapping of createdAt:

"createdAt": {
    "type": "date"
},

I insert the dates like this:

POST logs/_doc/_bulk?pretty
{"index":{"_id":1}}
{"createdAt":"2018-05-01T07:30:00Z","value":"on"}

When I request the documents

GET logs/_doc/_search

It shows me the date as I inserted it:

"_source": {
    "createdAt": "2018-05-01T07:30:00Z",
    "value":"on"
}

Now I'd like to compare this date with the current time:

"map_script": {
    long timestampLog = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S").parse(doc.createdAt.value).getTime();
    long timestampNow = new Date().getTime();

    if (timestampNow < timestampLog) {
        // case 1
    } else {
        // case 2
    }
}

Weird:
doc.createdAt.value returns "2018-05-01T07:30:00.000Z", which includes milliseconds that I never added.

This error occurs while parsing:

Cannot cast org.joda.time.MutableDateTime to java.lang.String 

When I replace doc.createdAt.value by the string 2018-05-01T07:30:00.000Z, it works.

Any help is appreciated. Thank you very much!

Answer

Hearen picture Hearen · Jun 14, 2018

Please remove the big S in the formatter, check Date and Time Patterns

long timestampLog = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").parse(doc.createdAt.value).getTime();
long timestampNow = new Date().getTime();