milvus-logo
LFAI
< Docs
  • Go

Search()

This method conducts a vector similarity search.

Invocation

client.Search(ctx, collName, partNames, expr, outputFields, vectors, fieldName, metricType, topK, sp)

Parameters

ParameterDescriptionType
ctxContext to control API invocation processcontext.Context
collNameName of the collection to search inString
partNamesList of names of the partitions to search on.
All partition will be searched if it is left empty.
Slice of string
exprBoolean expression to filter the dataString
outputFieldsList of names of fields to outputSlice of string
vectorsVectors to search withSlice of entity.Vector
fieldNameName of the vector field to search onString
metricTypeMetric type to calculate distance withentity.MetricType
topKNumber of nearest records to returnINT
spSpecific search parameter(s) of the index on the vector fieldentity.SearchParam

Return

A slice of SearchResult that contains the search result, one record per vector.

Errors

err: error in the process (if any). Possible errors are listed below:

  • ErrClientNotReady, error that the client is not connected.

  • ErrCollectionNotExists, error that collection with the specified name does not exist.

  • error that API invocation failed.

Example

searchResult, err := milvusClient.Search(
    context.Background(),                    // ctx
    "book",                                  // CollectionName
    []string{},                              // partitionNames
    "",                                      // expr
    []string{"book_id"},                     // outputFields
    []entity.Vector{entity.FloatVector([]float32{0.1, 0.2})}, // vectors
    "book_intro",                            // vectorField
    entity.L2,                               // metricType
    2,                                       // topK
    sp,                                      // sp
)
if err != nil {
    log.Fatal("fail to search collection:", err.Error())
}
fmt.Printf("%#v\n", searchResult)
for _, sr := range searchResult {
    fmt.Println(sr.IDs)
    fmt.Println(sr.Scores)
}
Feedback

Was this page helpful?