How to check a doc field exist while creating scripted field using painless

if (doc.containsKey('fieldname')  {
  return 0; 
}
else {
  return 1;
}

The above code return compile error in kibana and ES 5.2.0.
What is the correct syntax of painless to check if a field exists?

My solution:
After searching a while, find that

doc['field_name'].empty
A boolean indicating if the field has no values within the doc.

will return true if the field does not exist on the records.

Ref

1 Like

what about if (doc['fieldname']) ?

if (doc['fieldname'])

It return a warning "Courier Fetch: 5 of 5 shards failed." when I search *. And no result return.

Update:
The following error found in elasticsearch.log

Caused by: java.lang.ClassCastException: org.elasticsearch.index.fielddata.ScriptDocValues$Strings cannot be cast to java.lang.Boolean

1 Like

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

Did you solve your issue? I just posted a response here that might help you;