HybridSearchAsync()
Performs a hybrid search, combining the results of multiple ANN searches with a reranker.
public async Task<SearchResp> HybridSearchAsync(HybridSearchReq request, CancellationToken cancellationToken = default)
Request Syntax
var request = new HybridSearchReq
{
CollectionName = "my_collection",
SearchRequests =
[
new AnnSearchReq
{
VectorFieldName = "embedding",
Vectors = [new[] { 0.1f, 0.2f, 0.3f, 0.4f }],
MetricType = MetricType.L2,
Limit = 10,
Expression = "pages > 300"
},
new AnnSearchReq
{
VectorFieldName = "sparse",
Texts = ["The Hobbit"],
Limit = 10
}
],
Reranker = new RrfReranker(),
Limit = 5,
Parameters = new SearchParameters { OutputFields = { "title" } }
};
SearchResp results = await client.HybridSearchAsync(request);
REQUEST PARAMETERS:
CollectionName(string) -The name of the collection to search in.
DatabaseName(string?) -An optional database name to operate on. When empty, the client’s currently selected database is used.
SearchRequests(IReadOnlyList) - The individual ANN search requests to combine. Each
AnnSearchReqis a nested request type describing one vector field to search, with these members:VectorFieldName(string) -The name of the vector field to search in.
Vectors(IReadOnlyList> ) -The query vectors to search for (dense float vectors). Exactly one of
Vectors,SparseVectors,Float16Vectors,BFloat16Vectors,BinaryVectors,Int8Vectors,TextsorEmbeddingListsmust be set.SparseVectors(IReadOnlyList>? ) -The sparse query vectors to search for. When set,
Vectorsmust be empty. See SparseVector.Float16Vectors(IReadOnlyList>? ) -The float16 query vectors to search for. When set,
Vectorsmust be empty.BFloat16Vectors(IReadOnlyList>? ) -The bfloat16 query vectors to search for. When set,
Vectorsmust be empty.BinaryVectors(IReadOnlyList>? ) -The binary query vectors to search for (raw bytes). When set,
Vectorsmust be empty.Int8Vectors(IReadOnlyList>? ) -The int8 query vectors to search for. When set,
Vectorsmust be empty.Texts(IReadOnlyList? ) -The text query strings to search for (embedded-text / BM25 targets). When set,
Vectorsmust be empty.EmbeddingLists(IReadOnlyList? ) -The embedding lists to search for, used for Array-of-Struct vector sub-fields (e.g. searching
"clips[vector]"with a MAX_SIM metric). See EmbeddingList.Limit(int) -The maximum number of results to return for this leg, also known as
topk. The sum ofLimitandOffsetmust be in [1, 16384].MetricType(MetricType) -The metric type used to measure the distance between vectors for this leg. Defaults to
MetricType.Invalid, letting the server infer it from the index. See MetricType.Expression(string?) -A boolean expression to filter the results of this leg.
Offset(int?) -The number of entities to skip for this leg.
RoundDecimal(long?) -The number of decimal places to round the scores to for this leg.
Radius(string?) -The search radius for range searches on binary/float vectors.
RangeFilter(string?) -The range filter for range searches on binary/float vectors.
GroupByField(string?) -The field to group the results of this leg by.
GroupSize(int?) -The maximum number of results per group for this leg.
StrictGroupSize(bool?) -Whether the group size is strict for this leg.
IgnoreGrowing(bool?) -Whether to ignore the growing segments for this leg.
Timezone(string?) -The timezone used for this leg, e.g.
"+08:00"or"America/New_York".FunctionScoreReranker(IReranker?) -A structured function-score reranker (boost / decay / model) for this leg, mapped to the proto
schema.FunctionScore. When set, it takes precedence overRerank. See Reranker.Rerank(string?) -The reranker to apply to this leg’s results (e.g.
"rrf"). Setting this property causes the request to throw when the search is issued; use anIRerankersuch asRrfRerankerorWeightedRerankerinstead.ExtraParameters(IDictionary) - Additional search parameters passed through to the server for this leg. Entries are added via the collection-initializer syntax
ExtraParameters = { ... }.FilterTemplates(IDictionary) - Named expression-template values used by
Expressionfor this leg, for parameterized filters. Entries are added via the collection-initializer syntaxFilterTemplates = { ... }.Highlighter(IDictionary) - The highlighter settings used to highlight matched terms for this leg. Entries are added via the collection-initializer syntax
Highlighter = { ... }.HighlightType(HighlightType?) -The type of highlighter to apply (lexical or semantic).
FunctionChains(IList) - Function chains applied to this leg’s search results. See FunctionChain.
FunctionChains(IList) - Function chains applied to the search results. See FunctionChain.
Reranker(IReranker) -The reranker used to combine the results of the individual searches. See Reranker.
Limit(int) -The total number of results to return after reranking.
Parameters(SearchParameters?) -The optional hybrid search parameters (partitions, output fields, consistency, etc.). See SearchParameters.
RlsPrincipal(string?) -The row-level security (RLS) principal for the operation. When set, Milvus applies the row policies of the collection to this operation.
SkipRls(bool?) -Whether the operation should skip row-level security (RLS) enforcement. When
null, the server default applies.
RETURN TYPE:
Task
RETURNS:
A SearchResp object that contains the combined, reranked search results.
SearchResp
CollectionName // -> string
FieldsData // -> IReadOnlyList<FieldData>
Ids // -> Ids
NumQueries // -> long
Scores // -> IReadOnlyList<float>
Limit // -> long
Limits // -> IReadOnlyList<long>
GroupByFieldValue // -> FieldData?
AllSearchCount // -> long?
SessionTs // -> ulong
Recalls // -> IReadOnlyList<float>?
Cost // -> long
ScannedRemoteBytes // -> long
ScannedTotalBytes // -> long
CacheHitRatio // -> float?
SingleResults // -> IReadOnlyList<SingleResult>?
HighlightResults // -> IReadOnlyList<HighlightResult>?
ElementIndices // -> IReadOnlyList<long>?
AggregationBuckets // -> IReadOnlyList<IReadOnlyList<AggregationBucket>>?
PARAMETERS:
CollectionName (string) -
The name of the searched collection.
FieldsData (IReadOnlyList
) - The returned fields data. See FieldData.
Ids (Ids) -
The IDs of the returned rows. When the server returns no results, this is the default Ids, whose
LongIds/StringIdsare bothnull.NumQueries (long) -
The number of queries executed.
Scores (IReadOnlyList
) - The scores of the returned rows.
Limit (long) -
The limit used for the search.
Limits (IReadOnlyList
) - The per-query limits.
GroupByFieldValue (FieldData?) -
The group-by field value for each returned row, populated when the search used
SearchParameters.GroupByField. See FieldData.AllSearchCount (long?) -
The total number of rows matching the search (across all groups), populated when the search used
SearchParameters.GroupByField.nullonly when the server returned no results.SessionTs (ulong) -
The server-side timestamp at which the search was executed, used for session-like operations such as iterators (read-your-writes consistency).
Recalls (IReadOnlyList
? ) -The recall rates reported by the server, when it reports them. This is a flat per-returned-row array;
nullotherwise.Cost (long) -
The cost of the search in milliseconds, as reported by the server’s
report_valueextra info, or 0 when the server does not report a cost.ScannedRemoteBytes (long) -
The number of bytes scanned from remote storage during the search, when reported by the server.
ScannedTotalBytes (long) -
The total number of bytes scanned during the search, when reported by the server.
CacheHitRatio (float?) -
The cache hit ratio of the search (0–1), when reported by the server.
nullwhen not reported.SingleResults (IReadOnlyList
? ) -The per-query-vector results, sliced out of the flat response. One SingleResult per query vector, each holding that query’s top-K scores, primary keys and output fields.
HighlightResults (IReadOnlyList
? ) -The highlight results per field, populated when the search used full-text or hybrid highlighting, one entry per highlighted field. See HighlightResult.
nullwhen the server reported none.ElementIndices (IReadOnlyList
? ) -The element indices, populated for matched sub-struct elements when the search operated on array/struct fields.
nullwhen the server reported none.AggregationBuckets (IReadOnlyList
>? ) -The bucket-aggregation results, one inner list per query vector, populated when the request used
SearchAggregation. See AggregationBucket.
EXCEPTIONS:
-
Thrown when the RPC fails or the server returns an error. Inspect the exception message for details.
Example
using Milvus.Client.V2;
using Milvus.Client.V2.Requests.Dql;
using Milvus.Client.V2.Responses.Dql;
using Milvus.Client.V2.Types;
using var client = new MilvusClientV2(new ConnectConfig
{
Uri = "localhost:19530",
Username = "root",
Password = "Milvus"
});
await client.ConnectAsync();
SearchResp results = await client.HybridSearchAsync(new HybridSearchReq
{
CollectionName = "my_collection",
SearchRequests =
[
new AnnSearchReq
{
VectorFieldName = "embedding",
Vectors = [new[] { 0.1f, 0.2f, 0.3f, 0.4f }],
MetricType = MetricType.L2,
Limit = 10
},
new AnnSearchReq
{
VectorFieldName = "sparse",
Texts = ["The Hobbit"],
Limit = 10
}
],
Reranker = new RrfReranker(),
Limit = 5,
Parameters = new SearchParameters
{
OutputFields = { "title" },
ConsistencyLevel = ConsistencyLevel.Strong
}
});