How to get the similarity score by documentDB

0

I tried vector search on DocumentDB based on the reference below. https://docs.aws.amazon.com/documentdb/latest/developerguide/vector-search.html

I was able to get the document, but I can't get the score. I couldn't find any description of how to get the score in the reference either. Could you tell me how to get the similarity score?

db.collection.aggregate([
  {
    $search: {
      "vectorSearch": {
        "vector": [0.2, 0.5, 0.8], 
        "path": "vectorEmbedding", 
        "similarity": "euclidean",
        "k": 2,
        "probes": 1
      }
    }
  }
]);

{ "_id" : ObjectId("653d835ff96bee02cad7323c"), "product_name" : "Product A", "vectorEmbedding" : [ 0.2, 0.5, 0.8 ] }
已提問 2 個月前檢視次數 241 次
1 個回答
1

DocumentDB's output for vector searches does not include similarity scores, unlike MongoDB. To obtain these scores, you can calculate them manually using a function like the following:

import math

def euclidean_distance(vec1, vec2):
    return math.sqrt(sum((a - b) ** 2 for a, b in zip(vec1, vec2)))

This function calculates the Euclidean distance between two vectors, which can be used as a measure of similarity. For example:

query_vector = [0.2, 0.5, 0.8]
document_vector = [0.2, 0.5, 0.8]  # Example vector from a returned document

score = euclidean_distance(query_vector, document_vector)

In this case, the score value represents the similarity between the query vector and a document's vector. Smaller score indicate higher similarity.

Resources:

profile picture
專家
已回答 2 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南