Skip to content

Commit adff120

Browse files
Copilotdiberry
andcommitted
Update Go agent sample to use optimal bulk insert with SetOrdered(false) and error handling
Co-authored-by: diberry <41597107+diberry@users.noreply.github.com>
1 parent 020c66b commit adff120

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

  • ai/vector-search-agent-go/internal/vectorstore

ai/vector-search-agent-go/internal/vectorstore/store.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,25 @@ func (vs *VectorStore) InsertHotelsWithEmbeddings(ctx context.Context, hotels []
217217
docs[i] = hotel
218218
}
219219

220-
result, err := vs.collection.InsertMany(ctx, docs)
220+
// Use unordered inserts for better performance and parallel execution
221+
opts := options.InsertMany().SetOrdered(false)
222+
result, err := vs.collection.InsertMany(ctx, docs, opts)
221223
if err != nil {
222-
return fmt.Errorf("failed to insert documents: %w", err)
224+
// With unordered inserts, some documents may succeed despite errors
225+
if bulkErr, ok := err.(mongo.BulkWriteException); ok {
226+
inserted := len(docs) - len(bulkErr.WriteErrors)
227+
if vs.config.Debug {
228+
fmt.Printf("[vectorstore] Partial insert: %d inserted, %d failed\n", inserted, len(bulkErr.WriteErrors))
229+
}
230+
// Return error if all documents failed
231+
if inserted == 0 {
232+
return fmt.Errorf("failed to insert any documents: %w", err)
233+
}
234+
// Log partial success
235+
fmt.Printf("[vectorstore] Warning: partial insert completed with %d errors\n", len(bulkErr.WriteErrors))
236+
} else {
237+
return fmt.Errorf("failed to insert documents: %w", err)
238+
}
223239
}
224240

225241
if vs.config.Debug {

0 commit comments

Comments
 (0)