Skip to content

[BUG] function score for mismatch documents is 1 #21172

@urmichm

Description

@urmichm

Describe the bug

function_score gives _score 1 to the documents that do not match filters.

Shouldn't _score be 0 if none of the filters matched?

Related component

Indexing

To Reproduce

PUT /books
 {
   "mappings": {
     "properties": {
       "title" : { "type": "text" },
       "author": { "type": "keyword" },
       "genre" : { "type": "keyword" },
       "price" : { "type": "double" }
     }
   }
 }

Documents:

POST /books/_doc/1
{
  "title": "Lord of the rings",
  "author": "Tolkien",
  "genre": "fantasy",
  "price": 29.99
}
POST /books/_doc/2
{
  "title": "Sherlock Holmes",
  "author": "Conan Doyle",
  "genre": "detective",
  "price": 39.99
}
POST /books/_doc/3
{
  "title": "The Firm of Girdlestone",
  "author": "Conan Doyle",
  "genre": "novel",
  "price": 39.99
}
POST /books/_doc/11
{
  "title": "Lord of the rings. the fellowship",
  "author": "Tolkien",
  "genre": "fantasy",
  "price": 29.99
}
POST /books/_doc/12
{
  "title": "Lord of the rings. Two Towers",
  "author": "Tolkien",
  "genre": "fantasy",
  "price": 29.99
}
POST /books/_doc/13
{
  "title": "Lord of the rings. The Return of the king",
  "author": "Tolkien",
  "genre": "fantasy",
  "price": 29.99
}

Total 6 Documents.

Query:

GET /books/_search
{
  "query": {
    "function_score": {
      "score_mode": "sum",
      "functions": [  
        {
          "filter": [
            {
              "term": {
                "genre": {
                  "value": "detective",
                  "_name": "genre:detective" 
                }
              }
            }
          ],
            "weight": 0.5
        },
        {
          "filter": [
            {
              "term": {
                "author": {
                  "value": "Conan Doyle",
                  "_name": "author:Conan Doyle" 
                }
              }
            }
          ],
            "weight": 1
        }
      ]
    }
  }
}

The response:

"hits": {
    "total": {
      "value": 6, // all the docs returned
      "relation": "eq"
    },
    "max_score": 1.5,
    "hits": [
      {
        "_index": "books",
        "_id": "2",
        "_score": 1.5,
        "_source": {
          "title": "Sherlock Holmes",
          "author": "Conan Doyle",
          "genre": "detective",
          "price": 39.99
        },
        "matched_queries": [
          "genre:detective",
          "author:Conan Doyle"
        ]
      },
      {
        "_index": "books",
        "_id": "3",
        "_score": 1,
        "_source": {
          "title": "The Firm of Girdlestone",
          "author": "Conan Doyle",
          "genre": "novel",
          "price": 39.99
        },
        "matched_queries": [
          "author:Conan Doyle"
        ]
      },
      {
        "_index": "books",
        "_id": "13",
        "_score": 1,
        "_source": {
          "title": "Lord of the rings. The Return of the king",
          "author": "Tolkien",
          "genre": "fantasy",
          "price": 29.99
        }
      }, .....
      // all other documents with score 1
    ]
  }

Sherlock Holmes has score 1.5 (both genre and author match)
The Firm of Girdlestone has score 1 (author match)
Lord of the Rings has score 1 despite matching no filters. Where does this 1 come from?

Expected behavior

Lord of the Rings documents to have score 0

Additional Details

when executing search with explain flag
Lord of the rings document is as follows:

        "_index": "books",
        "_id": "13",
        "_score": 1,
        "_source": {
          "title": "Lord of the rings. The Return of the king",
          "author": "Tolkien",
          "genre": "fantasy",
          "price": 29.99
        },
        "_explanation": {
          "value": 1,
          "description": "function score, product of:",
          "details": [
            {
              "value": 1,
              "description": "*:*",
              "details": []
            },
            {
              "value": 1,
              "description": "min of:",
              "details": [
                {
                  "value": 1,   // Why  1 ?
                  "description": "No function matched",
                  "details": []
                },
                {
                  "value": 3.4028235e+38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }

Metadata

Metadata

Assignees

Labels

SearchSearch query, autocomplete ...etcbugSomething isn't working

Type

No type

Projects

Status

🆕 New

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions