# \_score inside function is NaN

**URL:** <https://discuss.elastic.co/t/score-inside-function-is-nan/154847>\
**Category:** Elasticsearch\
**Created:** [October 31, 2018, 1:04pm UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847 "2018-10-31T13:04:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![saturn](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@saturn](https://discuss.elastic.co/u/saturn)\
**Post date:** [October 31, 2018, 1:04pm UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/1 "2018-10-31T13:04:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [October 31, 2018, 4:21pm UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/2 "2018-10-31T16:21:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [October 31, 2018, 8:14pm UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/3 "2018-10-31T20:14:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saturn](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@saturn](https://discuss.elastic.co/u/saturn)\
**Post date:** [November 1, 2018, 9:48am UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/4 "2018-11-01T09:48:55Z")

</div>

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

Yes, the value is expected

---

<div class="post-metadata">

**Author:** ![saturn](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@saturn](https://discuss.elastic.co/u/saturn)\
**Post date:** [November 1, 2018, 9:49am UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/5 "2018-11-01T09:49:46Z")

</div>

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

No. `1+_score`doesn't help.` 2 + _score` too.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [November 1, 2018, 11:23am UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/6 "2018-11-01T11:23:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![saturn](https://avatars.discourse-cdn.com/v4/letter/s/e19b73/32.png) [@saturn](https://discuss.elastic.co/u/saturn)\
**Post date:** [November 1, 2018, 11:59am UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/7 "2018-11-01T11:59:11Z")

</div>

> [@mayya](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 29, 2018, 11:59am UTC](https://discuss.elastic.co/t/score-inside-function-is-nan/154847/8 "2018-11-29T11:59:15Z")

</div>

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