# Painless scripted field with nested IF statment

**URL:** <https://discuss.elastic.co/t/painless-scripted-field-with-nested-if-statment/352091>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [January 30, 2024, 1:20pm UTC](https://discuss.elastic.co/t/painless-scripted-field-with-nested-if-statment/352091 "2024-01-30T13:20:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![auato](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@auato](https://discuss.elastic.co/u/auato)\
**Post date:** [January 30, 2024, 1:20pm UTC](https://discuss.elastic.co/t/painless-scripted-field-with-nested-if-statment/352091/1 "2024-01-30T13:20:11Z")

</div>

I have these two working scripted fields:

`def M3 = doc['obj'].value.splitOnToken('_'); return M3.length >= 4 ? M3[3] : null;`

`def M2 = doc['obj'].value.splitOnToken('_'); return M2.length >= 4 ? M2[2] : null;`

that I would like to merge in an unique scripted field using a nested IF statement following these rules:

`if(doc['label'].value == 'MLD42')`

return (M3):  
`doc['obj'].value.splitOnToken('_'); return LU2.length >= 4 ? LU2[2] : null;`

else return(M2):  
`doc['obj'].value.splitOnToken('_'); return LU3.length >= 4 ? LU3[3] : null;`

I still can't do it correctly, please help me with the correct syntax  
Thank you

---

<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 27, 2024, 1:20pm UTC](https://discuss.elastic.co/t/painless-scripted-field-with-nested-if-statment/352091/2 "2024-02-27T13:20:18Z")

</div>

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