Skip to content

[BUG] 2.19.0 - KNN cosinesimil scoring no longer adding 1 #2561

Closed
@tmoitie

Description

@tmoitie

Describe the bug

The documentation describes cosinesimil space type as so:

Cosine similarity returns a number between -1 and 1, and because OpenSearch relevance scores can’t be below 0, the k-NN plugin adds 1 to get the final score.

In the latest version of Opensearch, this is no longer the case, as the output is scaled down to sit between 0 and 1.

Related component

Plugins

To Reproduce

Index settings:

{
    "mappings": {
        "properties": {
            "embedding": {
                "type": "knn_vector",
                "dimension": 4
            }
        }
    },
    "settings": {
        "index": {
            "knn": true,
            "knn.space_type": "cosinesimil"
        }
    }
}

Document:

{
    "embedding": [
        1.0,
        1.0,
        1.0,
        1.0
    ]
}

Query:

{
    "query": {
        "script_score" : {
            "query": {
                "bool": {
                    "must": []
                }
            },
            "script": {
                "source": "knn_score",
                "lang": "knn",
                "params": {
                    "field": "embedding",
                    "query_value": [
                      1.0,
                      1.0,
                      1.0,
                      1.0
                    ],
                    "space_type": "cosinesimil"
                }
            }
        }
    }
}

Expected behavior

Results.hits.hits.0._score was 2.0 in 2.18.0, it is now 1.0. It appears to now scale between 0.0 and 1.0.

Additional Details

Plugins
KNN

Host/Environment (please complete the following information):

  • OS: Docker running on Orbstack on MacOS 15.3.1
  • 2.19.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions