Non-existing field in the document in MVEL script causes exception even after containsKey check

I have a problem but the solution provided for some earlier posts on the
same issue to use "containsKey" check is not working.

Under custom script, I have this:

"script" : "(doc.containsKey("my_field") ? doc['my_field].value : 0)"

This fails on doc['my_field].value apprently, with the exception:
Query Failed [Failed to execute main query]]; nested:
PropertyAccessException[[Error: could not access: value; in class:
org.elasticsearch.index.fielddata.ScriptDocValues$Empty [Near : {... value

In the index document mapping i see that my_field has a valid entry. Is
that the reason why "containsKey" its returning true? The field is
defintely not in my document, which is what causing the issue.
Any way I could make the script work?

Greatly appreciate any help.

You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit