Go SDK $vectorSearch Aggregate Returning no results

Hello. When I run this search in the cli tooling I get back a result.

``
db.book.aggregate([ { “$vectorSearch”: { “index”: “vector_index”, “exact”: false, “numCandidates”: 10, “path”: “embedding”, “queryVector”: [1.2, 2.2, 3.2, 4.2], “limit”: 10 } }])
`

When I try the same search with the Go SDK, it comes back with no results.

`
pipeline := mongo.Pipeline{
{{
Key: “$vectorSearch”,
Value: bson.M{
“index”: “vector_index”,
“exact”: false,
“path”: “embedding”,
“queryVector”: float64{1.2, 2.2, 3.2, 4.2},
“numCandidates”: 10,
“limit”: 10,
}},
},
{{
Key: “$project”,
Value: bson.M{
“text”: 1,
“embedding”: 1,
“score”: bson.M{
“$meta”: “vectorSearchScore”,
},
}},
},
}

cur, err := col.Aggregate(ctx, pipeline)
`

I’ve been trying for hours and not sure how to proceed. I ran a pipeline with a simple match that did work.

go version go1.22.4 darwin/arm64 go.mongodb.org/mongo-driver v1.16.0

I figured out the problem last night. My program was deleting all existing documents before adding them back. This is an example/poc program and I thought it was easier to delete everything to avoid duplicates.

That delete call is doing something to cause the vector search to not find any records. A regular find call does return the newly inserted documents.

I changed the code for now to perform a find to see if documents already exist, instead of deleting them and inserting them back. The vector search is now working as expected.

Julia Tazin provided a nice explaination

Atlas search is eventually consistent. It’s not transactional. The code has an insert followed by an immediate read within milliseconds. That won’t workYou need to wait an unspecified amount of time (typically a few seconds) until Atlas Search picks up the changes from the mongodb database and indexes the contents

1 Like

Glad that explanation made sense. Please let me know if there’s anything else I can help with

1 Like