What I had originally posted is what we use in production with the $search keyword for vector search aggregation queries:
$search: {
index: 'vector-search',
knnBeta: {
vector: queryEmbeddings,
path: 'embedding',
filter: {
equals: {
value: new ObjectId(userId),
path: 'userId',
},
k: 200,
},
},
The main drawbacks that I noticed while using this vs $vectorSearch keyword is that:
- Results are not ordered by score so we had to add extra aggregation stages to get the score
{ $meta: 'searchScore' }and then sort ascending by the score before we could limit to whatever number of documents we needed to return - Because they’re not ordered, in some cases, the k field value had to be bigger than recommended so we would get all the relevant documents back
From the tests we’ve done using $vectorSearch keyword, results seemed to be ordered by score{ $meta: 'vectorSearchScore' }so that removes the need for some aggregation stages