Function score - boost_mode replace when functions are empty


(Orgil) #1

Hello, I have a question.

Our search platform generates our query dynamically depending on the query.

We use function_score scoring to modify our search ordering.

"boost_mode" is "replace" in order to take only our "functions" scoring.
And our "functions" have some weight in order to get our desired scoring instead of tfidf match scores.

Sample query is like below. (query is modified from original so please ignore some minor details)

{
  "_source": false,
  "query": {
    "function_score": {
      "boost_mode": "replace",
      "score_mode": "sum",
      "functions": [
        {
          "filter": {
            "term": {
              "region_id": 5
            }
          },
          "weight": 10
        },
        {
          "filter": {
            "term": {
              "title": "some query"
            }
          },
          "weight": 10
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "simple_query_string": {
                "fields": [
                  "title"
                ],
                "query": "some query",
                "default_operator": "AND"
              }
            }
          ],
          "must_not": [
            {
              "simple_query_string": {
                "fields": [
                  "title"
                ],
                "query": "some negative query",
                "default_operator": "AND"
              }
            }
          ]
        }
      }
    }
  }
}

Then the issue is when "functions" are empty. In this case even though I set "boost_mode" to "replace" which should ignore "query" scores, the function_score will take "query" scores.
But I want it to have 0 score if "functions" are empty, as "replace" should ignore "query" score.

{
  "_source": false,
  "query": {
    "function_score": {
      "boost_mode": "replace",
      "score_mode": "sum",
      "functions": [ ],
      "query": {
        "bool": {
          "must": [
            {
              "simple_query_string": {
                "fields": [
                  "title"
                ],
                "query": "some query",
                "default_operator": "AND"
              }
            }
          ],
          "must_not": [
            {
              "simple_query_string": {
                "fields": [
                  "title"
                ],
                "query": "some negative query",
                "default_operator": "AND"
              }
            }
          ]
        }
      }
    }
  }
}

So I have 2 questions.

  1. Is it desired feature that when "functions" are empty and "boost_mode" is "replace" then the "query" score is used?
    (even though "replace" should ignore "query" score)

  2. The solution to this issue can be to add some dummy scoring function in the "functions".
    What kind of dummy scoring function would you recommend?
    I am thinking about below "functions" query.

{
  "filter": {
    "match_all": {
    }
  },
  "weight": 0
}

(Orgil) #2

@elastic please someone have any idea on the first question? :bowing_man:


(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.