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

**URL:** https://discuss.elastic.co/t/non-existing-field-in-the-document-in-mvel-script-causes-exception-even-after-containskey-check/12198
**Category:** Elasticsearch
**Created:** [May 31, 2013, 4:01am UTC](https://discuss.elastic.co/t/non-existing-field-in-the-document-in-mvel-script-causes-exception-even-after-containskey-check/12198 "2013-05-31T04:01:55Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Amarthya\_Master](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@Amarthya\_Master](https://discuss.elastic.co/u/Amarthya_Master)
#### Post date: [May 31, 2013, 4:01am UTC](https://discuss.elastic.co/t/non-existing-field-in-the-document-in-mvel-script-causes-exception-even-after-containskey-check/12198/1 "2013-05-31T04:01:55Z")

</div>

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 2:33am UTC](https://discuss.elastic.co/t/non-existing-field-in-the-document-in-mvel-script-causes-exception-even-after-containskey-check/12198/2 "2017-07-06T02:33:45Z")

</div>


