# Accessing doc score in a native (Java) script

**URL:** <https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853>\
**Category:** Elasticsearch\
**Created:** [November 27, 2012, 10:17pm UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853 "2012-11-27T22:17:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Hill](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@Ryan\_Hill](https://discuss.elastic.co/u/Ryan_Hill)\
**Post date:** [November 27, 2012, 10:17pm UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/1 "2012-11-27T22:17:55Z")

</div>

It seems that under normal search conditions, that calling doc().score()  
from a Java script will result in an uncaught exception (since the scorer  
is null).

For instance, a simple query on the twitter example data:  
'{ "query": {"text": {"message":"elastic"}}, "script\_fields": {}}

The above executes without an error until I attempt to access the score.  
When handling the exception myself, the '\_score' and 'max\_score' fields  
have the expected values in the response.

Any advice would be most appreciated.

Cheers,  
-R

--

---

<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 29, 2012, 12:04am UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/2 "2012-11-29T00:04:36Z")

</div>

The script\_field is executed during fetch phase when scorer is not longer  
used (it already performed its function during search phase). But why do  
you need score in the script\_field?

On Tuesday, November 27, 2012 5:17:55 PM UTC-5, Ryan Hill wrote:

> It seems that under normal search conditions, that calling doc().score()  
> from a Java script will result in an uncaught exception (since the scorer  
> is null).
> 
> For instance, a simple query on the twitter example data:  
> '{ "query": {"text": {"message":"elastic"}}, "script\_fields": {}}
> 
> The above executes without an error until I attempt to access the score.  
> When handling the exception myself, the '\_score' and 'max\_score' fields  
> have the expected values in the response.
> 
> Any advice would be most appreciated.
> 
> Cheers,  
> -R

--

---

<div class="post-metadata">

**Author:** ![Klaus\_Brunner](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Klaus\_Brunner](https://discuss.elastic.co/u/Klaus_Brunner)\
**Post date:** [January 29, 2013, 9:43am UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/3 "2013-01-29T09:43:05Z")

</div>

Sorry for resurrecting an old thread, but we currently have exactly the  
same issue. In a Java script derived from Abstract\*SearchScript, we need to  
calculate a field based on (among other things) the score as calculated by  
ES.

I understand that the scorer is null because it's no longer needed, but  
isn't there any other way to get the score that was calculated by it  
earlier?

Thanks

Klaus

On Thursday, 29 November 2012 01:04:36 UTC+1, Igor Motov wrote:

> The script\_field is executed during fetch phase when scorer is not longer  
> used (it already performed its function during search phase). But why do  
> you need score in the script\_field?
> 
> On Tuesday, November 27, 2012 5:17:55 PM UTC-5, Ryan Hill wrote:
> 
> > It seems that under normal search conditions, that calling doc().score()  
> > from a Java script will result in an uncaught exception (since the scorer  
> > is null).

--  
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:** ![egaumer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egaumer/32/2365_2.png) [@egaumer](https://discuss.elastic.co/u/egaumer)\
**Post date:** [January 29, 2013, 2:05pm UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/4 "2013-01-29T14:05:02Z")

</div>

Rather than calling doc().score() try calling score().

-Eric

On Tuesday, January 29, 2013 4:43:05 AM UTC-5, Klaus Brunner wrote:

> Sorry for resurrecting an old thread, but we currently have exactly the  
> same issue. In a Java script derived from Abstract\*SearchScript, we need to  
> calculate a field based on (among other things) the score as calculated by  
> ES.
> 
> I understand that the scorer is null because it's no longer needed, but  
> isn't there any other way to get the score that was calculated by it  
> earlier?
> 
> Thanks
> 
> Klaus
> 
> On Thursday, 29 November 2012 01:04:36 UTC+1, Igor Motov wrote:
> 
> > The script\_field is executed during fetch phase when scorer is not longer  
> > used (it already performed its function during search phase). But why do  
> > you need score in the script\_field?
> > 
> > On Tuesday, November 27, 2012 5:17:55 PM UTC-5, Ryan Hill wrote:
> > 
> > > It seems that under normal search conditions, that calling doc().score()  
> > > from a Java script will result in an uncaught exception (since the scorer  
> > > is null).

--  
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:** ![Klaus\_Brunner](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Klaus\_Brunner](https://discuss.elastic.co/u/Klaus_Brunner)\
**Post date:** [January 29, 2013, 2:38pm UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/5 "2013-01-29T14:38:44Z")

</div>

On Tuesday, 29 January 2013 15:05:02 UTC+1, egaumer wrote:

> Rather than calling doc().score() try calling score().

We did. It's NaN, just the way the score field is initialised in  
AbstractSearchScript. Seems it's never touched. (We also set  
track\_scores=true, to make sure, even though we don't use sorting.)

The query works and goes through. The script is called and works fine,  
except when we want it to read the score. We also tried switching  
explanations on and off, no effect.

This is on 0.20.2, but from earlier testing 0.19.7 showed the same  
behaviour.

Klaus

--  
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:** ![egaumer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/egaumer/32/2365_2.png) [@egaumer](https://discuss.elastic.co/u/egaumer)\
**Post date:** [January 29, 2013, 3:05pm UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/6 "2013-01-29T15:05:59Z")

</div>

I have a native script in front of me that uses the score and works fine on  
0.20.2 but digging deeper...

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/script/AbstractSearchScript.java#L47](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/script/AbstractSearchScript.java#L47)

Seems like this only works if you're using a CustomScoreQuery (which I am).  
You should try wrapping your current query.

On Tuesday, January 29, 2013 9:38:44 AM UTC-5, Klaus Brunner wrote:

> On Tuesday, 29 January 2013 15:05:02 UTC+1, egaumer wrote:
> 
> > Rather than calling doc().score() try calling score().
> 
> We did. It's NaN, just the way the score field is initialised in  
> AbstractSearchScript. Seems it's never touched. (We also set  
> track\_scores=true, to make sure, even though we don't use sorting.)
> 
> The query works and goes through. The script is called and works fine,  
> except when we want it to read the score. We also tried switching  
> explanations on and off, no effect.
> 
> This is on 0.20.2, but from earlier testing 0.19.7 showed the same  
> behaviour.
> 
> Klaus

--  
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:** ![Klaus\_Brunner](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Klaus\_Brunner](https://discuss.elastic.co/u/Klaus_Brunner)\
**Post date:** [January 29, 2013, 3:19pm UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/7 "2013-01-29T15:19:10Z")

</div>

On Tuesday, 29 January 2013 16:05:59 UTC+1, egaumer wrote:

> I have a native script in front of me that uses the score and works fine  
> on 0.20.2 but digging deeper...
> 
> [https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/script/AbstractSearchScript.java#L47](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/script/AbstractSearchScript.java#L47)
> 
> Seems like this only works if you're using a CustomScoreQuery (which I  
> am). You should try wrapping your current query.
> 
> >

We're using a script\_field, just like the OP.

The problem is that we don't want to change the score, but to set a new  
field based on the score and other stuff. This doesn't seem to work from a  
CustomScoreQuery, but from a script\_field it does. However, it seems that  
the score is never set when running a script as part of a script\_field...

So, actually I'm looking for either

- a way to get the score in a Java script called from a script\_field, or
- a way to set a field from a Java script called from a CustomScoreQuery.

Klaus

--  
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:** ![Klaus\_Brunner](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Klaus\_Brunner](https://discuss.elastic.co/u/Klaus_Brunner)\
**Post date:** [February 6, 2013, 9:11am UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/8 "2013-02-06T09:11:46Z")

</div>

After some digging in the code, it seems that our application will  
definitely require a CustomScoreQuery: we're calculating a new score based  
on the "original" score and some other stuff. And it makes sense to  
actually sort on this new score.

However, we still need a way to get at the original score from the query  
wrapped by the CustomScoreQuery. My best guess so far is using the explain  
option, but that seems like overkill: we don't need the detailed info on  
all queries below, and it seems that explain will slow things down  
significantly. All we need is that one additional "original score" field  
for each returned hit.

Are there any other clever ideas on getting this value out?

Thanks

Klaus

--  
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:52am UTC](https://discuss.elastic.co/t/accessing-doc-score-in-a-native-java-script/9853/9 "2017-07-06T02:52:48Z")

</div>


