Vector indexes on owned entities silently skipped if first owned entity has none

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Vector indexes defined on a second or subsequent owned entity are silently skipped when calling CreateMissingVectorIndexes (or the async variant).

      The root cause is an early return in the HasAtlasIndexes guard method which returns based on the first owned entity rather than contnuing to look if the first one doesn'thave a vector index.

      Repr in form of test for IndexTests.cs

       

          [AtlasTheory]
          [InlineData(false)]
          [InlineData(true)]
          public async Task CreateMissingVectorIndexes_detects_vector_index_on_second_owned_entity(bool async)
          {
              var collection = database.CreateCollection<EntityWithTwoOwnedTypes>(values: async);
              using var db = SingleEntityDbContext.Create(collection,
                  b => b.Entity<EntityWithTwoOwnedTypes>(b =>
                  {
                      b.OwnsOne(e => e.First);
                      b.OwnsOne(e => e.Second)
                          .HasIndex(e => e.Doubles)
                          .IsVectorIndex(VectorSimilarity.Cosine, 2);
                  }));
              var options = new MongoDatabaseCreationOptions(CreateMissingVectorIndexes: false, WaitForVectorIndexes: false);
              var bsonCollection = database.GetCollection<BsonDocument>(collection.CollectionNamespace);
              if (async)
              {
                  await db.Database.EnsureCreatedAsync(options);
                  Assert.Empty(bsonCollection.SearchIndexes.List().ToList());
                  await db.Database.CreateMissingVectorIndexesAsync();
                  await db.Database.WaitForVectorIndexesAsync();
              }
              else
              {
                  db.Database.EnsureCreated(options);
                  Assert.Empty(bsonCollection.SearchIndexes.List().ToList());
                  db.Database.CreateMissingVectorIndexes();
                  db.Database.WaitForVectorIndexes();
              }
              var indexes = bsonCollection.SearchIndexes.List().ToList();
              var index = Assert.Single(indexes);
              var fields = index["latestDefinition"]["fields"].AsBsonArray;
              Assert.Equal("DoublesVectorIndex", index["name"].AsString);
              Assert.Equal("vector", fields[0]["type"].AsString);
              Assert.Equal("Second.Doubles", fields[0]["path"].AsString);
              Assert.Equal(2, fields[0]["numDimensions"].AsInt32);
              Assert.Equal("cosine", fields[0]["similarity"].AsString);
          }
      

       

            Assignee:
            Damien Guard
            Reporter:
            Damien Guard
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: