Date in script field

Good morning,

I'm doing examples of script fields and I have read the documentation but I don't understand how I can use date fields. My idea is get the age using scripts:

GET /prova/_search
{
"_source": {},
"script_fields": {
"a": {
"script": {
"lang": "painless",
"inline": "def a = '2017-09-27T00:00:00.000Z'; return a.date.year-doc['dt'].date.year"
}
}
}
}

Exist the type Date? I have read that there is: String, list, map etc. but not date, and now when I declare this variable
is a String, and thisis the cause that I can't do ".date.year

Can you help me please?

Thanks!!

Hi!

I have read that the painless use the primitive types of Java and the Date is not one of them, is imposible declare a date.now?

Thanks!

You need to look at the Java8 time apis. You will need to parse your string date into probably a DateTime, then convert the ReadableDateTime (a joda class) to java 8's DateTime as well in order to compare.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.