Skip to content

Commit 5330c99

Browse files
authored
Update vector-search.md
Added example and details to project the vector similarity score
1 parent d32dfd0 commit 5330c99

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

articles/cosmos-db/mongodb/vcore/vector-search.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,17 @@ To perform a vector search, use the `$search` aggregation pipeline stage in a Mo
122122
"vector": <vector_to_search>,
123123
"path": "<path_to_property>",
124124
"k": <num_results_to_return>
125-
}
126-
...
125+
},
126+
"returnStoredSource": True }},
127+
{
128+
"$project": { "<custom_name_for_similarity_score>": {
129+
"$meta": "searchScore" },
130+
"document" : "$$ROOT"
131+
}
127132
}
128133
}
129134
```
135+
To retrieve the similarity score (`searchScore`) along with the documents found by the vector search, use the `$project` operator to include `searchScore` and rename it as `<custom_name_for_similarity_score>` in the results. Then the document is also projected as nested object. Note that the similarity score is calculated using the metric defined in the vector index.
130136

131137
### Query a vector index by using $search
132138

@@ -142,8 +148,12 @@ db.exampleCollection.aggregate([
142148
"path": "vectorContent",
143149
"k": 2
144150
},
145-
"returnStoredSource": true
146-
}
151+
"returnStoredSource": true }},
152+
{
153+
"$project": { "similarityScore": {
154+
"$meta": "searchScore" },
155+
"document" : "$$ROOT"
156+
}
147157
}
148158
]);
149159
```
@@ -153,16 +163,22 @@ In this example, a vector search is performed by using `queryVector` as an input
153163
```javascript
154164
[
155165
{
156-
_id: ObjectId("645acb54413be5502badff94"),
157-
name: 'Eugenia Lopez',
158-
bio: 'Eugenia is the CEO of AdvenureWorks.',
159-
vectorContent: [ 0.51, 0.12, 0.23 ]
166+
similarityScore: 0.9465376,
167+
document: {
168+
_id: ObjectId("645acb54413be5502badff94"),
169+
name: 'Eugenia Lopez',
170+
bio: 'Eugenia is the CEO of AdvenureWorks.',
171+
vectorContent: [ 0.51, 0.12, 0.23 ]
172+
}
160173
},
161174
{
162-
_id: ObjectId("645acb54413be5502badff97"),
163-
name: 'Rory Nguyen',
164-
bio: 'Rory Nguyen is the founder of AdventureWorks and the president of the Our Planet initiative.',
165-
vectorContent: [ 0.91, 0.76, 0.83 ]
175+
similarityScore: 0.9006955,
176+
document: {
177+
_id: ObjectId("645acb54413be5502badff97"),
178+
name: 'Rory Nguyen',
179+
bio: 'Rory Nguyen is the founder of AdventureWorks and the president of the Our Planet initiative.',
180+
vectorContent: [ 0.91, 0.76, 0.83 ]
181+
}
166182
}
167183
]
168184
```

0 commit comments

Comments
 (0)