# Using max\_score inside script function

**URL:** https://discuss.elastic.co/t/using-max-score-inside-script-function/156871
**Category:** Elasticsearch
**Created:** [November 15, 2018, 1:21pm UTC](https://discuss.elastic.co/t/using-max-score-inside-script-function/156871 "2018-11-15T13:21:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Oscar\_Sanchez\_Sanche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oscar_sanchez_sanche/32/36678_2.png) [@Oscar\_Sanchez\_Sanche](https://discuss.elastic.co/u/Oscar_Sanchez_Sanche)
#### Post date: [November 15, 2018, 1:21pm UTC](https://discuss.elastic.co/t/using-max-score-inside-script-function/156871/1 "2018-11-15T13:21:36Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [November 15, 2018, 10:42pm UTC](https://discuss.elastic.co/t/using-max-score-inside-script-function/156871/2 "2018-11-15T22:42:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Oscar\_Sanchez\_Sanche](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oscar_sanchez_sanche/32/36678_2.png) [@Oscar\_Sanchez\_Sanche](https://discuss.elastic.co/u/Oscar_Sanchez_Sanche)
#### Post date: [November 19, 2018, 6:05pm UTC](https://discuss.elastic.co/t/using-max-score-inside-script-function/156871/3 "2018-11-19T18:05:22Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [November 19, 2018, 11:22pm UTC](https://discuss.elastic.co/t/using-max-score-inside-script-function/156871/4 "2018-11-19T23:22:55Z")

</div>

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](https://github.com/elastic/elasticsearch/issues/23850)

---

<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: [December 17, 2018, 11:22pm UTC](https://discuss.elastic.co/t/using-max-score-inside-script-function/156871/5 "2018-12-17T23:22:56Z")

</div>

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