Painless script error

Hi, I'm trying to create a scripted field with the simple painless-code bellow.
Math.round((doc['CRC_OK'].value / doc['NR_UWs'].value)*100)/100.0
Everything seems to be working when I preview the code:


When I open the discover-page I immediately get an error like this:
image
I tried to remove the multiplication and divisiion of 100, but that didn't make any difference.
I would be grateful if you could help my find the bug.

Thanks!

Is it possible there are documents in your indices which don't have a value for either CRC_OK or NR_UWs?

A typical solution is to safeguard the script with checks for all used fields:

if (doc['CRC_OK'].size()==0 || doc['NR_UWs'].size()==0) return;
return Math.round((doc['CRC_OK'].value / doc['NR_UWs'].value)*100)/100.0

You were right that one document was missing the values. Your code threw an error as well, but after a small change it worked!

if (doc['CRC_OK'].size()>0 || doc['NR_UWs'].size()>0) {
return Math.round((doc['CRC_OK'].value / doc['NR_UWs'].value)*100)/100.0
}

Thanks a lot!

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