# Inaccurate Elasticsearch scores with custom scoring functions

**URL:** <https://discuss.elastic.co/t/inaccurate-elasticsearch-scores-with-custom-scoring-functions/365681>\
**Category:** Elasticsearch\
**Created:** [August 28, 2024, 10:14am UTC](https://discuss.elastic.co/t/inaccurate-elasticsearch-scores-with-custom-scoring-functions/365681 "2024-08-28T10:14:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![imcarloseme](https://avatars.discourse-cdn.com/v4/letter/i/c68b51/32.png) [@imcarloseme](https://discuss.elastic.co/u/imcarloseme)\
**Post date:** [August 28, 2024, 10:14am UTC](https://discuss.elastic.co/t/inaccurate-elasticsearch-scores-with-custom-scoring-functions/365681/1 "2024-08-28T10:14:44Z")

</div>

My Elasticsearch scores become inaccurate when using big number differences.

I'm using Elasticsearch 7.10.1.

If I have an index that looks something like this:

```auto
[{:index=>
   {:_index=>"candidates",
    :_id=>"a1786607-e095-4621-bdf9-de2706475614",
    :data=>
     {:name=>"Carli Stark",
      :is_verified=>true, :has_work_permit=>true}}},
 {:index=>
   {:_index=>"candidates",
    :_id=>"57f78d3f-392e-4cdf-a5ff-6d10e7c89d5b",
    :data=>
     {:name=>"Gayla Keeling",
      :is_verified=>false, :has_work_permit=>true}}}]

```

And then I perform a query like this:

```auto
GET candidates/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "filter": {
            "term": {
              "is_verified": true
            }
          },
          "weight": 1000
        },
        {
          "filter": {
            "term": {
              "has_work_permit": true
            }
          },
          "weight": 100000000000
        }
      ],
      "score_mode": "sum",
      "boost_mode": "replace"
    }
  },
  "_source": ["is_verified", "has_work_permit"],
  "size": 50
}

```

For some reason, I get exactly the same scores for both documents, which is unexpected:

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 9.9999998E10,
    "hits" : [
      {
        "_index" : "candidates_production_20240828114811152",
        "_type" : "_doc",
        "_id" : "cbd1b70b-f889-4136-a43e-f6782955f58e",
        "_score" : 9.9999998E10,
        "_source" : {
          "is_verified" : false,
          "has_work_permit": true
        }
      },
      {
        "_index" : "candidates_production_20240828114811152",
        "_type" : "_doc",
        "_id" : "d644a5e5-09e0-496e-8830-c1a772c46611",
        "_score" : 9.9999998E10,
        "_source" : {
          "is_verified" : true
          "has_work_permit": true
        }
      }
    ]
  }
}

```

But if I use different weights that are closer, for example 10000 instead of 1000, then the scores are different and results come as expected:

```auto
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.00000006E11,
    "hits" : [
      {
        "_index" : "candidates_production_20240828114811152",
        "_type" : "_doc",
        "_id" : "d644a5e5-09e0-496e-8830-c1a772c46611",
        "_score" : 1.00000006E11,
        "_source" : {
          "is_verified" : true
          "has_work_permit": true
        }
      },
      {
        "_index" : "candidates_production_20240828114811152",
        "_type" : "_doc",
        "_id" : "cbd1b70b-f889-4136-a43e-f6782955f58e",
        "_score" : 9.9999998E10,
        "_source" : {
          "is_verified" : false
          "has_work_permit": true
        }
      }
    ]
  }
}

```

Any ideas of why does this happen? And is there a way to fix it?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 28, 2024, 6:57pm UTC](https://discuss.elastic.co/t/inaccurate-elasticsearch-scores-with-custom-scoring-functions/365681/2 "2024-08-28T18:57:44Z")

</div>

From #Elastic Search to #Elasticsearch

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 28, 2024, 7:05pm UTC](https://discuss.elastic.co/t/inaccurate-elasticsearch-scores-with-custom-scoring-functions/365681/3 "2024-08-28T19:05:03Z")

</div>

Why would you like to boost by so huge values?

I guess it's related to the internals. According to [this doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html), the max is `3.402823e+38`:

> The new score can be restricted to not exceed a certain limit by setting the `max_boost` parameter. The default for `max_boost` is FLT\_MAX.

May be you are hitting this?

> I'm using Elasticsearch 7.10.1.

Please upgrade to at least 7.17. Your version is unsafe.  
Better to switch to 8.15.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:** [September 25, 2024, 7:05pm UTC](https://discuss.elastic.co/t/inaccurate-elasticsearch-scores-with-custom-scoring-functions/365681/4 "2024-09-25T19:05:19Z")

</div>

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