SearchAsync()
Performs a vector similarity search.
public async Task<SearchResp> SearchAsync(SearchReq request, CancellationToken cancellationToken = default)
Request Syntax
var request = new SearchReq
{
CollectionName = "my_collection",
VectorFieldName = "embedding",
Vectors = [new[] { 0.1f, 0.2f, 0.3f, 0.4f }],
MetricType = MetricType.L2,
Limit = 5,
Parameters = new SearchParameters { OutputFields = { "title" } }
};
SearchResp results = await client.SearchAsync(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.
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,Texts,EmbeddingListsorIdsmust 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). When set,VectorFieldNamemust name a struct sub-field. See EmbeddingList.Ids(IReadOnlyList) -The primary keys to search for, as an alternative to query vectors. When set,
Vectors,SparseVectorsandFloat16Vectorsmust be empty.MetricType(MetricType) -The metric type used to measure the distance between vectors. See MetricType.
Limit(int) -The maximum number of results to return, also known as
topk. The sum ofLimitandSearchParameters.Offsetmust be in [1, 16384].Parameters(SearchParameters?) -The optional search parameters. See SearchParameters.
FunctionChains(IReadOnlyList) - Function chains applied to the search results. See FunctionChain.
SearchAggregation(SearchAggregation?) -Hierarchical bucket aggregation for the search. See SearchAggregation.
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 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.SearchAsync(new SearchReq
{
CollectionName = "my_collection",
VectorFieldName = "embedding",
Vectors = [new[] { 0.1f, 0.2f, 0.3f, 0.4f }],
MetricType = MetricType.L2,
Limit = 5,
Parameters = new SearchParameters
{
OutputFields = { "title" },
ConsistencyLevel = ConsistencyLevel.Strong
}
});