# Using a painless script inside Kibana

**URL:** <https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [April 20, 2020, 10:35am UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839 "2020-04-20T10:35:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NoobieDev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noobiedev/32/65661_2.png) [@NoobieDev](https://discuss.elastic.co/u/NoobieDev)\
**Post date:** [April 20, 2020, 10:35am UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839/1 "2020-04-20T10:35:00Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [April 20, 2020, 12:31pm UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839/2 "2020-04-20T12:31:41Z")

</div>

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

```auto
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](https://www.elastic.co/guide/en/elasticsearch/painless/master/index.html)

Hope this is helpful

---

<div class="post-metadata">

**Author:** ![NoobieDev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noobiedev/32/65661_2.png) [@NoobieDev](https://discuss.elastic.co/u/NoobieDev)\
**Post date:** [April 30, 2020, 1:15pm UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839/3 "2020-04-30T13:15:42Z")

</div>

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 ?

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

```

---

<div class="post-metadata">

**Author:** ![dosant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dosant/32/64489_2.png) [@dosant](https://discuss.elastic.co/u/dosant)\
**Post date:** [May 6, 2020, 10:45am UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839/4 "2020-05-06T10:45:08Z")

</div>

@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

```

---

<div class="post-metadata">

**Author:** ![NoobieDev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noobiedev/32/65661_2.png) [@NoobieDev](https://discuss.elastic.co/u/NoobieDev)\
**Post date:** [May 7, 2020, 8:11am UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839/5 "2020-05-07T08:11:24Z")

</div>

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

```auto
    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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 4, 2020, 8:11am UTC](https://discuss.elastic.co/t/using-a-painless-script-inside-kibana/228839/6 "2020-06-04T08:11:40Z")

</div>

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