# Constant Expression Script Score Returns Two Different Results for Different Queries

**URL:** https://discuss.elastic.co/t/constant-expression-script-score-returns-two-different-results-for-different-queries/189624
**Category:** Elasticsearch
**Created:** [July 9, 2019, 9:03pm UTC](https://discuss.elastic.co/t/constant-expression-script-score-returns-two-different-results-for-different-queries/189624 "2019-07-09T21:03:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)
#### Post date: [July 9, 2019, 9:03pm UTC](https://discuss.elastic.co/t/constant-expression-script-score-returns-two-different-results-for-different-queries/189624/1 "2019-07-09T21:03:58Z")

</div>

Hi all 🙂

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.

```auto
$ 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

---

<div class="post-metadata">

### Author: ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)
#### Post date: [July 9, 2019, 10:35pm UTC](https://discuss.elastic.co/t/constant-expression-script-score-returns-two-different-results-for-different-queries/189624/2 "2019-07-09T22:35:30Z")

</div>

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 query`exists` is `0.0` while for the `term` query it is `1.0` because the former is run in a filter context.

```auto
{
  "_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:

```auto
{
  "_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" : []
          }
        ]
      }
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![dvisz-inf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dvisz-inf/32/42617_2.png) [@dvisz-inf](https://discuss.elastic.co/u/dvisz-inf)
#### Post date: [July 10, 2019, 7:05pm UTC](https://discuss.elastic.co/t/constant-expression-script-score-returns-two-different-results-for-different-queries/189624/3 "2019-07-10T19:05:45Z")

</div>

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!

---

<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: [August 7, 2019, 7:05pm UTC](https://discuss.elastic.co/t/constant-expression-script-score-returns-two-different-results-for-different-queries/189624/4 "2019-08-07T19:05:53Z")

</div>

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