# Score of a Query

**URL:** <https://discuss.elastic.co/t/score-of-a-query/4978>\
**Category:** Elasticsearch\
**Created:** [July 27, 2011, 4:38pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978 "2011-07-27T16:38:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [July 27, 2011, 4:38pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978/1 "2011-07-27T16:38:12Z")

</div>

Hi,

Is there anyway I can ask for the score of each document returned to  
be added to the results in a query, I'm finding it hard to debug the  
ordering of my results.

Best Regards,

David.

---

<div class="post-metadata">

**Author:** ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)\
**Post date:** [July 27, 2011, 4:49pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978/2 "2011-07-27T16:49:58Z")

</div>

You should use the explain parameter (  
[http://www.elasticsearch.org/guide/reference/api/search/explain.html](http://www.elasticsearch.org/guide/reference/api/search/explain.html)). This  
will display score for each result and a detailed explanation of the score  
computation.

---

<div class="post-metadata">

**Author:** ![ppearcy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppearcy/32/980_2.png) [@ppearcy](https://discuss.elastic.co/u/ppearcy)\
**Post date:** [July 27, 2011, 4:51pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978/3 "2011-07-27T16:51:01Z")

</div>

Unless I am misunderstanding the question, this comes back in the  
result JSON as:  
hits.hits[n].\_score

If relevancy based scoring isn't used, this comes back as null.

On Jul 27, 10:38 am, davrob2 [davirobe...@gmail.com](mailto:davirobe...@gmail.com) wrote:

> Hi,
> 
> Is there anyway I can ask for the score of each document returned to  
> be added to the results in a query, I'm finding it hard to debug the  
> ordering of my results.
> 
> Best Regards,
> 
> David.

---

<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 27, 2011, 4:52pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978/4 "2011-07-27T16:52:58Z")

</div>

Hi David

> Is there anyway I can ask for the score of each document returned to  
> be added to the results in a query, I'm finding it hard to debug the  
> ordering of my results.

It is added to each result, unless you're sorting by something other  
than \_score

clint

---

<div class="post-metadata">

**Author:** ![Benjamin\_Deveze](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_deveze/32/1577_2.png) [@Benjamin\_Deveze](https://discuss.elastic.co/u/Benjamin_Deveze)\
**Post date:** [July 27, 2011, 5:03pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978/5 "2011-07-27T17:03:20Z")

</div>

On a side note if you are sorting by something other than \_score you should  
have a look to the track\_scores parameter.

---

<div class="post-metadata">

**Author:** ![davrob](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@davrob](https://discuss.elastic.co/u/davrob)\
**Post date:** [August 1, 2011, 11:51am UTC](https://discuss.elastic.co/t/score-of-a-query/4978/6 "2011-08-01T11:51:18Z")

</div>

Hi Guys,

Thanks for your answers. This is what I have managed to get done so  
far:

i) The equivalent of setting explain: true in the url params is to  
setExplain(true) on the SearchRequestIndexBuilder

ii) When I set explain true, this is a sample of what I get from  
SearchHit.explanation().toString():

1.2891362 = weight(listIds:6836 in 796), product of:  
0.99999994 = queryWeight(listIds:6836), product of:  
10.31309 = idf(docFreq=6, maxDocs=77575)  
0.09696414 = queryNorm  
1.2891363 = fieldWeight(listIds:6836 in 796), product of:  
1.0 = tf(termFreq(listIds:6836)=1)  
10.31309 = idf(docFreq=6, maxDocs=77575)  
0.125 = fieldNorm(field=listIds, doc=796)

A guide of what the above means would be really useful.

i.e. What is "idf" / doc frequency referring to?  
What is "queryNorm", "fieldWeight", "tf"/term frequency and  
"fieldNorm"?

iii) Whether I set explain(true) or not score() method on SearchHit  
always return NaN:

for (SearchHit hit : queryResponse.getHits()){  
Map\<String, Object\> source = hit.getSource();  
source.put("score", hit.score());  
...  
}

I think this is fine for me, to set explain(true) when I need to debug  
my results.

cheers,

David.

On Jul 27, 6:03 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:

> On a side note if you are sorting by something other than \_score you should  
> have a look to the track\_scores parameter.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [August 1, 2011, 8:18pm UTC](https://discuss.elastic.co/t/score-of-a-query/4978/7 "2011-08-01T20:18:40Z")

</div>

You might not be getting the score itself because you are sorting on a  
field? If you still want the score (without running explain, which is  
expensive), then you can set track scores to true (  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)), in the  
Java API, you simply set SearchRequestBuilder#setTrackScores in order to get  
scores back even though you are not sorting on them.

On Mon, Aug 1, 2011 at 2:51 PM, davrob2 [daviroberts@gmail.com](mailto:daviroberts@gmail.com) wrote:

> Hi Guys,
> 
> Thanks for your answers. This is what I have managed to get done so  
> far:
> 
> i) The equivalent of setting explain: true in the url params is to  
> setExplain(true) on the SearchRequestIndexBuilder
> 
> ii) When I set explain true, this is a sample of what I get from  
> SearchHit.explanation().toString():
> 
> 1.2891362 = weight(listIds:6836 in 796), product of:  
> 0.99999994 = queryWeight(listIds:6836), product of:  
> 10.31309 = idf(docFreq=6, maxDocs=77575)  
> 0.09696414 = queryNorm  
> 1.2891363 = fieldWeight(listIds:6836 in 796), product of:  
> 1.0 = tf(termFreq(listIds:6836)=1)  
> 10.31309 = idf(docFreq=6, maxDocs=77575)  
> 0.125 = fieldNorm(field=listIds, doc=796)
> 
> A guide of what the above means would be really useful.
> 
> i.e. What is "idf" / doc frequency referring to?  
> What is "queryNorm", "fieldWeight", "tf"/term frequency and  
> "fieldNorm"?
> 
> iii) Whether I set explain(true) or not score() method on SearchHit  
> always return NaN:
> 
> for (SearchHit hit : queryResponse.getHits()){  
> Map\<String, Object\> source = hit.getSource();  
> source.put("score", hit.score());  
> ...  
> }
> 
> I think this is fine for me, to set explain(true) when I need to debug  
> my results.
> 
> cheers,
> 
> David.
> 
> On Jul 27, 6:03 pm, Benjamin Devèze [benjamin.dev...@gmail.com](mailto:benjamin.dev...@gmail.com) wrote:
> 
> > On a side note if you are sorting by something other than \_score you  
> > should  
> > have a look to the track\_scores parameter.

---

<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, 3:58am UTC](https://discuss.elastic.co/t/score-of-a-query/4978/8 "2017-07-06T03:58:39Z")

</div>


