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

support for grouping search (#83) #84

Merged
merged 1 commit into from
May 14, 2024
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
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; }
}
Loading