Using max_score inside script function


(Oscar Sanchez Sanchez) #1

Hi there

I would like to run the following script with a function score query:

"return _score / max_score;"

However i'm getting the following result:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "return _score / max_score;",
          "                ^---- HERE"
        ],
        "script": "return _score / max_score;",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "Variable [max_score] is not defined."
    },
    "script_stack": [
      "return _score / max_score;",
      "                ^---- HERE"
    ],
    "script": "return _score / max_score;",
    "lang": "painless"
  },
  "status": 500
}

Is it actually possible to use max_score inside a script ? If so, what could I be doing wrong here?

Thanks!


(Ryan Ernst) #2

What is "max_score"? This is not something provided by Lucene or Elasticsearch. Is it a parameter you are passing into the script?


(Oscar Sanchez Sanchez) #3

Hi @rjernst,

Thanks for the reply, max_score would be the highest individual "_score" of a set of documents, I think this value is returned from ElasticSearch on each query if I'm not mistaken.

What I basically want to do with max_score is to normalize values to use later with min_score and filter out document that let's say do not meet 50% of the maximum score.

Appreciate your time


(Ryan Ernst) #4

This value is returned, but it is only the largest value seen when computing scores, not anything calculated that can be known up front before any documents are scored, thus it cannot be available in scripts.

In general, this has been discussed many times and is difficult to solve. See https://github.com/elastic/elasticsearch/issues/23850


(system) #5

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