Creating a new field through painless scripts

Hello,
I am trying to create a new field based on a particular condition.
if (doc['foo.keyword'].value == 'bar'){ doc['foo2.keyword'].value = 'bar2' }
But it gives my an error saying Script is invalid. View script preview for details

Hi @maheshamruthaluri,

the way scripted fields work is that you provide a script which returns a value that becomes the value of the field.

If you want to only set foo2.keyword to 'bar2' if foo.keyword equals 'bar', you have to create a scripted field with the name foo2.keyword and use the following script:

if (doc['foo.keyword'].value == 'bar'){ return 'bar2'; }

If the condition is not met, the field value is simply undefined

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