Hi, I am experimenting with using OpenSearch KNN functionality with a 512 dim vector. The index and query I am using are attached below. To summarize quickly I have a nested set of questions each with its corresponding vector and I also have one answer field per document. The use case is looking for answers to question based on similar meanings.
The problem I am having is that sometimes I get inconsistent results. For example, let’s say I have a document with just one question and then I query the index with a vector that I know matches that one exact document, sometimes open search will send me back a different document with a higher “Score” even though I queried with an exact match. This only happens on occasion.
I was hoping that someone might be able to advise me:
-
If my query or index is incorrect for what I am trying to achieve. I am using a script with a cosine similarity function. I reviewed this similar post Opendistro KNN score giving different scores on the same query vector - #3 by utpal but could not find an answer.
-
Is it possible to get the results of the cosine similarity scoring back with my search results? What I mean is when I query and get a document back can I get the cosine similarity between my query vector and my document’s vector?
{
"mappings" : {
"properties" : {
"questions": {
"type": "nested",
"properties": {
"question": {
"type": "text"
},
"vector": {
"type": "knn_vector",
"dimension": 512
}
}
},
"answer": {
"type": "text"
},
"enabled": {
"type": "boolean"
},
}
}
When I want to query open search to try and find an answer to a similar question then I take the text, get its 512 vector and search for that vector and the following query.
{
"size": 1,
"query": {
"bool": {
"must": [
{"match": {"enabled": true}}
],
"should" : [
{
"nested": {
"score_mode": "avg",
"path": "questions",
"query": {
"script_score": {
"query": {
"match_all": { }
},
"script": {
"lang": "knn",
"source": "knn_score",
"params": {
"field": "questions.vector",
"space_type": "cosinesimil",
"query_value": [VECTOR...]
}
}
}
}
}
}
]
}
}
}