# Script\_fields is throwing Exception

**URL:** <https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498>\
**Category:** Elasticsearch\
**Created:** [July 14, 2015, 6:02am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498 "2015-07-14T06:02:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 6:02am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/1 "2015-07-14T06:02:56Z")

</div>

Hi All,  
Am having the data with mappings as shown:

"ymme\_type": {  
"\_all": {  
"auto\_boost": true,  
"index\_analyzer": "wordAnalyzer",  
"search\_analyzer": "whitespace\_analyzer"  
},  
"properties": {  
"Engine": {  
"type": "string",  
"index": "not\_analyzed"  
},  
"EngineCode": {  
"type": "string",  
"include\_in\_all": false  
},  
"Make": {  
"type": "string",  
"boost": 3,  
"index": "not\_analyzed",  
"norms": {  
"enabled": true  
}  
},  
"MakeCode": {  
"type": "string",  
"include\_in\_all": false  
},  
"Model": {  
"type": "string",  
"boost": 2,  
"index": "not\_analyzed",  
"norms": {  
"enabled": true  
}  
},  
"ModelCode": {  
"type": "string",  
"include\_in\_all": false  
},  
"ShortYear": {  
"type": "string",  
"boost": 4,  
"index": "not\_analyzed",  
"norms": {  
"enabled": true  
}  
},  
"Year": {  
"type": "string",  
"boost": 5,  
"index": "not\_analyzed",  
"norms": {  
"enabled": true  
},  
"fields": {  
"raw": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
},  
"YearCode": {  
"type": "string",  
"include\_in\_all": false  
}  
}  
}

And the relevant analyzers are as shown:

"analysis": {  
"analyzer": {  
"analyzer\_startswith": {  
"type": "custom",  
"filter": "lowercase",  
"tokenizer": "keyword"  
},  
"whitespace\_analyzer": {  
"type": "custom",  
"filter": [  
"lowercase",  
"asciifolding"  
],  
"tokenizer": "whitespace"  
},  
"wordAnalyzer": {  
"type": "custom",  
"filter": [  
"lowercase",  
"asciifolding",  
"nGram\_filter"  
],  
"tokenizer": "whitespace"  
}  
},  
"filter": {  
"nGram\_filter": {  
"max\_gram": "20",  
"min\_gram": "1",  
"type": "nGram",  
"token\_chars": [  
"letter",  
"punctuation",  
"symbol",  
"digit"  
]  
}  
}  
}

I would like to keep the scripting to particular fields and I have used the following query:

GET ymme/ymme\_type/\_search  
{  
"query": {  
"match": {  
"\_all":  
{  
"query": "2012",  
"operator": "and"  
}  
}  
},  
"script\_fields": {  
"year\_script": {  
"script": "doc['Year'].value\*2"  
}  
}  
}

It is throwing the following Exception:

"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[jlllRXEgTlKVYQa7rLvLNw][ymme][0]: SearchParseException[[ymme][0]: query[\_all:2012],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "match": {\n "\_all": \n {\n "query": "2012",\n "operator": "and"\n }\n }\n },\n "script\_fields": {\n "year\_script": {\n "script": "doc['Year'].value_2"\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[jlllRXEgTlKVYQa7rLvLNw][ymme][1]: SearchParseException[[ymme][1]: query[\_all:2012],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "match": {\n "\_all": \n {\n "query": "2012",\n "operator": "and"\n }\n }\n },\n "script\_fields": {\n "year\_script": {\n "script": "doc['Year'].value_2"\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[jlllRXEgTlKVYQa7rLvLNw][ymme][2]: SearchParseException[[ymme][2]: query[\_all:2012],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "match": {\n "\_all": \n {\n "query": "2012",\n "operator": "and"\n }\n }\n },\n "script\_fields": {\n "year\_script": {\n "script": "doc['Year'].value_2"\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[jlllRXEgTlKVYQa7rLvLNw][ymme][3]: SearchParseException[[ymme][3]: query[\_all:2012],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "match": {\n "\_all": \n {\n "query": "2012",\n "operator": "and"\n }\n }\n },\n "script\_fields": {\n "year\_script": {\n "script": "doc['Year'].value_2"\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }{[jlllRXEgTlKVYQa7rLvLNw][ymme][4]: SearchParseException[[ymme][4]: query[\_all:2012],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "match": {\n "\_all": \n {\n "query": "2012",\n "operator": "and"\n }\n }\n },\n "script\_fields": {\n "year\_script": {\n "script": "doc['Year'].value\*2"\n }\n }\n}\n]]]; nested: ScriptException[dynamic scripting for [groovy] disabled]; }]",  
"status": 400

May I know what is the reason??????, my intention is to get the response based on the scripting that I have used but not based on the default Lucene Score ..

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 6:34am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/2 "2015-07-14T06:34:22Z")

</div>

Can anyone help me out when to use "script\_fields" and for what purpose and how to use it with an example...

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 7:12am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/3 "2015-07-14T07:12:45Z")

</div>

Hi I have used the following query:

GET /ymme/ymme\_type/\_search?pretty  
{  
"query": {  
"match": {  
"Make": "Acura"  
}  
},  
"script\_fields": {  
"test1": {  
"script": "\_source.Make\*2"  
}  
}  
}

Even this query is throwing the exception, am I doing any thing wrong or do I need to set anything inorder to make use of the script\_fields in search query??????

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 14, 2015, 9:00am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/4 "2015-07-14T09:00:22Z")

</div>

By default you cannot run dynamic groovy scripts (script specified inline in a request or indexed scripts) on Elasticsearch. This is for security so Elasticsearch clusters are not by default left exposed to security vunerabilities due to malicious scripts. Please read [https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#modules-scripting](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#modules-scripting) for more information on this as well as instructions on how use scripts without dynamic scripting (file based scripts) and how to enabled dynamic scripting.

Please read the above link in full and carefully as it directly affects the security of your cluster.

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 9:13am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/5 "2015-07-14T09:13:35Z")

</div>

Hi Colin,

For that I have added the below statement in elasticsearch.yml file  
script.disable\_dynamic: false  
So that I am able to get the response for the **"script\_fields"** but when am trying the query..

GET /ymme/ymme\_type/\_search?pretty  
{  
"query": {  
"function\_score": {  
"query": {  
"match": {  
"\_all": {  
"query": "2012",  
"operator": "and"  
}  
}  
},  
"functions": [  
{  
"script\_score": {  
"script": "\_score\*doc['Year'].value"  
}  
}  
]  
}  
}  
}

Am getting Exception as:

"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[eddpyS7lQ2Gzj0NWQr8dKw][ymme][0]: QueryPhaseExecutionException[[ymme][0]: query[filtered(function score (\_all:2012,function=script[\_score_doc['Year'].value], params [null]))-\>cache(\_type:ymme\_type)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.script.ScoreAccessor.multiply() is applicable for argument types: (java.lang.String) values: [2012]\nPossible solutions: multiply(java.lang.Character), multiply(java.lang.Number)]]; }{[eddpyS7lQ2Gzj0NWQr8dKw][ymme][1]: QueryPhaseExecutionException[[ymme][1]: query[filtered(function score (\_all:2012,function=script[\_score_doc['Year'].value], params [null]))-\>cache(\_type:ymme\_type)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.script.ScoreAccessor.multiply() is applicable for argument types: (java.lang.String) values: [2012]\nPossible solutions: multiply(java.lang.Character), multiply(java.lang.Number)]]; }{[eddpyS7lQ2Gzj0NWQr8dKw][ymme][2]: QueryPhaseExecutionException[[ymme][2]: query[filtered(function score (\_all:2012,function=script[\_score_doc['Year'].value], params [null]))-\>cache(\_type:ymme\_type)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.script.ScoreAccessor.multiply() is applicable for argument types: (java.lang.String) values: [2012]\nPossible solutions: multiply(java.lang.Character), multiply(java.lang.Number)]]; }{[eddpyS7lQ2Gzj0NWQr8dKw][ymme][3]: QueryPhaseExecutionException[[ymme][3]: query[filtered(function score (\_all:2012,function=script[\_score_doc['Year'].value], params [null]))-\>cache(\_type:ymme\_type)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.script.ScoreAccessor.multiply() is applicable for argument types: (java.lang.String) values: [2012]\nPossible solutions: multiply(java.lang.Character), multiply(java.lang.Number)]]; }{[eddpyS7lQ2Gzj0NWQr8dKw][ymme][4]: QueryPhaseExecutionException[[ymme][4]: query[filtered(function score (\_all:2012,function=script[\_score\*doc['Year'].value], params [null]))-\>cache(\_type:ymme\_type)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: GroovyScriptExecutionException[MissingMethodException[No signature of method: org.elasticsearch.script.ScoreAccessor.multiply() is applicable for argument types: (java.lang.String) values: [2012]\nPossible solutions: multiply(java.lang.Character), multiply(java.lang.Number)]]; }]",  
"status": 500

Unable to understand this is the query that am using is Wrong??

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 14, 2015, 9:18am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/6 "2015-07-14T09:18:29Z")

</div>

> [@anusha6](#):
>
> "script": "\_score\*doc['Year'].value"

> [@anusha6](#):
>
> No signature of method: org.elasticsearch.script.ScoreAccessor.multiply() is applicable for argument types: (java.lang.String) values: [2012]\nPossible solutions: multiply(java.lang.Character), multiply(java.lang.Number)]]; }]",

It looks like your `year` field is mapped as a String instead of an integer. The exception is saying that it cannot multiple a number (`_score`) by a String (`year`)

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 9:26am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/7 "2015-07-14T09:26:12Z")

</div>

Then how can I achieve dynamic scoring, and my intention is to change the scoring and the response need to come by giving more priority to Year field.

When am using the following query:

"query": {  
"match": {  
"\_all": {  
"query": "2012",  
"operator": "and",  
"fuzziness": 1,  
"prefix\_length": 1  
}  
}  
}  
The fuzziness is changing the response, and am getting the records with some other years like 1991, 1992, 1993,......  
For that I have moved to make use of dynamic scoring as default scoring is not behaving like expected..

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 14, 2015, 9:41am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/8 "2015-07-14T09:41:23Z")

</div>

As I said before, you will need your `year` field to be indexed as a number not as a string. This will mean you will need to re-index your data into an index with [the mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) set appropriately.

You may also want to look at the [field value factor function score](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor) which will allow you to multiply the score by the value of `year` without using a script. You will still need `year` to be mapped as an integer for this though.

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 9:54am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/9 "2015-07-14T09:54:13Z")

</div>

Hi Colin,  
changed mappings for that fields as:  
"Year": {  
"type": "integer",  
"boost": 4,  
"index": "not\_analyzed",  
"norms": {  
"enabled": true  
}  
},

And re-indexed the data again, this time I got the Exception as:

"error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[eddpyS7lQ2Gzj0NWQr8dKw][ymme1][0]: QueryPhaseExecutionException[[ymme1][0]: query[filtered(function score (\_all:2012,function=org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction@70240bcf))-\>cache(\_type:ymme\_type)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: ElasticsearchException[java.lang.NumberFormatException: Invalid shift value in prefixCoded bytes (is encoded value really an INT?)]; nested: UncheckedExecutionException[java.lang.NumberFormatException: Invalid shift value in prefixCoded bytes (is encoded value really an INT?)];

Query I have used:

GET /ymme1/ymme\_type/\_search  
{  
"query": {  
"function\_score": {  
"query": {  
"match": {  
"\_all":  
{  
"query": "2012",  
"operator": "and"  
}  
}  
},  
"field\_value\_factor": {  
"field": "Year",  
"factor":2  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 10:03am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/10 "2015-07-14T10:03:47Z")

</div>

And one more thing like the **"field\_value\_factor"** which uses **Year** field, will this boost the scoring for the **Year** field as my query will have a combination of different fields, the search will took place in 4 fields like Year, Make ,Model , Engine but priority should be given to the Year field then to Make, Model and Engine..

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 14, 2015, 12:37pm UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/11 "2015-07-14T12:37:22Z")

</div>

Hi Is there any solution for the requirement that I have mentioned?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 14, 2015, 10:00pm UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/12 "2015-07-14T22:00:55Z")

</div>

> [@anusha6](#):
>
> "Year": { "type": "integer", "boost": 4, "index": "not\_analyzed", "norms": { "enabled": true } },

Change your mapping for `Year` to be:

```json
"Year": {
"type": "integer",
"boost": 4
}
}

```

The other settings don't apply to non-string types.

> [@anusha6](#):
>
> And one more thing like the "field\_value\_factor" which uses Year field, will this boost the scoring for the Year field as my query will have a combination of different fields, the search will took place in 4 fields like Year, Make ,Model , Engine but priority should be given to the Year field then to Make, Model and Engine..

The `field_value_factor` will boost the score of all documents which match the query by the factor multiplied by the value of 'Year'. This boost will happen regardless of which field is actually matched in the query. More information is availbale here: [Function score query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor)

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 15, 2015, 5:23am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/13 "2015-07-15T05:23:41Z")

</div>

But how can I make my fuzziness to work as expected?

Am using the query :  
"query": {  
"match": {  
"\_all": {  
"query": "2012",  
"operator": "and"  
}  
}  
}

My response looks like this:

2012 AM GENERAL-VPG MV-1 V8-281 4.6L SOHC  
2012 CHEVROLET CAMARO All Engine  
2012 CHEVROLET CAMARO v6-3564 3.6L DOHC  
2012 CHEVROLET CAMARO V8-376 6.2L  
2012 LAMBORGHINI AVENTADOR 12-654 6.5L DOHC  
2012 LAMBORGHINI GALLARDO 10-520 DOHC

But when am adding fuzziness to my query am getting response as:(example data)

"query": {  
"match": {  
"\_all": {  
"query": "2012",  
"operator": "and"  
"fuzziness": 1,  
"prefix\_length": 1  
}  
}  
}  
Response:

1990 AM GENERAL-VPG MV-1 V8-281 4.6L SOHC  
1991 CHEVROLET CAMARO All Engine  
1994 CHEVROLET CAMARO v6-3564 3.6L DOHC  
1997 CHEVROLET CAMARO V8-376 6.2L  
2000 LAMBORGHINI AVENTADOR 12-654 6.5L DOHC  
2011 LAMBORGHINI GALLARDO 10-520 DOHC

And even I tried by keeping prefix\_length as 4 but even for that few responses are not matching.

I dont know why am getting this irrelevant data? May be score based response is making like this I have thought, so for that I have chosen this dynamic scoring.  
May I know A better solution for my issue???

---

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [July 16, 2015, 10:57am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/14 "2015-07-16T10:57:15Z")

</div>

Hi Colin,

Thanks for your response and I found a good solution for to preserve my fuzziness and the year data preference with a new query that I have implemented as shown:

GET ymme/ymme\_type/\_search  
{  
"size": 10,  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"\_all": {  
"query": "aston,, ma",  
"operator": "and",  
"fuzziness": 2,  
"prefix\_length": 1  
}  
}  
},  
{  
"query\_string": {  
"fields": [  
"Year^4","ShortYear^5","Model^3","Engine^2"  
],  
"query": "_70_ AND _95_"  
}  
}

```
     ]
  }

```

}  
}  
By this I can able to apply fuzziness and even my intention to give priority to year field is working fine..  
Thanks for your support...

---

<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, 12:01am UTC](https://discuss.elastic.co/t/script-fields-is-throwing-exception/25498/15 "2017-07-06T00:01:02Z")

</div>


