# If condition in scripting

**URL:** <https://discuss.elastic.co/t/if-condition-in-scripting/179963>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [May 7, 2019, 12:01pm UTC](https://discuss.elastic.co/t/if-condition-in-scripting/179963 "2019-05-07T12:01:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![srivalya\_elluru](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srivalya_elluru/32/45718_2.png) [@srivalya\_elluru](https://discuss.elastic.co/u/srivalya_elluru)\
**Post date:** [May 7, 2019, 12:01pm UTC](https://discuss.elastic.co/t/if-condition-in-scripting/179963/1 "2019-05-07T12:01:47Z")

</div>

Hello I'm new to painless language, and I don't understand how to exactly to write an if condition.  
I want to show the value 1 or 0 in my visualization but the if statement I wrote doesn't seem to work.

{ "script" : {  
"lang": "painless",  
"source": "if ( (float)doc['field'].value/ 3 \>=1 ) ? 1 : 0 ; "  
}}

Also what is inline and when to use it ?  
Please help me out, I seem to be badly stuck at this.

Thank You

---

<div class="post-metadata">

**Author:** ![lukeelmers](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukeelmers/32/35230_2.png) [@lukeelmers](https://discuss.elastic.co/u/lukeelmers)\
**Post date:** [May 7, 2019, 10:36pm UTC](https://discuss.elastic.co/t/if-condition-in-scripting/179963/2 "2019-05-07T22:36:09Z")

</div>

It looks like you are combining an `if` statement with a ternary -- try removing `if` and see if your script works.

Inline scripts allow you to define the script in your request directly. The alternative is using [stored scripts](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#modules-scripting-stored-scripts), and simply referring to them by `id` in your request. This means you'd be using the `_scripts` APIs to store the actual script, and then you only need to reference that `id` elsewhere (and pass params as needed).

---

<div class="post-metadata">

**Author:** ![srivalya\_elluru](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srivalya_elluru/32/45718_2.png) [@srivalya\_elluru](https://discuss.elastic.co/u/srivalya_elluru)\
**Post date:** [May 8, 2019, 1:45pm UTC](https://discuss.elastic.co/t/if-condition-in-scripting/179963/3 "2019-05-08T13:45:00Z")

</div>

@ lukeelmers

Thankyou, I have made similar change and it worked 🙂

---

<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 5, 2019, 1:45pm UTC](https://discuss.elastic.co/t/if-condition-in-scripting/179963/4 "2019-06-05T13:45:04Z")

</div>

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