Elasticsearch version : 6.3.1
Plugins installed: []
JVM version : 1.8.0_144
OS version : Darwin mbp.local 18.0.0 Darwin Kernel Version 18.0.0
Description of the problem including expected versus actual behavior:
The boost on function_score is not applied when there is only one function present
Steps to reproduce:
Starting with query:
"query": {
"function_score" : {
"query" : {
"match_all" : {}
},
"functions" : [
{
"filter" : {
"match_all" : {}
},
"script_score" : {
"script" : { "source" : "8" }
}
},
{
"filter" : {
"match_all" : {}
},
"script_score" : {
"script" : { "source" : "4" }
}
}
],
"score_mode" : "avg",
"boost" : 0.5
}
}
which gets the expected score 3 (average of 6 times boost of 0.5).
When I keep just one function though:
"query": {
"function_score" : {
"query" : {
"match_all" : {}
},
"functions" : [
{
"filter" : {
"match_all" : {}
},
"script_score" : {
"script" : { "source" : "8" }
}
}
],
"score_mode" : "avg",
"boost" : 0.5
}
}
I get a score of 8, the boost not being applied.
Pinging @elastic/es-search-aggs
It seems that adding a "weight" of 1 to the function(s) fixes the issue. Was the "weight" mandatory in this scenario?
@Murukaen thank you for filing this issue. From stepping through the code I think there is a bug around how we treat boost and boost_mode when there is only one function provided. It shouldn't be mandatory to specify weight in this situation.
When only one function was provided and no explicit boost_mode was specified, we sometimes incorrectly used a function's default score_mode in place of boost_mode. This meant that in certain circumstances, we would use replace instead of the default multiply, which overrides not just the subquery score, but also the boost applied to the query. As of #35148 we always use the correct boost_mode, and so the example in the issue comment should work in future versions.
As a note, I am a bit confused by the behavior of the replace mode described above: I would expect it to replace the subquery's score, but not to touch the overall query boost, which should apply to the function_score query as a whole. However, we are actively working to replace function_score and I'm not sure the effort + complexity of changing this is worth it.
Most helpful comment
When only one function was provided and no explicit
boost_modewas specified, we sometimes incorrectly used a function's defaultscore_modein place ofboost_mode. This meant that in certain circumstances, we would usereplaceinstead of the defaultmultiply, which overrides not just the subquery score, but also theboostapplied to the query. As of #35148 we always use the correctboost_mode, and so the example in the issue comment should work in future versions.As a note, I am a bit confused by the behavior of the
replacemode described above: I would expect it to replace the subquery's score, but not to touch the overall queryboost, which should apply to thefunction_scorequery as a whole. However, we are actively working to replacefunction_scoreand I'm not sure the effort + complexity of changing this is worth it.