Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle empty Search results #35

Merged
merged 1 commit into from
Sep 4, 2023
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
41 changes: 41 additions & 0 deletions Milvus.Client.Tests/SearchQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,47 @@ public async Task Search_with_offset()
Assert.Collection(results.Limits, l => Assert.Equal(2, l));
}

[Fact]
public async Task Search_with_no_results()
{
// Create and load an empty collection
MilvusCollection collection = Client.GetCollection(nameof(Search_with_no_results));
string collectionName = collection.Name;

await collection.DropAsync();
collection = await Client.CreateCollectionAsync(
collectionName,
new[]
{
FieldSchema.Create<long>("id", isPrimaryKey: true),
FieldSchema.CreateFloatVector("float_vector", 2)
});

await collection.CreateIndexAsync("float_vector", IndexType.Flat, SimilarityMetricType.L2);

await collection.LoadAsync();
await collection.WaitForCollectionLoadAsync(
waitingInterval: TimeSpan.FromMilliseconds(100), timeout: TimeSpan.FromMinutes(1));

var results = await collection.SearchAsync(
"float_vector",
new ReadOnlyMemory<float>[] { new[] { 0.1f, 0.2f } },
SimilarityMetricType.L2,
limit: 2,
new() { OutputFields = { "id" } });

Assert.Equal(collectionName, results.CollectionName);

// When there are no results, Milvus returns a null "Ids" result, so there's no way to know if it's generally
// long or string IDs
Assert.Null(results.Ids.LongIds);
Assert.Null(results.Ids.StringIds);

Assert.Empty(results.FieldsData);
Assert.Equal(1, results.NumQueries);
Assert.Empty(results.Scores);
}

[Fact]
public async Task Search_with_json_filter()
{
Expand Down
2 changes: 1 addition & 1 deletion Milvus.Client/MilvusCollection.Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ await _client.InvokeAsync(_client.GrpcClient.SearchAsync, request, static r => r
{
CollectionName = response.CollectionName,
FieldsData = response.Results.FieldsData.Select(FieldData.FromGrpcFieldData).ToList(),
Ids = MilvusIds.FromGrpc(response.Results.Ids),
Ids = response.Results.Ids is null ? default : MilvusIds.FromGrpc(response.Results.Ids),
NumQueries = response.Results.NumQueries,
Scores = response.Results.Scores,
Limit = response.Results.TopK,
Expand Down