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

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?

--

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 APIhttp://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?

--

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 APIhttp://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?

--

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.
For more options, visit https://groups.google.com/groups/opt_out.