Skip to main content

Search()

This method conducts a similarity search.

func (c *GrpcClient) Search(ctx context.Context, collName string, partitions []string, expr string, outputFields []string, vectors []entity.Vector, vectorField string, metricType entity.MetricType, topK int, sp entity.SearchParam, opts ...SearchQueryOptionFunc) ([]SearchResult, error)

Request Parameters

Parameter

Description

Type

ctx

Context for the current call to work.

context.Context

collName

Name of a collection.

string

partitions

List of partition names.

If left empty, all partitions are involved in this operation. Otherwise, only the specified partitions are involved.

[]string

expr

Boolean expression for metadata filtering.

For details, refer to Scalar Filtering Rules.

string

outputFields

List of field names to include in the return.

[]string

vectors

Query vectors.

[]entity.Vector

vectorField

Name of the vector field in the target collection.

string

metricType

Metric type for this operation.

A metric type defines how Milvus measures similarity between the query vectors and vectors in the collection. For details, refer to Similarity Metrics.

entity.MetricType

topK

Number of entities to return.

int

sp

Search parameters.

entity.SearchParam

opts

Extra search options.

...client.SearchQueryOptionFunc

entity.Vector

This interface type defines a set of method signatures as follows.

type Vector interface {
Dim() int
Serialize() []byte
FieldType() FieldType
}

Method Signature

Return Type

Description

Dim()

int

Return the dimensionality of the vector.

Serialize()

[]byte

Return the serialized representation of the vector.

FieldType()

entity.FieldType

Return the field type of the vector.

For details on the types that implement the above method signatures, refer to Vectors.

entity.MetricType

const (
L2 MetricType = "L2"
IP MetricType = "IP"
COSINE MetricType = "COSINE"
HAMMING MetricType = "HAMMING"
JACCARD MetricType = "JACCARD"
TANIMOTO MetricType = "TANIMOTO"
SUBSTRUCTURE MetricType = "SUBSTRUCTURE"
SUPERSTRUCTURE MetricType = "SUPERSTRUCTURE"
)

entity.SearchParam

This interface type defines a set of method signatures as follows.

Method Signature

Return Type

Description

Params()

map[string]interface

Return the specified search settings.

AddRadius(radius float64)

null

Set the radius for a range search.

AddRangeFilter(rangeFilter float64)

null

Set the range filter for a range search.

📘Notes

In a range search, radius defines the outer boundary of your search space, and rangeFilter optionally defines an inner boundary, creating a distance range within which vectors must fall to be considered matches.

The value ranges of these parameters vary with the metric type.

For details on the types that implement the above method signatures, refer to SearchParams.

client.SearchQueryOptionFunc

Each of the following methods instantiates a client.SearchQueryOptionFunc type.

Method

Description

WithGroupByField(groupByField string)

Specifies the name of the field that serves as the grouping criteria.

WithGuaranteeTimestamp(gt uint64)

Specifies the guarantee timestamp.

WithIgnoreGrowing()

Specifies whether to ignore the entities within growing segments during the search.

WithLimit(limit int64)

Specifies the number of returned entities.

The sum of limit and offset should be less than 16,384.

WithOffset(offset int64)

Specifies the number of entities to skip within the return.

The sum of limit and offset should be less than 16,384.

WithSearchQueryConsistencyLevel(cl entity.ConsistencyLevel)

Specifies the consistency level for the search.

You can include one or multiple of the above methods in the search request if you see fit.

Return

This method returns a client.SearchResult slice.

client.SearchResult

A client.SearchResult is a struct type defined as follows:

type SearchResult struct {
ResultCount int // the returning entry count
GroupByValue entity.Column
IDs entity.Column // auto generated id, can be mapped to the columns from `Insert` API
Fields ResultSet // []entity.Column // output field data
Scores []float32 // distance to the target vector
Err error // search error if any
}

Errors

Any error in the execution of the request. Possible errors are as follows:

  • ErrClientNotReady: The client is not connected to Milvus.

  • ErrCollectionNotExists: The collection with the specified name does not exist.

  • The call to this API fails.

Example

  • Default search
// search
v1 := make([]float32, 0, 768)
v2 := make([]float32, 0, 768)
for j := 0; j < 768; j++ {
v1 = append(v1, rand.Float32())
v2 = append(v2, rand.Float32())
}
sp, errSp := entity.NewIndexHNSWSearchParam(74)
if errSp != nil {
log.Fatal("failed to new hnsw search params:", errSp.Error())
}
searchRes, errSearch := mc.Search(
context.Background(),
collectionName,
[]string{},
"",
[]string{},
[]entity.Vector{entity.FloatVector(v1), entity.FloatVector(v2)},
"vector",
entity.COSINE,
10,
sp,
client.WithSearchQueryConsistencyLevel(entity.ClBounded),
)
if errSearch != nil {
log.Fatal("failed to search collection:", errSearch.Error())
}
for _, res := range searchRes {
log.Println(res.ResultCount, res.IDs, res.Scores)
}
  • Range search
// search
v1 := make([]float32, 0, 768)
v2 := make([]float32, 0, 768)
for j := 0; j < 768; j++ {
v1 = append(v1, rand.Float32())
v2 = append(v2, rand.Float32())
}
sp, errSp := entity.NewIndexHNSWSearchParam(74)
sp.AddRadius(0.6)
sp.AddRangeFilter(0.9)
if errSp != nil {
log.Fatal("failed to new hnsw search params:", errSp.Error())
}
searchRes, errSearch := mc.Search(
context.Background(),
collectionName,
[]string{},
"",
[]string{},
[]entity.Vector{entity.FloatVector(v1), entity.FloatVector(v2)},
"vector",
entity.COSINE,
10,
sp,
)
if errSearch != nil {
log.Fatal("failed to search collection:", errSearch.Error())
}
for _, res := range searchRes {
log.Println(res.ResultCount, res.IDs, res.Scores)
}
  • Pagination search
// search
v1 := make([]float32, 0, 768)
v2 := make([]float32, 0, 768)
for j := 0; j < 768; j++ {
v1 = append(v1, rand.Float32())
v2 = append(v2, rand.Float32())
}
sp, errSp := entity.NewIndexHNSWSearchParam(74)
if errSp != nil {
log.Fatal("failed to new hnsw search params:", errSp.Error())
}
searchRes, errSearch := mc.Search(
context.Background(),
collectionName,
[]string{},
"",
[]string{},
[]entity.Vector{entity.FloatVector(v1), entity.FloatVector(v2)},
"vector",
entity.COSINE,
10,
sp,
client.WithOffset(5),
)
if errSearch != nil {
log.Fatal("failed to search collection:", errSearch.Error())
}
for _, res := range searchRes {
log.Println(res.ResultCount, res.IDs, res.Scores)
}
  • GroupBy search
// search
v1 := make([]float32, 0, 768)
v2 := make([]float32, 0, 768)
for j := 0; j < 768; j++ {
v1 = append(v1, rand.Float32())
v2 = append(v2, rand.Float32())
}
sp, errSp := entity.NewIndexHNSWSearchParam(74)
if errSp != nil {
log.Fatal("failed to new hnsw search params:", errSp.Error())
}
searchRes, errSearch := mc.Search(
context.Background(),
collectionName,
[]string{},
"",
[]string{},
[]entity.Vector{entity.FloatVector(v1), entity.FloatVector(v2)},
"vector",
entity.COSINE,
10,
sp,
client.WithGroupByField("varchar"),
)
if errSearch != nil {
log.Fatal("failed to search collection:", errSearch.Error())
}
for _, res := range searchRes {
log.Println(res.ResultCount, res.IDs, res.Scores, res.GroupByValue)
}