Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public Task RemoveBatchAsync(string collectionName, IEnumerable<string> 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<string> ids = results.Ids.StringIds!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MilvusException>(() => 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()
{
Expand Down