[CSHARP-4864] Unable to create Vector search index via MongoDB driver Created: 08/Dec/23  Updated: 12/Dec/23  Resolved: 12/Dec/23

Status: Closed
Project: C# Driver
Component/s: Builders
Affects Version/s: 2.22.0
Fix Version/s: None

Type: Task Priority: Unknown
Reporter: VenkateshSrini N/A Assignee: Boris Dogadov
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Hi,

I would like to perform a vector cosine similarity in mongo DB. For that as a first step I need to create an index similar to below

{{{}}
  "createIndexes": "<collection_name>",
  "indexes": [
    {
      "name": "<index_name>",
      "key": {
        "<path_to_property>": "cosmosSearch"
      },
      "cosmosSearchOptions": {
        "kind": "vector-ivf",
        "numLists": <integer_value>,
        "nProbes": <integer_value>,
        "similarity": "<string_value>",
        "dimensions": <integer_value>
{{      }}}

I want to do the same via MongoDB C# drivers. This is because I create the collection dynamically via code. I tried using the CreateIndexModelOptions. This class does not seem to have the properties required to create the above index. How do I go about doing this ?

 

{{    }}}
  ]
}

 



 Comments   
Comment by Boris Dogadov [ 12/Dec/23 ]

Thanks for the update venkateshsrini3@gmail.com 

Comment by VenkateshSrini N/A [ 12/Dec/23 ]

Thanks. You can consider the issue as closed.

Comment by Boris Dogadov [ 11/Dec/23 ]

Hi venkateshsrini3@gmail.com 

The Vector Search tutorial  https://www.mongodb.com/products/platform/atlas-vector-search that you've mentioned is a great resource of all information needed to setup the indexes.
I would recommend experimenting with the index definition in Atlas UI or Compass first, testing it and then just copying the definition into C# driver.

If you discover that something is not working as expected in C# driver, please file a Jira ticket including steps to reproduce or self contained reproduction code.

Comment by VenkateshSrini N/A [ 09/Dec/23 ]

boris.dogadov@mongodb.com ,

Thanks for the input. As stated above I wanted to do a create a KNN vector for vector search as described in the link How to Index Vector Embeddings for Vector Search — MongoDB Atlas. The below is the code that I have come up. 

Document structure

{
     "name": "Derby sugar free orange candy",
     "description": "This candy from Derby is sugar free. It is orange flavor. People with diabetes can also have it",
     "taste": "sweet-tart",
     "price": 249.50,
     "embeddings": []
   }

Code for creating index is 

 

private void CreateIndex(string indexName)
{
    var indexDef = new BsonDocument
    {
        { "mappings", new BsonDocument
            {
                {"name",indexName },
                {"dynamic", true },
                {"fields", new BsonDocument
                    {
                        {"embeddings", new BsonDocument
                            {
                                {"type","knnVector" },
                                {"dimensions",1},
                                {"similarity","cosine" }
                            } 
                        }
                    } 
                }
            }
        }
    };
    var searchIndexModel = new CreateSearchIndexModel("gen-ai-idx", indexDef);
    _candiesCollection.SearchIndexes.CreateOne(searchIndexModel);
}
 

Can you please confirm if this is correct? Since I'm doing it the first time need your guidance please

Comment by Boris Dogadov [ 09/Dec/23 ]

Hello venkateshsrini3@gmail.com 

Please see the tutorial for creating search indexes with MongoDB C# Driver: https://www.mongodb.com/docs/atlas/atlas-search/tutorial/create-index/#programatically

You would need to use collection.SearchIndexes.CreateOne(CreateSearchIndexModel) instead of collection.Indexes.CreateOne(CreateIndexModelOptions).

Comment by PM Bot [ 08/Dec/23 ]

Hi venkateshsrini3@gmail.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.