Using a painless script inside Kibana

Hi there.
I want to extract for some coluns inside a Excel importing inside Kibana few datas.
I've wrote the following script :

def GetServedCall = doc['Success'].value > 0;

if(GetServedCall = true) {
    if(doc['WaitDuration'].value > 0 && doc['WaitDuration'].value <=30)
   return 100 * doc['Success'].value / doc['WaitDuration'].value;
}

But I don't understand few things..

First for my column Success I have bool inside. How Can I get values only if true ?
Next. I want to do a percentage by doing :
//100 * doc['Success'].value / doc['WaitDuration'].value;

Where Success = 1
And WaitDuration<= 30 (it could be 0) ... Any idea how to deal with it ?

Last, How can I display the results inside a Line chart ?
THanks a lot

Hi @NoobieDev,

Unfortunately, I don't understand the meaning behind your data and what result do you want to achieve. But just technically this should work:

First for my column Success I have bool inside. How Can I get values only if true

I think if(doc['Success]'.value) should work

Next. I want to do a percentage by doing

if(doc['Success'].value) {
    if (doc['WaitDuration'].value == 0) return 0; // handle 0 separately to avoid dividing by 0
    if(doc['WaitDuration'].value > 0 && doc['WaitDuration'].value <=30) {
       return 100 * 1 / doc['WaitDuration'].value;
    }
}

return null; // empty otherwise 

Then your new "scripted field" will be available in Kibana visualise to build aggregations for visualisations.

For further guidance with painless language syntax, please refer to Painless Scripting Language [master] | Elastic

Hope this is helpful

Hi @dosant first thanks for you answer it help me a lot.
I want to specify few points.

My objective si from multiple columns having stats. I know how many percent of the calls have been answered under X seconds.

That's Why I want only if my columns Succes is equal to 1 (This is a boolean).

Concerning your script thanks a lot.

But quick question, When I put your script to try it I gave the following error ?

cannot convert MethodHandle(Longs)long to (Object)boolean

@NoobieDev, oh

cannot convert MethodHandle(Longs)long to (Object)boolean

Looks like problem in this line:

if(doc['Success'].value)

Hope this should work then:

 if(doc['Success'].value == 1) {
     if (doc['WaitDuration'].value == 0) return 0; // handle 0 separately to avoid dividing by 0
     if(doc['WaitDuration'].value > 0 && doc['WaitDuration'].value <=30) {
        return 100 * 1 / doc['WaitDuration'].value;
     }
 }
 
return null; // empty otherwise

Thank you I don't know why I don't think about it earlier...

Last question I'm sorry I'm a bit noob on painless.. idon't understand averything..
Is it possible to make something similar to a count in SQL ?
By this, I mean, I want to able to count all ma lines where Success == 1.
SO I think a autoincrement of type

    nb_success = 0;
    if(doc['Success'].value == 1) {
    nb_sucess ++;
    }

But is it possible to filter later By date Like I can do in Vizuaisation ? TO get all sucess on last monday for exemple ?

Thank a lot for your help @dosant :slight_smile:

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