# Kibana painless scripted fields

**URL:** <https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [October 17, 2022, 10:45am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762 "2022-10-17T10:45:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [October 17, 2022, 10:45am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/1 "2022-10-17T10:45:00Z")

</div>

Hi elastic Team,

I want to calculate the number of adults and children in every room.  
Below is an example of my document:  
"rooms": [  
{  
"pensionId": 16,  
"LibelleChambre": "Twin GardenView ",  
"MontantRemise": 0,  
"NumChambre": 1,  
"Enfants": 0,  
"Arrangement": "All Inclusive Soft Drink",  
"Bebes": 0,  
"quantite": 1,  
"Adultes": 2  
},  
{  
"pensionId": 16,  
"LibelleChambre": "Triple Vue Jardin",  
"MontantRemise": 0,  
"Ages": "11",  
"NumChambre": 2,  
"Enfants": 1,  
"Arrangement": "All Inclusive Soft Drink",  
"Bebes": 0,  
"quantite": 1,  
"Adultes": 2  
},  
{  
"pensionId": 16,  
"LibelleChambre": "Triple Vue Jardin",  
"MontantRemise": 0,  
"NumChambre": 3,  
"Enfants": 0,  
"Arrangement": "All Inclusive Soft Drink",  
"Bebes": 0,  
"quantite": 1,  
"Adultes": 3  
},  
{  
"pensionId": 16,  
"LibelleChambre": "Triple Vue Jardin",  
"MontantRemise": 0,  
"NumChambre": 4,  
"Enfants": 0,  
"Arrangement": "All Inclusive Soft Drink",  
"Bebes": 0,  
"quantite": 1,  
"Adultes": 3  
}  
],

The output goes as  
2  
I am anticipating the output as below :  
2,3,3,3

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [October 17, 2022, 4:40pm UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/2 "2022-10-17T16:40:40Z")

</div>

How does your runtime field look like?

---

<div class="post-metadata">

**Author:** ![hiba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hiba/32/110919_2.png) [@hiba](https://discuss.elastic.co/u/hiba)\
**Post date:** [October 18, 2022, 7:27am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/3 "2022-10-18T07:27:48Z")

</div>

> [@Felix\_Roessel](#):
>
> How does your runtime field look like?

if( !doc['rooms.Enfants'].empty && !doc['rooms.Adultes'].empty) {  
return doc['rooms.Enfants'].value + doc['rooms.Adultes'].value;  
} else return null;

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [October 18, 2022, 7:58am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/4 "2022-10-18T07:58:21Z")

</div>

Your code looks like the deprecated scripted field. It's not a runtime field right?

In a runtime field you need to iterate through the "rooms" array and emit() the value of your calculation per room to get to the expected results.

In these [Runtime field examples](https://elastic-content-share.eu/elastic-runtime-field-example-repository/) you get some ideas how to do that.

---

<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:** [November 13, 2022, 7:16pm UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/5 "2022-11-13T19:16:23Z")

</div>



---

<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:** [December 11, 2022, 11:06am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/6 "2022-12-11T11:06:55Z")

</div>



---

<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:** [January 8, 2023, 7:42am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/7 "2023-01-08T07:42:56Z")

</div>



---

<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:** [February 5, 2023, 7:43am UTC](https://discuss.elastic.co/t/kibana-painless-scripted-fields/316762/8 "2023-02-05T07:43:47Z")

</div>

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