diff --git a/dotnet/src/Connectors/Connectors.Memory.Milvus/MilvusMemoryStore.cs b/dotnet/src/Connectors/Connectors.Memory.Milvus/MilvusMemoryStore.cs index 38d10778a723..7bdd2f03db94 100644 --- a/dotnet/src/Connectors/Connectors.Memory.Milvus/MilvusMemoryStore.cs +++ b/dotnet/src/Connectors/Connectors.Memory.Milvus/MilvusMemoryStore.cs @@ -446,7 +446,7 @@ public Task RemoveBatchAsync(string collectionName, IEnumerable keys, Ca MilvusCollection collection = this.Client.GetCollection(collectionName); SearchResults results = await collection - .SearchAsync(EmbeddingFieldName, [embedding], SimilarityMetricType.Ip, limit, this._searchParameters, cancellationToken) + .SearchAsync(EmbeddingFieldName, [embedding], this._metricType, limit, this._searchParameters, cancellationToken) .ConfigureAwait(false); IReadOnlyList ids = results.Ids.StringIds!; diff --git a/dotnet/src/IntegrationTests/Connectors/Memory/Milvus/MilvusMemoryStoreTests.cs b/dotnet/src/IntegrationTests/Connectors/Memory/Milvus/MilvusMemoryStoreTests.cs index 0ed028eba747..5fba220a3ad4 100644 --- a/dotnet/src/IntegrationTests/Connectors/Memory/Milvus/MilvusMemoryStoreTests.cs +++ b/dotnet/src/IntegrationTests/Connectors/Memory/Milvus/MilvusMemoryStoreTests.cs @@ -220,6 +220,45 @@ public async Task GetNearestMatchesAsync(bool withEmbeddings) }); } + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task GetNearestMatchesWithMetricTypeAsync(bool withEmbeddings) + { + //Create collection with default, Ip metric + await this.Store.CreateCollectionAsync(CollectionName); + await this.InsertSampleDataAsync(); + await this.Store.Client.FlushAsync([CollectionName]); + + //Search with Ip metric, run correctly + List<(MemoryRecord Record, double SimilarityScore)> ipResults = + this.Store.GetNearestMatchesAsync(CollectionName, new[] { 5f, 6f, 7f, 8f, 9f }, limit: 2, withEmbeddings: withEmbeddings).ToEnumerable().ToList(); + + Assert.All(ipResults, t => Assert.True(t.SimilarityScore > 0)); + + //Set the store to Cosine metric, without recreate collection + this.Store = new(this._milvusFixture.Host, vectorSize: 5, port: this._milvusFixture.Port, metricType: SimilarityMetricType.Cosine, consistencyLevel: ConsistencyLevel.Strong); + + //An exception will be thrown here, the exception message includes "metric type not match" + MilvusException milvusException = Assert.Throws(() => this.Store.GetNearestMatchesAsync(CollectionName, new[] { 5f, 6f, 7f, 8f, 9f }, limit: 2, withEmbeddings: withEmbeddings).ToEnumerable().ToList()); + + Assert.NotNull(milvusException); + + Assert.Contains("metric type not match", milvusException.Message); + + //Recreate collection with Cosine metric + await this.Store.DeleteCollectionAsync(CollectionName); + await this.Store.CreateCollectionAsync(CollectionName); + await this.InsertSampleDataAsync(); + await this.Store.Client.FlushAsync([CollectionName]); + + //Search with Ip metric, run correctly + List<(MemoryRecord Record, double SimilarityScore)> cosineResults = + this.Store.GetNearestMatchesAsync(CollectionName, new[] { 5f, 6f, 7f, 8f, 9f }, limit: 2, withEmbeddings: withEmbeddings).ToEnumerable().ToList(); + + Assert.All(cosineResults, t => Assert.True(t.SimilarityScore > 0)); + } + [Fact] public async Task GetNearestMatchesWithMinRelevanceScoreAsync() {