If else Painless Script on a Painless Scripted Field

Hi. Our team made a scripted field for time difference called "time". We have a new use case where if the value in "time" is greater than 8hours, it will return as 'Yes' and if not 'No'. The value for 'time' is set to minutes. The script below kept returning 'Yes' even if 'time' is less than 8hours. Are we going in the right direction? Thanks.

if (!doc.containsKey('time') || doc['delay'].value >= 28800000) { return 'Yes' } else { return 'No' }

So time is a scripted field itself? If that's the case, you can't use it within another scripted field as they can't reference each other. If you want to use the same logic, you have to copy it over. That's probably also why the new scripted field is always returning Yes - the time field is never available because it's not in the original document the scripted field has access to.

Hi @flash1293. Thank you for replying.

Can you check this?

if (!doc.containsKey('T1') || (doc['T1'].value.getMillis() - doc['T2'].value.getMillis()) >= 28800000) { 
    return 'Inactive' 
} 
else { 
    return 'Active' 
}

This is implementing what you said about copying over the logic instead of using the scripted field "time". On the preview, there was no error but there are no results as well. The preview came back with:

[]

What might be the problem this time? Thank you.

Maybe you have to check for the existence of T2 as well? If that's not the issue, could you share an example document?

Hi @flash1293. We tried saving the scripted field even if the preview shows

[]

The script works properly in Discover and in other visualizations we tried it on. I guess there's a bug on painless? We are using Kibana 7.6.2. Thank you.

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