Perform vector search on MongoDB in C#

501 views Asked by At

I am using an Azure Cosmos DB for MongoDB and the new feature to create a vector index for a collection. Part of my data structure looks like this:

public class TextChunk
{
    public string Id {get; set;} = null!;
    public float[]? TextVectors { get; internal set; }
}

I followed this Microsoft tutorial to create the vector index, however all the examples are for JavaScript, while I want to use C#/.Net to perform a vector search.

I have tried using collection.Aggregate().VectorSearch but I get the error:

An exception of type 'MongoDB.Driver.MongoCommandException' occurred in System.Private.CoreLib.dll but was not handled in user code: 
'Command aggregate failed: Unrecognized pipeline stage name: $vectorSearch.'

The Vector Search pipeline stage might only be available when using MongoDB Atlas?

My question is, how do I perform a vector search in my setting? I.e. basically, how do I translate the JavaScript sample from the above-mentioned tutorial to the C# MongoDB library?

(.Net 7, MongoDb.Driver version 2.22.0)

1

There are 1 answers

0
Christian F On BEST ANSWER

I found a way to do exactly what I wanted to. I am using (part of) the JSON provided in the tutorial which I linked in my question. I then parse it to a BSON document, create a pipeline definition from it, and execute it the following way:

BsonDocument bson = BsonDocument.Parse(json);
    
PipelineDefinition<TextChunk, TextChunk> pipeline = new BsonDocument[] 
{
    bson
};

return collection.Aggregate(pipeline);

If anyone is curious, this is the json string:

var json =
   "{" + 
     "\"$search\": {" +
       "\"cosmosSearch\": {" +
         "\"vector\": " + searchVectorString +"," +
         "\"path\": \"" + "TextVectors" + "\"," +
         "\"k\": 2" +
        "}," +
       "\"returnStoredSource\": true }" +
   "}";

where searchVectorString is the query vector in the format [0.00, 0.00, ...]