Error in script


(nachiket) #1

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.


(nachiket) #2

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 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.


(Clinton Gormley) #3

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
http://www.elasticsearch.org/guide/reference/modules/scripting

clint


(Clinton Gormley) #4

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
http://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


(nachiket) #5

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 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


(nachiket) #6

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 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


(nachiket) #7

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

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 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


(nachiket) #8

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.


(nachiket) #9

I have put my gist recreation at https://gist.github.com/1067158.


(Clinton Gormley) #10

Hi Nachiket

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

I have put my gist recreation at https://gist.github.com/1067158.

With the gist, suddenly all becomes clear :slight_smile:

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' -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


(nachiket) #11

Thanks Clinton. It worked well.

On Jul 6, 6:27 pm, Clinton Gormley 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 :slight_smile:

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' -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


(rloth) #12

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

Romain


(system) #13