Kibana painless scripted fields

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

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;

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 you get some ideas how to do that.