Using max_score inside script function

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!

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

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

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

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