scth
scth

Reputation: 71

Elasticsearch: comparing dates (painless script)

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!

Upvotes: 3

Views: 13427

Answers (2)

Steven Ensslen
Steven Ensslen

Reputation: 1386

Elasticsearch index fields with date types are org.joda.time.DateTime in painless. Using the SimpleDateFormat is the source of the error. Try this instead:

long timestampLog = doc['createdAt'].value.getMillis();
long timestampNow = new Date().getTime();

The rest works as is.

Tested on Elasticsearch 6.3.0.

Upvotes: 5

Hearen
Hearen

Reputation: 7838

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();

Upvotes: 1

Related Questions