Abhinav Keshri
Abhinav Keshri

Reputation: 625

How to get _score instead of null while using Script to sort Results in Elasticsearch

While using Script to sort the the Results of a Query, Why does Elasticsearch gives null instead of the actual score.

I am using this simple script for testing purpose.

PUT _scripts/simple_sorting
{
  "script" :{
    "lang": "painless",
    "source": """
      return  Math.random();
    """
  }
}

And the Query is

GET some_index/_search
{
  "explain": true, 
    "stored_fields": [
      "_source"
      ], 
    "sort": {
      "_script":{
        "type" : "number",
        "script" : {
          "id": "simple_sorting"
        },
        "order" : "desc"

      }
    },
    "query" : {
      "bool": {
        "should": [
          {
            "match": {
              "tm_applied_for": {
                "query": "bisire"
              }
            }
          }
        ]
      }
    }
}

The query gives me result which look like this.

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 20,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_shard" : "[some_index][0]",
        "_node" : "UIMgEAZNRzmIpRGyQt232g",
        "_index" : "some_index",
        "_type" : "_doc",
        "_id" : "1171229",
        "_score" : null,
        "_source" : {
          "status" : "Registered",
          "proprietor_name
.
.
.
.
          "@timestamp" : "2020-03-27T20:05:25.753Z",
          "tm_applied_for_anan" : "BISLERI"
        },
        "sort" : [
          0.28768208622932434
        ],

You can see that the max_score and _score values are null. But it gives a value in sort array according to which elasticsearch has sorted the documents.

I want that the original score that Elasticsearch had given to Query before i used the script to sort, be returned instead of null.

Also when i change the script simple_sorting as follows. I get some value in sort array(say 0.234...) which is not equal to what it returned earlier (say 12.1234...) when i did not use script to sort.

PUT _scripts/simple_sorting
{
  "script" :{
    "lang": "painless",
    "source": """
      return  _score;
    """
  }
}

Why isn't the _score value same both the time ?

When Elasticsearch Documentation clearly says that i can access _score while using script to sort.

What i am expecting to happen when I use Script to sort is that.

1) max_score and _score to remain as it as it was given by Elasticsearch instead of becoming null.

2) Sorting to happen on the basis of Math.random() value.

Upvotes: 3

Views: 2062

Answers (1)

Nishant
Nishant

Reputation: 7864

This is the default behaviour of elasticsearch since you are using you own logic to sort the results and hence it omits the score. In order to still get the score set track_scores parameter to true. This will give you the elasticsearch calculated relevance score.

GET some_index/_search
{
  "explain": true,
  "stored_fields": [
    "_source"
  ],
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "id": "simple_sorting"
      },
      "order": "desc"
    }
  },
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "tm_applied_for": {
              "query": "bisire"
            }
          }
        }
      ]
    }
  },
  "track_scores": true
}

Upvotes: 5

Related Questions