# Scoring of queries on nested documents

**URL:** https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357
**Category:** Elasticsearch
**Created:** [October 21, 2014, 8:56am UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357 "2014-10-21T08:56:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![barry](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@barry](https://discuss.elastic.co/u/barry)
#### Post date: [October 21, 2014, 8:56am UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357/1 "2014-10-21T08:56:51Z")

</div>

Hello,  
I am having a problem understanding how scoring of nested documents works.  
I have found other people with similar questions which have remained  
unanswered:

> <https://stackoverflow.com/questions/25619632/elasticsearch-how-is-the-score-for-nested-queries-computed>

> <https://stackoverflow.com/questions/26263562/elasticsearch-boost-score-with-nested-query>

The relevant section of my current mapping (with nested parts) is:  
mappings: {

```
person: {
    properties: {
        city: {
            type: nested
            properties: {
                visityear: {
                    type: integer
                }
                name: {
                    type: string
                }
            }
        }
    }
}

```

}

If I have three people who have visited different numbers of cities and I  
search for a common city they have all visited I get different score  
values. The person who visited the greatest number of cities is ranked  
first, with the person who visited only one city getting a score of 1  
(currently ranked lowest). The output of the explanation is that hthe score  
is based on 'child doc range from 0 to x'. My question is how do TF, IDF  
and Field Norm work for nested documents when the score is being  
calculated?

Many thanks,  
Barry

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b6dd8305-43df-4146-89f2-28fea0264f61%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b6dd8305-43df-4146-89f2-28fea0264f61%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![barry](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@barry](https://discuss.elastic.co/u/barry)
#### Post date: [October 21, 2014, 10:05am UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357/2 "2014-10-21T10:05:21Z")

</div>

Edit: There is only one shard being used in this mapping.

On Tuesday, October 21, 2014 9:56:51 AM UTC+1, [ba...@intalex.com](mailto:ba...@intalex.com) wrote:

> Hello,  
> I am having a problem understanding how scoring of nested documents works.  
> I have found other people with similar questions which have remained  
> unanswered:
> 
> [ElasticSearch: how is the score for nested queries computed? - Stack Overflow](http://stackoverflow.com/questions/25619632/elasticsearch-how-is-the-score-for-nested-queries-computed)
> 
> [Elasticsearch boost score with nested query - Stack Overflow](http://stackoverflow.com/questions/26263562/elasticsearch-boost-score-with-nested-query)
> 
> The relevant section of my current mapping (with nested parts) is:  
> mappings: {
> 
> ```
> person: {
> properties: {
> city: {
> type: nested
> properties: {
> visityear: {
> type: integer
> }
> name: {
> type: string
> }
> }
> }
> }
> }
> 
> ```
> 
> }
> 
> If I have three people who have visited different numbers of cities and I  
> search for a common city they have all visited I get different score  
> values. The person who visited the greatest number of cities is ranked  
> first, with the person who visited only one city getting a score of 1  
> (currently ranked lowest). The output of the explanation is that hthe score  
> is based on 'child doc range from 0 to x'. My question is how do TF, IDF  
> and Field Norm work for nested documents when the score is being  
> calculated?
> 
> Many thanks,  
> Barry

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ca8a63ee-9700-4ac6-8a4b-b6bb4362ee85%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ca8a63ee-9700-4ac6-8a4b-b6bb4362ee85%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Mark\_Harwood\_2](https://avatars.discourse-cdn.com/v4/letter/m/c77e96/32.png) [@Mark\_Harwood\_2](https://discuss.elastic.co/u/Mark_Harwood_2)
#### Post date: [October 21, 2014, 2:48pm UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357/3 "2014-10-21T14:48:15Z")

</div>

The "score\_mode" setting determines how the scores of the various child  
docs are attributed to the parent doc which is the final scored element.  
See [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query)

You can for example choose to take the average, max or sum of all the child  
documents that match your nested query and reward the parent doc with that  
value

On Tuesday, October 21, 2014 9:56:51 AM UTC+1, [ba...@intalex.com](mailto:ba...@intalex.com) wrote:

> Hello,  
> I am having a problem understanding how scoring of nested documents works.  
> I have found other people with similar questions which have remained  
> unanswered:
> 
> [ElasticSearch: how is the score for nested queries computed? - Stack Overflow](http://stackoverflow.com/questions/25619632/elasticsearch-how-is-the-score-for-nested-queries-computed)
> 
> [Elasticsearch boost score with nested query - Stack Overflow](http://stackoverflow.com/questions/26263562/elasticsearch-boost-score-with-nested-query)
> 
> The relevant section of my current mapping (with nested parts) is:  
> mappings: {
> 
> ```
> person: {
> properties: {
> city: {
> type: nested
> properties: {
> visityear: {
> type: integer
> }
> name: {
> type: string
> }
> }
> }
> }
> }
> 
> ```
> 
> }
> 
> If I have three people who have visited different numbers of cities and I  
> search for a common city they have all visited I get different score  
> values. The person who visited the greatest number of cities is ranked  
> first, with the person who visited only one city getting a score of 1  
> (currently ranked lowest). The output of the explanation is that hthe score  
> is based on 'child doc range from 0 to x'. My question is how do TF, IDF  
> and Field Norm work for nested documents when the score is being  
> calculated?
> 
> Many thanks,  
> Barry

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/071b29f4-2684-4239-bbae-5395d6bbc13c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/071b29f4-2684-4239-bbae-5395d6bbc13c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![barry](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@barry](https://discuss.elastic.co/u/barry)
#### Post date: [October 21, 2014, 3:55pm UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357/4 "2014-10-21T15:55:56Z")

</div>

Thanks for the help Mark.  
When calculating relevance can I assume that TF is the number of times that  
the term appears in the collapsed nested field? I.e. all of the city names  
get merged into one field, or is it handled a different way? Is the Field  
Length Norm calculated in the same way?

Barry

On Tuesday, October 21, 2014 3:48:15 PM UTC+1, Mark Harwood wrote:

> The "score\_mode" setting determines how the scores of the various child  
> docs are attributed to the parent doc which is the final scored element.  
> See  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query)
> 
> You can for example choose to take the average, max or sum of all the  
> child documents that match your nested query and reward the parent doc with  
> that value
> 
> On Tuesday, October 21, 2014 9:56:51 AM UTC+1, [ba...@intalex.com](mailto:ba...@intalex.com) wrote:
> 
> > Hello,  
> > I am having a problem understanding how scoring of nested documents  
> > works. I have found other people with similar questions which have remained  
> > unanswered:
> > 
> > [ElasticSearch: how is the score for nested queries computed? - Stack Overflow](http://stackoverflow.com/questions/25619632/elasticsearch-how-is-the-score-for-nested-queries-computed)
> > 
> > [Elasticsearch boost score with nested query - Stack Overflow](http://stackoverflow.com/questions/26263562/elasticsearch-boost-score-with-nested-query)
> > 
> > The relevant section of my current mapping (with nested parts) is:  
> > mappings: {
> > 
> > ```
> > person: {
> > properties: {
> > city: {
> > type: nested
> > properties: {
> > visityear: {
> > type: integer
> > }
> > name: {
> > type: string
> > }
> > }
> > }
> > }
> > }
> > 
> > ```
> > 
> > }
> > 
> > If I have three people who have visited different numbers of cities and I  
> > search for a common city they have all visited I get different score  
> > values. The person who visited the greatest number of cities is ranked  
> > first, with the person who visited only one city getting a score of 1  
> > (currently ranked lowest). The output of the explanation is that hthe score  
> > is based on 'child doc range from 0 to x'. My question is how do TF, IDF  
> > and Field Norm work for nested documents when the score is being  
> > calculated?
> > 
> > Many thanks,  
> > Barry

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/2b9d6917-a085-4ac1-930a-8e8a70c7c1ae%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/2b9d6917-a085-4ac1-930a-8e8a70c7c1ae%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![barry](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@barry](https://discuss.elastic.co/u/barry)
#### Post date: [October 22, 2014, 10:38am UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357/5 "2014-10-22T10:38:11Z")

</div>

After some investigation, the number of nested docs get counted  
individually along with the root doc.

On Tuesday, October 21, 2014 4:55:56 PM UTC+1, [ba...@intalex.com](mailto:ba...@intalex.com) wrote:

> Thanks for the help Mark.  
> When calculating relevance can I assume that TF is the number of times  
> that the term appears in the collapsed nested field? I.e. all of the city  
> names get merged into one field, or is it handled a different way? Is the  
> Field Length Norm calculated in the same way?
> 
> Barry
> 
> On Tuesday, October 21, 2014 3:48:15 PM UTC+1, Mark Harwood wrote:
> 
> > The "score\_mode" setting determines how the scores of the various child  
> > docs are attributed to the parent doc which is the final scored element.  
> > See  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html#query-dsl-nested-query)
> > 
> > You can for example choose to take the average, max or sum of all the  
> > child documents that match your nested query and reward the parent doc with  
> > that value
> > 
> > On Tuesday, October 21, 2014 9:56:51 AM UTC+1, [ba...@intalex.com](mailto:ba...@intalex.com) wrote:
> > 
> > > Hello,  
> > > I am having a problem understanding how scoring of nested documents  
> > > works. I have found other people with similar questions which have remained  
> > > unanswered:
> > > 
> > > [ElasticSearch: how is the score for nested queries computed? - Stack Overflow](http://stackoverflow.com/questions/25619632/elasticsearch-how-is-the-score-for-nested-queries-computed)
> > > 
> > > [Elasticsearch boost score with nested query - Stack Overflow](http://stackoverflow.com/questions/26263562/elasticsearch-boost-score-with-nested-query)
> > > 
> > > The relevant section of my current mapping (with nested parts) is:  
> > > mappings: {
> > > 
> > > ```
> > > person: {
> > > properties: {
> > > city: {
> > > type: nested
> > > properties: {
> > > visityear: {
> > > type: integer
> > > }
> > > name: {
> > > type: string
> > > }
> > > }
> > > }
> > > }
> > > }
> > > 
> > > ```
> > > 
> > > }
> > > 
> > > If I have three people who have visited different numbers of cities and  
> > > I search for a common city they have all visited I get different score  
> > > values. The person who visited the greatest number of cities is ranked  
> > > first, with the person who visited only one city getting a score of 1  
> > > (currently ranked lowest). The output of the explanation is that hthe score  
> > > is based on 'child doc range from 0 to x'. My question is how do TF, IDF  
> > > and Field Norm work for nested documents when the score is being  
> > > calculated?
> > > 
> > > Many thanks,  
> > > Barry

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d8809201-3806-4a49-9b87-7eb0c2e02dc2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d8809201-3806-4a49-9b87-7eb0c2e02dc2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:54am UTC](https://discuss.elastic.co/t/scoring-of-queries-on-nested-documents/20357/6 "2017-07-06T00:54:28Z")

</div>


