# Script\_score will be calculated multiple times

**URL:** <https://discuss.elastic.co/t/script-score-will-be-calculated-multiple-times/198885>\
**Category:** Elasticsearch\
**Created:** [September 10, 2019, 11:00am UTC](https://discuss.elastic.co/t/script-score-will-be-calculated-multiple-times/198885 "2019-09-10T11:00:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sailxjx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sailxjx/32/53625_2.png) [@sailxjx](https://discuss.elastic.co/u/sailxjx)\
**Post date:** [September 10, 2019, 11:00am UTC](https://discuss.elastic.co/t/script-score-will-be-calculated-multiple-times/198885/1 "2019-09-10T11:00:06Z")

</div>

I used script\_score to calculate the rank score of my documents, however the result was not in my expectations. I specified the score in 0 or 2. The search query was:

```auto
GET my_index/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "script": """
            def dueDate = doc['dueDate'].value.toInstant().toEpochMilli();
            def overDue = (doc['isDone'].value == false) && (dueDate > 0) && (dueDate - System.currentTimeMillis() < 86400000);
            return overDue ? 2 : 0;
            """
          }
        }
      ],
      "boost_mode": "sum"
    }
  }
}

```

The search results were:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 3.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 3.0,
        "_source" : {
          "message" : "已过期并未完成",
          "dueDate" : 1568108066498,
          "isDone" : false,
          "created" : 1568110746090
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.0,
        "_source" : {
          "message" : "一天内截止并未完成",
          "dueDate" : 1568194441195,
          "isDone" : false,
          "created" : 1568110746091
        }
      },

```

---

<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:** [September 11, 2019, 5:45pm UTC](https://discuss.elastic.co/t/script-score-will-be-calculated-multiple-times/198885/2 "2019-09-11T17:45:14Z")

</div>

The boost mode `sum` will add the value you return from the function to the \_score of the doc, which will be a constant score of 1.0 in this case, thus you get 3.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:** [October 9, 2019, 5:54pm UTC](https://discuss.elastic.co/t/script-score-will-be-calculated-multiple-times/198885/3 "2019-10-09T17:54:42Z")

</div>

Closed on October 9, 2019, 5:54pm UTC

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