Scripted Field Percentage Calculation

Hi all,

I am trying to make a % calculation based on two fields.

I made the following script:

if (doc['doc.contextMap.totalErrors.keyword'].value != null && doc['doc.contextMap.totalInput.keyword'].value != null) {
return (Integer.parseInt(doc['doc.contextMap.totalErrors.keyword'].value) / Integer.parseInt(doc['doc.contextMap.totalInput.keyword'].value)) * 100;
} else {
return -100;
}

The outcome is always 0. It pass the IF condition but the calculation is always 0.

Any suggestions on what I am doing wrong?

Thanks

null and 0 are different values. It may be that the doc['doc.contextMap.totalErrors.keyword'].value is 0. Can you verify?

Hi,

There are a values in totalErrors. Now I am thinking about another problem, may be I need to cast it into float and not an integer. What should be the syntax to do it?

Thanks.

Indeed the problem was in casting to integer instead of float.

Here is the correct script that work for me

if (doc['doc.contextMap.totalErrors.keyword'].value != null && doc['doc.contextMap.totalInput.keyword'].value != null) {
return (Float.parseFloat(doc['doc.contextMap.totalErrors.keyword'].value) / Float.parseFloat(doc['doc.contextMap.totalInput.keyword'].value));
} else {
return -100;
}

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