Skip to content

Commit

Permalink
Merge pull request #35 from roji/SearchEmptyResults
Browse files Browse the repository at this point in the history
Properly handle empty Search results
  • Loading branch information
weianweigan authored Sep 4, 2023
2 parents b47e75a + d14fe65 commit 9ec610d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
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

0 comments on commit 9ec610d

Please sign in to comment.