# Unexpected behavior when using nested query and score\_mode = max

**URL:** <https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110>\
**Category:** Elasticsearch\
**Created:** [December 18, 2012, 9:43pm UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110 "2012-12-18T21:43:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![y\_itay](https://avatars.discourse-cdn.com/v4/letter/y/76d3ee/32.png) [@y\_itay](https://discuss.elastic.co/u/y_itay)\
**Post date:** [December 18, 2012, 9:43pm UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110/1 "2012-12-18T21:43:26Z")

</div>

Hey,

I'm doing some discovery around the nested query feature elasticsearch  
supports and I need to use the max score\_mode.  
I'm expecting to see document id 100 before 101 since id 100 has the full  
phrase "Porsche Engine V6" but from some reason document id 101 appears  
before in the search results.  
Is there any explanations for that?

Thanks,  
Itay

Query:  
{  
"query": {  
"nested": {  
"path": "Relations",  
"score\_mode": "max",  
"query": {  
"query\_string": {  
"fields": [  
"Relations.Title"  
],  
"query": "Engine V6 porsche"  
}  
}  
}  
}  
}

Data Set:  
id 100  
{  
"Title": "2007 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "V6"  
},  
{  
"Title": "BMW v3"  
}  
]  
}

id 101  
{  
"Title": "2005 Porsche 911",  
"Relations": [  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "Good condition Porsche Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}

Result:  
{  
"took": 16,  
"timed\_out": false,  
"\_shards": {  
"total": 10,  
"successful": 10,  
"failed": 0  
},  
"hits": {  
"total": 2,  
"max\_score": 2.197319,  
"hits": [  
{  
"\_index": "searchtest\_es",  
"\_type": "test",  
"\_id": "101",  
"\_score": 2.197319,  
"\_source": {  
"Title": "2005 Porsche 911",  
"Relations": [  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "Good condition Porsche Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}  
},  
{  
"\_index": "searchtest\_es",  
"\_type": "test",  
"\_id": "100",  
"\_score": 2.149176,  
"\_source": {  
"Title": "2007 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "V6"  
},  
{  
"Title": "BMW v3"  
}  
]  
}  
}  
]  
}  
}

--

---

<div class="post-metadata">

**Author:** ![y\_itay](https://avatars.discourse-cdn.com/v4/letter/y/76d3ee/32.png) [@y\_itay](https://discuss.elastic.co/u/y_itay)\
**Post date:** [December 18, 2012, 10:06pm UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110/2 "2012-12-18T22:06:37Z")

</div>

I also tried changing the documents to be exactly the same and the score is  
not the same.  
I really don't understand what's wrong (Note: I did flush the index before  
query).

id 100  
{  
"Title": "2007 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}

id 101  
{  
"Title": "2005 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}

Result:  
{  
"took": 16,  
"timed\_out": false,  
"\_shards": {  
"total": 10,  
"successful": 10,  
"failed": 0  
},  
"hits": {  
"total": 2,  
"max\_score": 2.511222,  
"hits": [  
{  
"\_index": "searchtest\_es",  
"\_type": "test",  
"\_id": "101",  
"\_score": 2.511222,  
"\_source": {  
"Title": "2005 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}  
},  
{  
"\_index": "searchtest\_es",  
"\_type": "test",  
"\_id": "100",  
"\_score": 1.8294235,  
"\_source": {  
"Title": "2007 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}  
}  
]  
}  
}

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 19, 2012, 5:57am UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110/3 "2012-12-19T05:57:27Z")

</div>

You can activate explain option : [http://www.elasticsearch.org/guide/reference/api/search/explain.html](http://www.elasticsearch.org/guide/reference/api/search/explain.html)

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 18 déc. 2012 à 23:06, itay yahimovitz [itay1336@gmail.com](mailto:itay1336@gmail.com) a écrit :

I also tried changing the documents to be exactly the same and the score is not the same.  
I really don't understand what's wrong (Note: I did flush the index before query).

id 100  
{  
"Title": "2007 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}

id 101  
{  
"Title": "2005 Porsche 911",  
"Relations": [  
{  
"Title": "Porsche Engine V6"  
},  
{  
"Title": "Best Engine V6"  
},  
{  
"Title": "V6 porsche"  
}  
]  
}

## Result: { "took": 16, "timed\_out": false, "\_shards": { "total": 10, "successful": 10, "failed": 0 }, "hits": { "total": 2, "max\_score": 2.511222, "hits": [{ "\_index": "searchtest\_es", "\_type": "test", "\_id": "101", "\_score": 2.511222, "\_source": { "Title": "2005 Porsche 911", "Relations": [ { "Title": "Porsche Engine V6" }, { "Title": "Best Engine V6" }, { "Title": "V6 porsche" }] } }, { "\_index": "searchtest\_es", "\_type": "test", "\_id": "100", "\_score": 1.8294235, "\_source": { "Title": "2007 Porsche 911", "Relations": [{ "Title": "Porsche Engine V6" }, { "Title": "Best Engine V6" }, { "Title": "V6 porsche" }] } } ] } }

--

---

<div class="post-metadata">

**Author:** ![y\_itay](https://avatars.discourse-cdn.com/v4/letter/y/76d3ee/32.png) [@y\_itay](https://discuss.elastic.co/u/y_itay)\
**Post date:** [December 19, 2012, 10:02pm UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110/4 "2012-12-19T22:02:41Z")

</div>

I have tried that already, when I try to use explain it's failing:

{"took":15,"timed\_out":false,"\_shards":{"total":10,"successful":8,"failed":2,"failures":[{"status":500,"reason":"RemoteTransportException[[Blue  
Diamond][inet[/10.109.76.76:9300]][search/phase/fetch/id]]; nested:  
UnsupportedOperationException[org.elasticsearch.index.search.nested.BlockJoinQuery$BlockJoinWeight  
cannot explain match on parent document];  
"},{"status":500,"reason":"RemoteTransportException[[Blue  
Diamond][inet[/10.109.76.76:9300]][search/phase/fetch/id]]; nested:  
UnsupportedOperationException[org.elasticsearch.index.search.nested.BlockJoinQuery$BlockJoinWeight  
cannot explain match on parent document];  
"}]},"hits":{"total":2,"max\_score":2.511222,"hits":}}

On Tuesday, December 18, 2012 9:57:27 PM UTC-8, David Pilato wrote:

> You can activate explain option :  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/explain.html)
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 18 déc. 2012 à 23:06, itay yahimovitz \<[itay...@gmail.com](mailto:itay...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> I also tried changing the documents to be exactly the same and the score  
> is not the same.  
> I really don't understand what's wrong (Note: I did flush the index before  
> query).
> 
> id 100  
> {  
> "Title": "2007 Porsche 911",  
> "Relations": [  
> {  
> "Title": "Porsche Engine V6"  
> },  
> {  
> "Title": "Best Engine V6"  
> },  
> {  
> "Title": "V6 porsche"  
> }  
> ]  
> }
> 
> id 101  
> {  
> "Title": "2005 Porsche 911",  
> "Relations": [  
> {  
> "Title": "Porsche Engine V6"  
> },  
> {  
> "Title": "Best Engine V6"  
> },  
> {  
> "Title": "V6 porsche"  
> }  
> ]  
> }
> 
> Result:  
> {  
> "took": 16,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 10,  
> "successful": 10,  
> "failed": 0  
> },  
> "hits": {  
> "total": 2,  
> "max\_score": 2.511222,  
> "hits": [  
> {  
> "\_index": "searchtest\_es",  
> "\_type": "test",  
> "\_id": "101",  
> "\_score": 2.511222,  
> "\_source": {  
> "Title": "2005 Porsche 911",  
> "Relations": [  
> {  
> "Title": "Porsche Engine V6"  
> },  
> {  
> "Title": "Best Engine V6"  
> },  
> {  
> "Title": "V6 porsche"  
> }  
> ]  
> }  
> },  
> {  
> "\_index": "searchtest\_es",  
> "\_type": "test",  
> "\_id": "100",  
> "\_score": 1.8294235,  
> "\_source": {  
> "Title": "2007 Porsche 911",  
> "Relations": [  
> {  
> "Title": "Porsche Engine V6"  
> },  
> {  
> "Title": "Best Engine V6"  
> },  
> {  
> "Title": "V6 porsche"  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }
> 
> --

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [December 19, 2012, 10:26pm UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110/5 "2012-12-19T22:26:35Z")

</div>

Is the `Relations` of type nested your mapping?

Also can you try running with `search_type` option set to  
`dfs_query_then_fetch ` in your query string. In the case just a few  
documents are in an index with 5 primary shards, the score is off.  
Using this option makes sure the score based on the right overall  
document frequency. Note: In production the data is usually properly  
distributed amongst the primary shards and the default search type is  
fine.

The explain for the nested query isn't implemented in the current  
released versions of ES. It is implemented in the master branch, so  
the in next major version it should work. Each query has its own  
explain function, not all query implement this yet. Also the query  
explain varies per query implementation.

Martijn

On 19 December 2012 23:02, itay yahimovitz [itay1336@gmail.com](mailto:itay1336@gmail.com) wrote:

> I have tried that already, when I try to use explain it's failing:
> 
> {"took":15,"timed\_out":false,"\_shards":{"total":10,"successful":8,"failed":2,"failures":[{"status":500,"reason":"RemoteTransportException[[Blue  
> Diamond][inet[/10.109.76.76:9300]][search/phase/fetch/id]]; nested:  
> UnsupportedOperationException[org.elasticsearch.index.search.nested.BlockJoinQuery$BlockJoinWeight  
> cannot explain match on parent document];  
> "},{"status":500,"reason":"RemoteTransportException[[Blue  
> Diamond][inet[/10.109.76.76:9300]][search/phase/fetch/id]]; nested:  
> UnsupportedOperationException[org.elasticsearch.index.search.nested.BlockJoinQuery$BlockJoinWeight  
> cannot explain match on parent document];  
> "}]},"hits":{"total":2,"max\_score":2.511222,"hits":}}
> 
> On Tuesday, December 18, 2012 9:57:27 PM UTC-8, David Pilato wrote:
> 
> > You can activate explain option :  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/explain.html)
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 18 déc. 2012 à 23:06, itay yahimovitz [itay...@gmail.com](mailto:itay...@gmail.com) a écrit :
> > 
> > I also tried changing the documents to be exactly the same and the score  
> > is not the same.  
> > I really don't understand what's wrong (Note: I did flush the index before  
> > query).
> > 
> > id 100  
> > {  
> > "Title": "2007 Porsche 911",  
> > "Relations": [  
> > {  
> > "Title": "Porsche Engine V6"  
> > },  
> > {  
> > "Title": "Best Engine V6"  
> > },  
> > {  
> > "Title": "V6 porsche"  
> > }  
> > ]  
> > }
> > 
> > id 101  
> > {  
> > "Title": "2005 Porsche 911",  
> > "Relations": [  
> > {  
> > "Title": "Porsche Engine V6"  
> > },  
> > {  
> > "Title": "Best Engine V6"  
> > },  
> > {  
> > "Title": "V6 porsche"  
> > }  
> > ]  
> > }
> > 
> > Result:  
> > {  
> > "took": 16,  
> > "timed\_out": false,  
> > "\_shards": {  
> > "total": 10,  
> > "successful": 10,  
> > "failed": 0  
> > },  
> > "hits": {  
> > "total": 2,  
> > "max\_score": 2.511222,  
> > "hits": [  
> > {  
> > "\_index": "searchtest\_es",  
> > "\_type": "test",  
> > "\_id": "101",  
> > "\_score": 2.511222,  
> > "\_source": {  
> > "Title": "2005 Porsche 911",  
> > "Relations": [  
> > {  
> > "Title": "Porsche Engine V6"  
> > },  
> > {  
> > "Title": "Best Engine V6"  
> > },  
> > {  
> > "Title": "V6 porsche"  
> > }  
> > ]  
> > }  
> > },  
> > {  
> > "\_index": "searchtest\_es",  
> > "\_type": "test",  
> > "\_id": "100",  
> > "\_score": 1.8294235,  
> > "\_source": {  
> > "Title": "2007 Porsche 911",  
> > "Relations": [  
> > {  
> > "Title": "Porsche Engine V6"  
> > },  
> > {  
> > "Title": "Best Engine V6"  
> > },  
> > {  
> > "Title": "V6 porsche"  
> > }  
> > ]  
> > }  
> > }  
> > ]  
> > }  
> > }
> > 
> > --
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<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:59am UTC](https://discuss.elastic.co/t/unexpected-behavior-when-using-nested-query-and-score-mode-max/10110/6 "2017-07-06T02:59:15Z")

</div>


