Skip to content

Commit

Permalink
support for grouping search (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reptarsrage committed May 8, 2024
1 parent b1bd801 commit ee7fd98
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Milvus.Client.Tests/SearchQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,30 @@ public async Task Search_with_unsupported_vector_type_throws()
Assert.Equal("vectors", exception.ParamName);
}

[Fact]
public async Task Search_with_group_by_field()
{
var parameters = new SearchParameters();
parameters.GroupByField = "id";
var results = await Collection.SearchAsync(
"float_vector",
new ReadOnlyMemory<float>[] { new[] { 0.1f, 0.2f } },
SimilarityMetricType.L2,
parameters: parameters,
limit: 2);

Assert.Equal(CollectionName, results.CollectionName);
Assert.Empty(results.FieldsData);
Assert.Collection(results.Ids.LongIds!,
id => Assert.Equal(1, id),
id => Assert.Equal(2, id));
Assert.Null(results.Ids.StringIds); // TODO: Need to test string IDs
Assert.Equal(1, results.NumQueries);
Assert.Equal(2, results.Scores.Count);
Assert.Equal(2, results.Limit);
Assert.Collection(results.Limits, l => Assert.Equal(2, l));
}

public class QueryCollectionFixture : IAsyncLifetime
{
public QueryCollectionFixture(MilvusFixture milvusFixture)
Expand Down
5 changes: 5 additions & 0 deletions Milvus.Client/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ internal static class Constants
/// </summary>
internal const string IgnoreGrowing = "ignore_growing";

/// <summary>
/// Key name.
/// </summary>
internal const string GroupByField = "group_by_field";

/// <summary>
/// Default database name.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions Milvus.Client/MilvusCollection.Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ public async Task<SearchResults> SearchAsync<T>(
Key = Constants.IgnoreGrowing, Value = parameters.IgnoreGrowing.Value.ToString()
});
}

if (parameters.GroupByField is not null)
{
request.SearchParams.Add(new Grpc.KeyValuePair
{
Key = Constants.GroupByField,
Value = parameters.GroupByField
});
}
}

// Note that we send both the consistency level and the guarantee timestamp, although the latter is derived
Expand Down
8 changes: 8 additions & 0 deletions Milvus.Client/SearchParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ public class SearchParameters
/// searches involve growing segments.
/// </summary>
public bool? IgnoreGrowing { get; private set; }

/// <summary>
/// Group search results by the specified field.
/// </summary>
/// <remarks>
/// See <see href="https://milvus.io/docs/single-vector-search.md#Grouping-search" /> for more information.
/// </remarks>
public string? GroupByField { get; set; }
}

0 comments on commit ee7fd98

Please sign in to comment.