Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4893

Unable to create Vector search index via MongoDB driver

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Blocker - P1 Blocker - P1
    • None
    • Affects Version/s: 2.23.1
    • Component/s: Index Management

      Summary

      I use the following code to create my collection and search index on the fly.

      public CandiesRepo(IMongoClient mongoClient, MongoUrl mongoUrl,
          ILogger<CandiesRepo> logger)
      {
          _logger = logger;
          var db = mongoClient.GetDatabase(mongoUrl?.DatabaseName);
          var collectionName = "Candies";
          _aiIdxName = "gen-ai-idx";
          _candiesCollection = db.GetCollection<Candy>(collectionName);
          CreateIndex(_aiIdxName);
          _logger = logger;
      }
      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(indexName, indexDef);
          _candiesCollection.SearchIndexes.CreateOne(searchIndexModel);
      }
      public async Task<List<Candy>> SeedCandiesAsync(List<Candy> candies)
      {
          await _candiesCollection.InsertManyAsync(candies);
          return candies;
      }

      When I run the code I get the below exception. I'm also attaching the code file for your reference CandiesRepo.cs. Please help me to resolve the same

      MongoDB.Driver.MongoCommandException
        HResult=0x80131500
        Message=Command createSearchIndexes failed: no such command: 'createSearchIndexes'.
        Source=MongoDB.Driver.Core
        StackTrace:
         at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
         at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.Execute(IConnection connection, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocol[TResult](IWireProtocol`1 protocol, ICoreSession session, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.Servers.Server.ServerChannel.Command[TResult](ICoreSession session, ReadPreference readPreference, DatabaseNamespace databaseNamespace, BsonDocument command, IEnumerable`1 commandPayloads, IElementNameValidator commandValidator, BsonDocument additionalOptions, Action`1 postWriteAction, CommandResponseHandling responseHandling, IBsonSerializer`1 resultSerializer, MessageEncoderSettings messageEncoderSettings, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocol(IChannelHandle channel, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.Operations.CommandOperationBase`1.ExecuteProtocol(IChannelSource channelSource, ICoreSessionHandle session, ReadPreference readPreference, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.Operations.WriteCommandOperation`1.Execute(IWriteBinding binding, CancellationToken cancellationToken)
         at MongoDB.Driver.Core.Operations.CreateSearchIndexesOperation.Execute(IWriteBinding binding, CancellationToken cancellationToken)
         at MongoDB.Driver.OperationExecutor.ExecuteWriteOperation[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken)
         at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperation[TResult](IClientSessionHandle session, IWriteOperation`1 operation, CancellationToken cancellationToken)
         at MongoDB.Driver.MongoCollectionImpl`1.MongoSearchIndexManager.<>cDisplayClass2_0.<CreateMany>b0(IClientSessionHandle session)
         at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
         at MongoDB.Driver.MongoCollectionImpl`1.MongoSearchIndexManager.CreateMany(IEnumerable`1 models, CancellationToken cancellationToken)
         at MongoDB.Driver.MongoCollectionImpl`1.MongoSearchIndexManager.CreateOne(CreateSearchIndexModel model, CancellationToken cancellationToken)
         at candy_shop_ai.Repository.CandiesRepo.CreateIndex(String indexName) in C:\CarryCase\Venkatesh\ASPNETCore\8\candy-shop-ai\Repository\CandiesRepo.cs:line 47
         at candy_shop_ai.Repository.CandiesRepo..ctor(IMongoClient mongoClient, MongoUrl mongoUrl, ILogger`1 logger) in C:\CarryCase\Venkatesh\ASPNETCore\8\candy-shop-ai\Repository\CandiesRepo.cs:line 21

       

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      MongoDB Driver version: 2.23.1

      MongoDB server version: 6.0.3

      MongoDB Topolgy : standalone (installed in Kubernetes on Docker Desktop)

      How to Reproduce

       

      1) Install Mongo DB from https://charts.bitnami.com/bitnami on your Kubernetes cluster

      2) Port-forward the same and check the access from Mongo Shell or Mongo Compass

      3) On success of above step, Take the attached code and create appropriate mongo URL and mongo client and invoke the same with attached code. Alternatively use the provided code in console app and try

      Additional Background

      Please provide any additional background information that may be helpful in diagnosing the bug.

        1. CandiesRepo.cs
          4 kB
          VenkateshSrini N/A
        2. Candy.cs
          0.5 kB
          VenkateshSrini N/A
        3. ICandiesRepo.cs
          0.6 kB
          VenkateshSrini N/A

            Assignee:
            boris.dogadov@mongodb.com Boris Dogadov
            Reporter:
            venkateshsrini3@gmail.com VenkateshSrini N/A
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: