Division in Scripted field

Hi Team,

We are trying to create percentage based on 2 specific properties using Scripted field in kibana.

Below is the code

int AF_failures = 0;
int FL_failures = 0;
int count = 0;

if(!(doc['MarketingCarrier.keyword'].size()==0) && !(doc['OperatingCarrier.keyword'].size()==0))
{
count++;
}

if(!(doc['SegmentSellStatus.keyword'].size()==0)) 
{ 
    if(doc['SegmentSellStatus.keyword'].value == "AF")
    {
        AF_failures++;
    }
}
if(!(doc['SegmentSellStatus.keyword'].size()==0)) 
{ 
    if(doc['SegmentSellStatus.keyword'].value == "FL")
    {
        FL_failures++;
    }
}
if(AF_failures > 0 && count > 0)
{
    return AF_failures/count;
}
else
{
    return 0;
}

The below statement isn't working properly.

if(AF_failures > 0 && count > 0)
{
    return AF_failures/count;
}

if we return directly we are getting divide by zero exception.

Could you please us in resolving this as we have some of the values with zero.

I'm using sum aggregation for this.

Currently we are using 7.4.2.Could you please suggest if we can fix this with any latest kibana version.

Thanks & Regards,
Ramadevi

Painless is basically Java, and your variables are all integers, this is why you are getting zero (it's only doing integer division): Painless integer division

Cast your variables to float before doing the division, then it should work like you expect.

Tried to cast to float, but still it isnt working

if(AF_failures > 0 && count > 0)
{
    return (float)AF_failures/(float)count
}

image

Could you check whether you have a field formatter set on that field removing all things after the decimal point?

Also, please double check your script whether it's actually doing the right thing - when I'm looking at the script, count can only ever be 0 or 1 - is this really intended?

Im expecting calculated value for example if ypu see in 1st row, 887/5004 value in percentage column, but division isn't working.

Your script won't do that calculation. Scripted fields are executed for each document separately, so you can't access aggregated data. In your script, count is set to 0 in the beginning, and bumped to 1 if MarketingCarrier and OperatingCarrier set. This will make count either 0 or 1 for this calculation:

if(AF_failures > 0 && count > 0)
{
    return AF_failures/count;
}

Thanks for your input!
I thought count and af failures will all get calculated based at a time like for loop.
Our goal is to find the percentage of total failure count of AF w.r.t total count(based on 2 other properties which is marketing and operating carrier).
Is there a way we can achieve this.

When you are working with time based data, you can use TSVB to do this because it has a "math" aggregation which allows you to specify a formula executed on the aggregated data.

Actually our goal is to implement datatable, is it not possible to implement this in datatable using scripted fields

You can do calculations in scripted fields and use them in the data table, but you have to be aware these calculations happen for each document separately - there's no way around this. In the datatable visualization you can't do calculations in any another way.

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