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


(Kambiu) #1
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
https://www.elastic.co/guide/en/elasticsearch/reference/5.2/modules-scripting-expression.html


How to fix index_out_of_bounds_exception exception in aggregation in painless script
(Peter Pisljar) #2

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


(Kambiu) #3

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


(system) #4

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


(Lee Drengenberg) #5

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