# Error in script

**URL:** https://discuss.elastic.co/t/error-in-script/4787
**Category:** Elasticsearch
**Created:** [July 6, 2011, 8:05am UTC](https://discuss.elastic.co/t/error-in-script/4787 "2011-07-06T08:05:55Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 8:05am UTC](https://discuss.elastic.co/t/error-in-script/4787/1 "2011-07-06T08:05:55Z")

</div>

Hi.  
I want to use custom score query for my scoring.

I am creating a new type script in index.

curl -XPUT localhost:9200/twitter/script/1 -d ' {"user" : 1}'

Now my custom query is  
curl -XGET localhost:9200/twitter/script/\_search?pretty=true -d '  
{  
"query":  
{  
"custom\_score" : {  
"query" : {  
"match\_all" : {}  
},  
"script" : "\_score \* doc['user']"  
}  
}  
}'

It throws error as  
QueryPhaseExecutionException[[twitter][2]: query[custom score  
(ConstantScore(_:_),function=org.elasticsearch.index.query.xcontent.CustomScoreQueryParser  
$ScriptScoreFunction@13ff9d4)],from[0],size[10]: Query Failed [Failed  
to execute main query]]; nested: CompileException[[Error: No field  
found for  
[org.elasticsearch.index.field.data.strings.StringDocFieldData@2a09a0]]  
\n[Near : {... \_score \* doc[user] ....}]\n ^\n[Line: 1,  
Column: 1]]; nested: ElasticSearchIllegalArgumentException[No field  
found for  
[org.elasticsearch.index.field.data.strings.StringDocFieldData@2a09a0]];

Now if I want to use \_source.user then it gives correct output. Can  
somebody tell me what is the difference in doc['field\_name'] and  
\_source.field\_name and when to use it?

Can I use non-numeric values in source.field\_name and use functions on  
them like .toString.length()

Regards.

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 8:09am UTC](https://discuss.elastic.co/t/error-in-script/4787/2 "2011-07-06T08:09:13Z")

</div>

Also what happens if the field is not present? Does Elastic Search  
throw any error?

On Jul 6, 1:05 pm, nachiket bhagwat [nachiketbhag...@gmail.com](mailto:nachiketbhag...@gmail.com) wrote:

> Hi.  
> I want to use custom score query for my scoring.
> 
> I am creating a new type script in index.
> 
> curl -XPUT localhost:9200/twitter/script/1 -d ' {"user" : 1}'
> 
> Now my custom query is  
> curl -XGET localhost:9200/twitter/script/\_search?pretty=true -d '  
> {  
> "query":  
> {  
> "custom\_score" : {  
> "query" : {  
> "match\_all" : {}  
> },  
> "script" : "\_score \* doc['user']"
> 
> }  
> }  
> }'
> 
> It throws error as  
> QueryPhaseExecutionException[[twitter][2]: query[custom score  
> (ConstantScore(_:_),function=org.elasticsearch.index.query.xcontent.CustomScoreQueryParser  
> $ScriptScoreFunction@13ff9d4)],from[0],size[10]: Query Failed [Failed  
> to execute main query]]; nested: CompileException[[Error: No field  
> found for  
> [org.elasticsearch.index.field.data.strings.StringDocFieldData@2a09a0]]  
> \n[Near : {... \_score \* doc[user] ....}]\n ^\n[Line: 1,  
> Column: 1]]; nested: ElasticSearchIllegalArgumentException[No field  
> found for  
> [org.elasticsearch.index.field.data.strings.StringDocFieldData@2a09a0]];
> 
> Now if I want to use \_source.user then it gives correct output. Can  
> somebody tell me what is the difference in doc['field\_name'] and  
> \_source.field\_name and when to use it?
> 
> Can I use non-numeric values in source.field\_name and use functions on  
> them like .toString.length()
> 
> Regards.

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [July 6, 2011, 9:30am UTC](https://discuss.elastic.co/t/error-in-script/4787/3 "2011-07-06T09:30:00Z")

</div>

Hi Nachiket

> Now if I want to use \_source.user then it gives correct output. Can  
> somebody tell me what is the difference in doc['field\_name'] and  
> \_source.field\_name and when to use it?
> 
> Can I use non-numeric values in source.field\_name and use functions on  
> them like .toString.length()

Have a look at

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [July 6, 2011, 10:14am UTC](https://discuss.elastic.co/t/error-in-script/4787/4 "2011-07-06T10:14:45Z")

</div>

Hi Nachiket

Please keep the conversation on the list.

On Wed, 2011-07-06 at 02:58 -0700, nachiket bhagwat wrote:

> Hi. I have looked at the module already but could not understand it.  
> My question is if my script is like  
> {"script": "\_score + \_source.email.length"} then it throws an error in  
> documents that do not contain that field and do not retreive the docs  
> even if the query is working.

If you look at the docs on

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

you will see that it uses "doc['fieldname'].value" and variants.

So for you example above, try:

"\_score + doc['email'].stringValue.length()/10"

> Here I just want to define priority of documents and hence I am using  
> '+' in script to change scoring function. But it is changing my  
> definition of relevant and not relevant.  
> How can I do above thing? Is it possible in scripting to retrive docs  
> even if some fields have null values??

I'm not sure if stringValue will take care of the null values or not.  
If it doesn't then check if that field has a value before applying the  
rest of the calculation.

> My final question is what does doc['field\_name'] means? If the doc  
> means current doc can I use it in any script? It is not working as I  
> stated in earlier message. Also can I access like  
> doc['obj1.field\_name']?

yes and yes.

clint

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 10:43am UTC](https://discuss.elastic.co/t/error-in-script/4787/5 "2011-07-06T10:43:22Z")

</div>

Hi.  
Thanks for the help.  
But I am still getting same error on the last point.  
If I use \_souce.field\_name then it gives output correctly but instead  
if I use doc['field\_name'] (Even without nesting) then it throws an  
error.

nested: ElasticSearchIllegalArgumentException  
[No field found for

[org.elasticsearch.index.field.data.strings.StringDocFieldData@1d08960  
]  
]

I am not getting difference between those. In documentation there is a  
point that  
"In all scripts that can be used in facets, allow to access the  
current doc score using doc.score"

Does this mean there are only some of scripts where I can use  
doc['field\_name'] and its variants?

Thanks for the help in advance.  
Regards.

On Jul 6, 3:14 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Nachiket
> 
> Please keep the conversation on the list.
> 
> On Wed, 2011-07-06 at 02:58 -0700, nachiket bhagwat wrote:
> 
> > Hi. I have looked at the module already but could not understand it.  
> > My question is if myscriptis like  
> > {"script": "\_score + \_source.email.length"} then it throws anerrorin  
> > documents that do not contain that field and do not retreive the docs  
> > even if the query is working.
> 
> If you look at the docs onhttp://www.elasticsearch.org/guide/reference/modules/scripting  
> you will see that it uses "doc['fieldname'].value" and variants.
> 
> So for you example above, try:
> 
> "\_score + doc['email'].stringValue.length()/10"
> 
> > Here I just want to define priority of documents and hence I am using  
> > '+' inscriptto change scoring function. But it is changing my  
> > definition of relevant and not relevant.  
> > How can I do above thing? Is it possible in scripting to retrive docs  
> > even if some fields have null values??
> 
> I'm not sure if stringValue will take care of the null values or not.  
> If it doesn't then check if that field has a value before applying the  
> rest of the calculation.
> 
> > My final question is what does doc['field\_name'] means? If the doc  
> > means current doc can I use it in anyscript? It is not working as I  
> > stated in earlier message. Also can I access like  
> > doc['obj1.field\_name']?
> 
> yes and yes.
> 
> clint

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 11:42am UTC](https://discuss.elastic.co/t/error-in-script/4787/6 "2011-07-06T11:42:16Z")

</div>

Hi.  
My script works if I am using \_source.field\_name but doesn't work if I  
use doc['field\_name'] (or its variants as stated in documentation).  
Can somebody tell me if there is any difference in these two?  
If yes how can I use functions like distance() etc using  
\_source.field\_name ?

Regards.

On Jul 6, 3:14 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Nachiket
> 
> Please keep the conversation on the list.
> 
> On Wed, 2011-07-06 at 02:58 -0700, nachiket bhagwat wrote:
> 
> > Hi. I have looked at the module already but could not understand it.  
> > My question is if my script is like  
> > {"script": "\_score + \_source.email.length"} then it throws an error in  
> > documents that do not contain that field and do not retreive the docs  
> > even if the query is working.
> 
> If you look at the docs onhttp://www.elasticsearch.org/guide/reference/modules/scripting  
> you will see that it uses "doc['fieldname'].value" and variants.
> 
> So for you example above, try:
> 
> "\_score + doc['email'].stringValue.length()/10"
> 
> > Here I just want to define priority of documents and hence I am using  
> > '+' in script to change scoring function. But it is changing my  
> > definition of relevant and not relevant.  
> > How can I do above thing? Is it possible in scripting to retrive docs  
> > even if some fields have null values??
> 
> I'm not sure if stringValue will take care of the null values or not.  
> If it doesn't then check if that field has a value before applying the  
> rest of the calculation.
> 
> > My final question is what does doc['field\_name'] means? If the doc  
> > means current doc can I use it in any script? It is not working as I  
> > stated in earlier message. Also can I access like  
> > doc['obj1.field\_name']?
> 
> yes and yes.
> 
> clint

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 11:49am UTC](https://discuss.elastic.co/t/error-in-script/4787/7 "2011-07-06T11:49:33Z")

</div>

Hi. I want to write my own scripts in java.

> <https://gist.github.com/tsxn26/1043766>

What I can't figure out is how scripts work.  
I mean \_source.field\_name works for a field but instead if I use  
doc['field\_name'] it throws an error like No field found for  
'field\_name'.  
Is there any difference between those too on logical level?

If yes how can I use functions like getDistance etc?

On Jul 6, 3:14 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Nachiket
> 
> Please keep the conversation on the list.
> 
> On Wed, 2011-07-06 at 02:58 -0700, nachiket bhagwat wrote:
> 
> > Hi. I have looked at the module already but could not understand it.  
> > My question is if myscriptis like  
> > {"script": "\_score + \_source.email.length"} then it throws anerrorin  
> > documents that do not contain that field and do not retreive the docs  
> > even if the query is working.
> 
> If you look at the docs onhttp://www.elasticsearch.org/guide/reference/modules/scripting  
> you will see that it uses "doc['fieldname'].value" and variants.
> 
> So for you example above, try:
> 
> "\_score + doc['email'].stringValue.length()/10"
> 
> > Here I just want to define priority of documents and hence I am using  
> > '+' inscriptto change scoring function. But it is changing my  
> > definition of relevant and not relevant.  
> > How can I do above thing? Is it possible in scripting to retrive docs  
> > even if some fields have null values??
> 
> I'm not sure if stringValue will take care of the null values or not.  
> If it doesn't then check if that field has a value before applying the  
> rest of the calculation.
> 
> > My final question is what does doc['field\_name'] means? If the doc  
> > means current doc can I use it in anyscript? It is not working as I  
> > stated in earlier message. Also can I access like  
> > doc['obj1.field\_name']?
> 
> yes and yes.
> 
> clint

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 12:22pm UTC](https://discuss.elastic.co/t/error-in-script/4787/8 "2011-07-06T12:22:15Z")

</div>

Hi.  
I was not able to post in my earlier post Errot in script. So I am  
starting new discussion.  
I want to understand basic difference between doc['field\_name'] ,  
\_fields['field\_name'] and \_source.field\_name.

My query works for \_source.field\_name but same query when  
doc['field\_name'] is used throws an error named  
"ElasticSearchIllegalArgumentException : No field found for ......"

So there must be some difference between those three.  
Can somebody help me with this issue?

Regards.

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 12:57pm UTC](https://discuss.elastic.co/t/error-in-script/4787/9 "2011-07-06T12:57:07Z")

</div>

I have put my gist recreation at [https://gist.github.com/1067158](https://gist.github.com/1067158).

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [July 6, 2011, 1:27pm UTC](https://discuss.elastic.co/t/error-in-script/4787/10 "2011-07-06T13:27:38Z")

</div>

Hi Nachiket

On Wed, 2011-07-06 at 05:57 -0700, nachiket bhagwat wrote:

> I have put my gist recreation at [I am getting error in using a simple script. Can anybody take a look at what is wrong? I want to use doc['field\_name'] but it is throwing error. I want to use doc['field\_name'] in my code. · GitHub](https://gist.github.com/1067158).

With the gist, suddenly all becomes clear 🙂

The ' around the field name are interfering with the ' wrapping your  
whole query.

Try this: (embedded ' encoded as \u0027)

curl -XGET '[http://127.0.0.1:9200/twitter/script/\_search?pretty=1](http://127.0.0.1:9200/twitter/script/_search?pretty=1)' -d '  
{  
"query" : {  
"custom\_score" : {  
"script" : "1.0 + doc[\u0027user\u0027].value",  
"query" : {  
"match\_all" : {}  
}  
}  
}  
}  
'

And regarding your question about \_source vs doc, it is all explained  
on the scripting page that i linked you to

clint

---

<div class="post-metadata">

### Author: ![nachiket](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@nachiket](https://discuss.elastic.co/u/nachiket)
#### Post date: [July 6, 2011, 2:45pm UTC](https://discuss.elastic.co/t/error-in-script/4787/11 "2011-07-06T14:45:20Z")

</div>

Thanks Clinton. It worked well.

On Jul 6, 6:27 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Nachiket
> 
> On Wed, 2011-07-06 at 05:57 -0700, nachiket bhagwat wrote:
> 
> > I have put my gist recreation athttps://gist.github.com/1067158.
> 
> With the gist, suddenly all becomes clear 🙂
> 
> The ' around the field name are interfering with the ' wrapping your  
> whole query.
> 
> Try this: (embedded ' encoded as \u0027)
> 
> curl -XGET '[http://127.0.0.1:9200/twitter/script/\_search?pretty=1](http://127.0.0.1:9200/twitter/script/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "custom\_score" : {  
> "script" : "1.0 + doc[\u0027user\u0027].value",  
> "query" : {  
> "match\_all" : {}  
> }  
> }  
> }}
> 
> '
> 
> And regarding your question about \_source vs doc, it is all explained  
> on the scripting page that i linked you to
> 
> clint

---

<div class="post-metadata">

### Author: ![rloth](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@rloth](https://discuss.elastic.co/u/rloth)
#### Post date: [April 24, 2014, 3:00pm UTC](https://discuss.elastic.co/t/error-in-script/4787/12 "2014-04-24T15:00:17Z")

</div>

Thanks both... I had the same problem

IMHO this \u0027 trick should be mentioned in the docs around [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html)

Romain

---

<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, 1:33am UTC](https://discuss.elastic.co/t/error-in-script/4787/13 "2017-07-06T01:33:43Z")

</div>


