Constant Expression Script Score Returns Two Different Results for Different Queries

Hi all :slight_smile:

Using Elasticsearch 6.8.1 I'm seeing some very strange behavior with script_score. Using either expression or painless lang, the script "42" which should be a constant expression in either language that evaluates the same value for every document every time, is giving me two different results, even for the same document, when used to score a different query.

$ cat ~/audit.sh
#!/bin/bash

function search() {
  curl -sS -X POST 'http://10.101.0.1:20400/saas/_search?pretty' -H content-type:application/json -d "$1"
}

search '{
  "size": 1,
  "_source": false,
  "query": {
    "function_score": {
      "script_score": {
        "script": {
          "lang": "expression",
          "source": "42"
        }
      },
      "query": {
        "bool": {
          "filter": [
            {
              "exists": {
                "field": "social_account.customer_codes"
              }
            }
          ]
        }
      }
    }
  }
}'

search '{
  "size": 1,
  "_source": false,
  "query": {
    "function_score": {
      "script_score": {
        "script": {
          "lang": "expression",
          "source": "42"
        }
      },
      "query": {
        "term": {
          "_id" : "a903c3a5-32f5-45ba-b092-3a80dc4e965a"
        }
      }
    }
  }
}'
$ ~/audit.sh
{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 441988,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "saas-data-2",
        "_type" : "_doc",
        "_id" : "a903c3a5-32f5-45ba-b092-3a80dc4e965a",
        "_score" : 0.0
      }
    ]
  }
}
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 42.0,
    "hits" : [
      {
        "_index" : "saas-data-2",
        "_type" : "_doc",
        "_id" : "a903c3a5-32f5-45ba-b092-3a80dc4e965a",
        "_score" : 42.0
      }
    ]
  }
}

I'm unsure why the score would be 42.0 in some cases and 0.0 in another, even on the same document.

Thanks in advance

I've solved the issue.

It turns out that function_score has a boost_mode parameter that is set by default to product and according to the _explain, the score contribution for the queryexists is 0.0 while for the term query it is 1.0 because the former is run in a filter context.

{
  "_index" : "saas-data-2",
  "_type" : "_doc",
  "_id" : "a903c3a5-32f5-45ba-b092-3a80dc4e965a",
  "matched" : true,
  "explanation" : {
    "value" : 0.0,
    "description" : "sum of:",
    "details" : [
      {
        "value" : 0.0,
        "description" : "function score, product of:",
        "details" : [
          {
            "value" : 0.0,
            "description" : "ConstantScore(DocValuesFieldExistsQuery [field=social_account.customer_codes])^0.0",
            "details" : [ ]
          },
          {
            "value" : 42.0,
            "description" : "min of:",
            "details" : [
              {
                "value" : 42.0,
                "description" : "script score function, computed with script:\"Script{type=inline, lang='expression', idOrCode='42', options={}, params={}}\" and parameters: \n{}",
                "details" : [
                  {
                    "value" : 0.0,
                    "description" : "_score: ",
                    "details" : [
                      {
                        "value" : 0.0,
                        "description" : "ConstantScore(DocValuesFieldExistsQuery [field=social_account.customer_codes])^0.0",
                        "details" : [ ]
                      }
                    ]
                  }
                ]
              },
              {
                "value" : 3.4028235E38,
                "description" : "maxBoost",
                "details" : [ ]
              }
            ]
          }
        ]
      },
      {
        "value" : 0.0,
        "description" : "match on required clause, product of:",
        "details" : [
          {
            "value" : 0.0,
            "description" : "# clause",
            "details" : [ ]
          },
          {
            "value" : 1.0,
            "description" : "DocValuesFieldExistsQuery [field=_primary_term]",
            "details" : [ ]
          }
        ]
      }
    ]
  }
}

versus:

{
  "_index" : "saas-data-2",
  "_type" : "_doc",
  "_id" : "a903c3a5-32f5-45ba-b092-3a80dc4e965a",
  "matched" : true,
  "explanation" : {
    "value" : 42.0,
    "description" : "sum of:",
    "details" : [
      {
        "value" : 42.0,
        "description" : "function score, product of:",
        "details" : [
          {
            "value" : 1.0,
            "description" : "ConstantScore(_id:[6b dd 37 73 76 b9 fb 7d 9f e7 ee 39 6d af 9b d3 dd be dd af 34 75 ce 1e f7 ae 5a])",
            "details" : [ ]
          },
          {
            "value" : 42.0,
            "description" : "min of:",
            "details" : [
              {
                "value" : 42.0,
                "description" : "script score function, computed with script:\"Script{type=inline, lang='expression', idOrCode='42', options={}, params={}}\" and parameters: \n{}",
                "details" : [
                  {
                    "value" : 1.0,
                    "description" : "_score: ",
                    "details" : [
                      {
                        "value" : 1.0,
                        "description" : "ConstantScore(_id:[6b dd 37 73 76 b9 fb 7d 9f e7 ee 39 6d af 9b d3 dd be dd af 34 75 ce 1e f7 ae 5a])",
                        "details" : [ ]
                      }
                    ]
                  }
                ]
              },
              {
                "value" : 3.4028235E38,
                "description" : "maxBoost",
                "details" : [ ]
              }
            ]
          }
        ]
      },
      {
        "value" : 0.0,
        "description" : "match on required clause, product of:",
        "details" : [
          {
            "value" : 0.0,
            "description" : "# clause",
            "details" : [ ]
          },
          {
            "value" : 1.0,
            "description" : "DocValuesFieldExistsQuery [field=_primary_term]",
            "details" : [ ]
          }
        ]
      }
    ]
  }
}

So it turns out that 6.5.3 and 6.8.1 behave differently. The results above are from 6.8.1 but in 6.5.3, both queries score the document the same way.

I believe 6.5.3 has a bug that has been fixed in 6.8.1 that hasn't been backported to 6.5.

Is 6.5 still receiving fixes or no?

Thanks!

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