Script field to subtract dates, compute time difference, not working


(Sijakret) #1

hi,

i am trying to compute a date difference in a scripted field

if(!doc['lastseen'].empty && !doc['firstseen'].empty) {
 return doc['lastseen'].value - doc['firstseen'].value
} else {
 return 0
}

first seen and last seen are date fields.
When i go to discover i get the following error:

"Courier Fetch: 2 of 5 shards failed."

if i return 0 in the if statement all is fine. The same if i just return doc['firsteen'].value or doc['lastseen'].value.
The field is set to type: number.

to me it seems to me that as soon as i start to do arithmetics on the fields values the discovery breaks.


(Sijakret) #2

the scripting language was set to painless.
if i switch to "expression" and use a ternary expression it works :confused:

(!doc['lastseen'].empty && !doc['firstseen'].empty) ?
 doc['lastseen'].value - doc['firstseen'].value
: 0

(Matt Bargar) #3

If you want to do date math in Painless you'll need to cast the field values to date types first. There's a good blog post here which includes some examples of working with dates, and other great Painless tips.


(system) #4

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