Can't figure out how to handle missing doc fields with ScriptedFields

I upgraded my ES to version 7.1 and ofcourse I ran into issue, where Scripted Fields cant have empty doc fields anymore.

My scripted field is concating 3 fields and last one of might be sometimes missing.
Could someone reach an helping hand and help me converting the script so that it would work with 7.1 ES?

The script itself:
doc['field1.keyword'].value + ':' +
doc['field2.keyword'].value + ':' +
doc['field3.keyword'].value

I tried implementing the if statement, but it went to total garbage. As I said field3 is sometimes missing from doc.

I figured out the solution:

def field1='';
def field2='';
def field3='';
if (doc['field1.keyword'].size() > 0) field1 = doc['field1.keyword'].value + ':';
if (doc['field2.keyword'].size() > 0) field2 = doc['field2.keyword'].value;
if (doc['field3.keyword'].size() > 0) field3 = ':' + doc['field3.keyword'].value;
return field1 + field2 + field3;

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