diff --git a/Milvus.Client.Tests/SearchQueryTests.cs b/Milvus.Client.Tests/SearchQueryTests.cs index 73fc481..d6b6c80 100644 --- a/Milvus.Client.Tests/SearchQueryTests.cs +++ b/Milvus.Client.Tests/SearchQueryTests.cs @@ -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("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[] { 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() { diff --git a/Milvus.Client/MilvusCollection.Entity.cs b/Milvus.Client/MilvusCollection.Entity.cs index 1ca8c43..1dd830e 100644 --- a/Milvus.Client/MilvusCollection.Entity.cs +++ b/Milvus.Client/MilvusCollection.Entity.cs @@ -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,