_score inside function is NaN

I have a simple query:

{
  "size": 10,
  "_source": "email_id",
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "match_phrase": {
              "message": {
                "query": "female"
              }
            }
          }
        }
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "Math.log(_score + 1)"
            }
          }
        }
      ]
    }
  }
}

And I get this error:
{

"error": {

"root_cause": [

  {

"type": "script_exception",

"reason": "compile error",

"script_stack": [

  "NaN",

  "^---- HERE"

],

"script": "NaN",

"lang": "painless"

}

],

"type": "search_phase_execution_exception",

"reason": "all shards failed",

"phase": "query",

"grouped": true,
}

If I remove function Math.log, the query will work. If I put any number inside Math.log, it works too. What is wrong?

What is the output if you only return _score? Is this an expected value?

1 Like

Math.log producing an error NaN means that it's argument is less than 0. Do you have documents with negative scores?

What is the output if you only return _score? Is this an expected value?

Yes, the value is expected

Math.log producing an error NaN means that it's argument is less than 0. Do you have documents with negative scores?

No. 1+_scoredoesn't help. 2 + _score too.

I am puzzled why this may happening. Is the error happening in the first 10 docs? Can you output just the scores of docs where the error is produced? Also, what elasticsearch version are you using?

This error just happens, without care any limit or document. 6.2.2
min score must be greater 0, even if if some docs are 0, 1+_score doesn't help at all.
And I can't output scores because I don't see them due to error. If I remove Math.log, scores are greater then 0.

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