Elasticsearch: Boost is not applied for function_score

Created on 31 Oct 2018  路  4Comments  路  Source: elastic/elasticsearch

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.

:SearcRanking >bug

Most helpful comment

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.

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clintongormley picture clintongormley  路  3Comments

rpalsaxena picture rpalsaxena  路  3Comments

abrahamduran picture abrahamduran  路  3Comments

ppf2 picture ppf2  路  3Comments

clintongormley picture clintongormley  路  3Comments