Make painless script conditional on presence of field

I currently have the following painless script:

def m = /^(/[^/?&]+).+/.matcher(doc['request_uri.keyword'].value); if ( m.matches() ) { return m.group(1) } else { return "" }

This works just fine, as long as request_uri.keyword exists. And fails horribly when it doesn't exist.

I tried stuff like:

if (doc['request_uri.keyword'] != null) { def m = /^(/[^/?&]+).+/.matcher(doc['request_uri.keyword'].value); if ( m.matches() ) { return m.group(1) } else { return "" } }

But that doesn't appear to be helping either (various other variants won't even compile).

What would be the proper way to check if a field actually exists? This is on ES5.5 btw.

The doc variable is a java Map, so you can do doc.containsKey('request_uri.keyword')

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