# Performing operations on Scripted fields in painless

**URL:** <https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [May 23, 2022, 1:03pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403 "2022-05-23T13:03:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![behappy\_alwayz0401](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/behappy_alwayz0401/32/105988_2.png) [@behappy\_alwayz0401](https://discuss.elastic.co/u/behappy_alwayz0401)\
**Post date:** [May 23, 2022, 1:03pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/1 "2022-05-23T13:03:09Z")

</div>

Can if conditions be performed on scripted fields in Kibana painless?  
For example, I have a scripted field "TAT" which is the difference in 2 times to return turn around time in minutes. I want to see values for TAT \> 90 minutes but I am unable to do so even in the filter section or as a scripted field on TAT.

**Filter:** TAT.keyword \>= 90

**Scripted Field Attempt:**  
if(doc['TAT.keyword'].value\>90) {return "High"}

For both, it says TAT does not exist or TAT.Keyword does not exist. TAT is a scripted field as well. Thanks

---

<div class="post-metadata">

**Author:** ![Stratoula\_Kalafateli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stratoula_kalafateli/32/70923_2.png) [@Stratoula\_Kalafateli](https://discuss.elastic.co/u/Stratoula_Kalafateli)\
**Post date:** [May 23, 2022, 1:54pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/2 "2022-05-23T13:54:04Z")

</div>

If I am not mistaken, referencing a scripted field inside another will most likely fail as there is no priority in computing the scripted fields so you don't always have the field present when trying to compute the other one.  
Why don't you move both calculations inside one single scripted field?

---

<div class="post-metadata">

**Author:** ![behappy\_alwayz0401](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/behappy_alwayz0401/32/105988_2.png) [@behappy\_alwayz0401](https://discuss.elastic.co/u/behappy_alwayz0401)\
**Post date:** [May 23, 2022, 2:05pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/3 "2022-05-23T14:05:12Z")

</div>

Yes I can do that, I was only wondering with this approach each time I need to perform an operation on the TAT, I would need to include the calculations for TAT rather than use TAT like any other field. But since you mention, that it may not be possible to use a scripted field inside another, I'll take the calculations approach. Thanks!!

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [May 25, 2022, 2:28pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/4 "2022-05-25T14:28:02Z")

</div>

This sounds like a good use case for [runtime fields](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/runtime.html), which was added in 7.11. They are scripted fields that act like real fields, which means that they can be referenced by another runtime field.

---

<div class="post-metadata">

**Author:** ![behappy\_alwayz0401](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/behappy_alwayz0401/32/105988_2.png) [@behappy\_alwayz0401](https://discuss.elastic.co/u/behappy_alwayz0401)\
**Post date:** [May 25, 2022, 4:13pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/5 "2022-05-25T16:13:08Z")

</div>

Sounds good! Is there any example you can show me please about how to bring the above out in a runtime field? Thanks

---

<div class="post-metadata">

**Author:** ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)\
**Post date:** [May 25, 2022, 6:46pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/6 "2022-05-25T18:46:52Z")

</div>

In this case you would make `TAT` a runtime field, the only difference is to `emit(returnValue);` rather than `return returnValue;`.

To do add a runtime field via kibana, checkout the [managing data views](https://www.elastic.co/guide/en/kibana/master/managing-data-views.html#create-runtime-fields) documentation.

You can try it out by adding it in a [search request](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/runtime-search-request.html) against Elasticsearch directly.

---

<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 22, 2022, 6:47pm UTC](https://discuss.elastic.co/t/performing-operations-on-scripted-fields-in-painless/305403/7 "2022-06-22T18:47:15Z")

</div>

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