Script_score will be calculated multiple times

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:

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:

{
  "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
        }
      },

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.

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