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 |
---|---|---|
| Context for the current call to work. |
|
| Name of a collection. |
|
| List of partition names. If left empty, all partitions are involved in this operation. Otherwise, only the specified partitions are involved. |
|
| Boolean expression for metadata filtering. For details, refer to Scalar Filtering Rules. |
|
| List of field names to include in the return. |
|
| Query vectors. |
|
| Name of the vector field in the target collection. |
|
| 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. |
|
| Number of entities to return. |
|
| Search parameters. |
|
| Extra search options. |
|
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 |
---|---|---|
|
| Return the dimensionality of the vector. |
|
| Return the serialized representation of the vector. |
|
| 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 |
---|---|---|
|
| Return the specified search settings. |
| null | Set the radius for a range search. |
| null | Set the range filter for a range search. |
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 |
---|---|
| Specifies the name of the field that serves as the grouping criteria. |
| Specifies the guarantee timestamp. |
| Specifies whether to ignore the entities within growing segments during the search. |
| Specifies the number of returned entities. The sum of |
| Specifies the number of entities to skip within the return. The sum of |
| 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)
}