# Referring to a non-existent field in an elasticsearch mvel scoring script

**URL:** https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530
**Category:** Elasticsearch
**Created:** [November 2, 2012, 2:59pm UTC](https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530 "2012-11-02T14:59:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Amy](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Amy](https://discuss.elastic.co/u/Amy)
#### Post date: [November 2, 2012, 2:59pm UTC](https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530/1 "2012-11-02T14:59:49Z")

</div>

Hi,  
I have a mvel scoring script that refers to a field:

"(doc['myfield'].value == '1') ? 1 : 0)"

None of the documents in the type that I am searching have this field yet.  
When I perform a search that includes this script I get a  
QueryPhaseExecutionException:

ElasticSearchIllegalArgumentException[No field found for [myfield] in  
mapping with types [type]];

As a workaround, whenever I add a document to a new type that I want to  
search I have to add an 'initialiser' document that contains all the fields  
referred to in my scoring script.  
Is there a better way?

--

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [November 2, 2012, 5:23pm UTC](https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530/2 "2012-11-02T17:23:23Z")

</div>

There are a couple of ways to deal with this issue. First, instead of  
adding an "initialiser" document, you can simply add mapping for the new  
field using Put Mapping API[http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html](http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html). Alternatively,  
you can add a check into your script for doc.containsKey('myfield'). If  
myfield is not in the mapping yet, this check will return false.

On Friday, November 2, 2012 10:59:49 AM UTC-4, Amy wrote:

> Hi,  
> I have a mvel scoring script that refers to a field:
> 
> "(doc['myfield'].value == '1') ? 1 : 0)"
> 
> None of the documents in the type that I am searching have this field yet.  
> When I perform a search that includes this script I get a  
> QueryPhaseExecutionException:
> 
> ElasticSearchIllegalArgumentException[No field found for [myfield] in  
> mapping with types [type]];
> 
> As a workaround, whenever I add a document to a new type that I want to  
> search I have to add an 'initialiser' document that contains all the fields  
> referred to in my scoring script.  
> Is there a better way?

--

---

<div class="post-metadata">

### Author: ![Amy](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Amy](https://discuss.elastic.co/u/Amy)
#### Post date: [November 9, 2012, 12:25pm UTC](https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530/3 "2012-11-09T12:25:38Z")

</div>

Thanks very much! I used doc.containsKey("myfield") and it worked.

On Friday, November 2, 2012 5:23:23 PM UTC, Igor Motov wrote:

> There are a couple of ways to deal with this issue. First, instead of  
> adding an "initialiser" document, you can simply add mapping for the new  
> field using Put Mapping API[http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html](http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html). Alternatively,  
> you can add a check into your script for doc.containsKey('myfield'). If  
> myfield is not in the mapping yet, this check will return false.
> 
> On Friday, November 2, 2012 10:59:49 AM UTC-4, Amy wrote:
> 
> > Hi,  
> > I have a mvel scoring script that refers to a field:
> > 
> > "(doc['myfield'].value == '1') ? 1 : 0)"
> > 
> > None of the documents in the type that I am searching have this field  
> > yet. When I perform a search that includes this script I get a  
> > QueryPhaseExecutionException:
> > 
> > ElasticSearchIllegalArgumentException[No field found for [myfield] in  
> > mapping with types [type]];
> > 
> > As a workaround, whenever I add a document to a new type that I want to  
> > search I have to add an 'initialiser' document that contains all the fields  
> > referred to in my scoring script.  
> > Is there a better way?

--

---

<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, 3:59am UTC](https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530/4 "2013-05-31T03:59:46Z")

</div>

I have a similar problem but the solution provided here 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?

On Friday, November 2, 2012 7:59:49 AM UTC-7, Amy wrote:

> Hi,  
> I have a mvel scoring script that refers to a field:
> 
> "(doc['myfield'].value == '1') ? 1 : 0)"
> 
> None of the documents in the type that I am searching have this field yet.  
> When I perform a search that includes this script I get a  
> QueryPhaseExecutionException:
> 
> ElasticSearchIllegalArgumentException[No field found for [myfield] in  
> mapping with types [type]];
> 
> As a workaround, whenever I add a document to a new type that I want to  
> search I have to add an 'initialiser' document that contains all the fields  
> referred to in my scoring script.  
> Is there a better way?

--  
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: ![patrick\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/patrick_2/32/2247_2.png) [@patrick\_2](https://discuss.elastic.co/u/patrick_2)
#### Post date: [May 31, 2013, 12:13pm UTC](https://discuss.elastic.co/t/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530/5 "2013-05-31T12:13:08Z")

</div>

On Friday, May 31, 2013 5:59:46 AM UTC+2, string theory wrote:

> "script" : "(doc.containsKey("my\_field") ? doc['my\_field].value : 0)"

Try:  
"script" : "(doc["my\_field"].empty ? 0 : doc['my\_field].value)"

This works on 20.x and 0.90.

Patrick

--  
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/referring-to-a-non-existent-field-in-an-elasticsearch-mvel-scoring-script/9530/6 "2017-07-06T02:33:37Z")

</div>


