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!
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With