Search()
This method performs a vector search.
func (c *Client) Search(ctx context.Context, option SearchOption, callOptions ...grpc.CallOption) ([]ResultSet, error)
Request Parameters
Parameter | Description | Type |
|---|---|---|
| Context for the current call to work. |
|
| Optional parameters of the methods. | |
| Optional parameters for calling the methods. |
|
SearchOption
This is an interface type. The searchOption struct types implement this interface type.
You can use the NewSearchOption function to get the concrete implementation.
NewSearchOption
The signature of this method is as follows:
func NewSearchOption(collectionName string, limit int, vectors []entity.Vector) *searchOption
Parameter | Description | Type |
|---|---|---|
| Name of the target collection. |
|
| Number of entities included in the result set. |
|
| Query vectors |
You can chain the following methods to append more parameters to the searchOption struct type:
WithPartitions
This method appends the settings regarding the partitionNames parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithPartitions(partitionNames ...string) *searchOption
Parameter | Description | Type |
|---|---|---|
| The names of the target partitions |
|
WithFilter
This method appends the settings regarding the expr parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithFilter(expr string) *searchOption
Parameter | Description | Type |
|---|---|---|
| The filtering expression. |
|
WithTemplateParam
This method appends the settings regarding the arguments used in the expr parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithTemplateParam(key string, val any) *searchOption
Parameter | Description | Type |
|---|---|---|
| The name of the argument used in the |
|
| The value of the specified argument. |
|
WithOffset
This method appends the settings regarding the offset parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithOffset(offset int) *searchOption
Parameter | Description | Type |
|---|---|---|
| The number of entities to skip before the search results are returned. |
|
WithOutputFields
This method appends the settings regarding the outputFields parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithOutputFields(fieldNames ...string) *searchOption
Parameter | Description | Type |
|---|---|---|
| The names of fields to include in the search results |
|
WithConsistencyLevel
This method appends the settings regarding the consistencyLevel parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithConsistencyLevel(consistencyLevel entity.ConsistencyLevel) *searchOption
Parameter | Description | Type |
|---|---|---|
| Consistency level for the search. For details, refer to Consistency Level. |
|
WithANNSField
This method appends the settings regarding the annsField parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithANNSField(annsField string) *searchOption
Parameter | Description | Type |
|---|---|---|
| The name of the target vector field in the current operation. |
|
WithGroupByField
This method appends the settings regarding the groupByField parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithGroupByField(groupByField string) *searchOption
Parameter | Description | Type |
|---|---|---|
| The name of the field, according to which the search results are grouped, ensures diversity and avoids returning multiple results from the same group. |
|
WithGroupSize
This method appends the settings regarding the groupSize parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithGroupSize(groupSize int) *searchOption
Parameter | Description | Type |
|---|---|---|
| The target number of entities to return within each group in a grouping search. For example, setting |
|
WithStrictGroupSize
This method appends the settings regarding the strictGroupSize parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithStrictGroupSize(strictGroupSize bool) *searchOption
Parameter | Description | Type |
|---|---|---|
| This Boolean parameter dictates whether When you set it to |
|
WIthIgnoreGrowing
This method appends the settings regarding the ignoreGrowing parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithIgnoreGrowing(ignoreGrowing bool) *searchOption
Parameter | Description | Type |
|---|---|---|
| When set, this option instructs the search to exclude data from growing segments. Using this setting can enhance search performance by focusing on only indexed, fully processed data. |
|
WithAnnParam
This method appends the settings regarding the ap parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithAnnParam(ap index.AnnParam) *searchOption
Parameter | Description | Type |
|---|---|---|
| Specifies the parameters for the approximate nearest neighbor (ANN) search. |
WithSearchParam
This method appends the settings regarding the searchParams parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithSearchParam(key, value string) *searchOption
Parameter | Description | Type |
|---|---|---|
| The name of the argument used in the |
|
| The value of the specified argument. |
|
WithFunctionReranker
This method appends the settings regarding the fr parameter to the searchOption struct. The signature of this method is as follows:
func (opt *searchOption) WithFunctionReranker(fr *entity.Function) *searchOption
Parameter | Description | Type |
|---|---|---|
| A function that serves as the reranker that reorders the entities in the search results. |
|
entity.Vector
This is an interface. The following types implement this interface.
entity.FloatVector
This is a list containing numbers of the float32 type. The signature is as follows:
type FloatVector []float32
entity.Float16Vector
This is a list containing numbers of the byte type. The signature is as follows:
type Float16Vector []byte
entity.BFloat16Vector
This is a list containing numbers of the byte type. The signature is as follows:
type BFloat16Vector []byte
entity.BinaryVector
This is a list containing numbers of the byte type. The signature is as follows:
type BinaryVector []byte
entity.Text
This is a string type. The signature is as follows:
type Text string
ResultSet
This is a struct type. You can use the GetColumn method to get the result values in a specific field, the Len method to get the total number of entities in the set, and the Slice method to get a subset of the return.
GetColumn
This method returns the query result in a specific column. The signature is as follows:
func (rs *ResultSet) GetColumn(fieldName string) column.Column
Parameter | Description | Type |
|---|---|---|
| Name of the target field. |
|
Len
This method returns the total number of entities in the return. The signature is as follows:
func (rs ResultSet) Len() int
Slice
This method returns a subset of the return. The signature is as follows:
func (rs ResultSet) Slice(start, end int) ResultSet
Parameter | Description | Type |
|---|---|---|
| The ID of the start entity |
|
| The ID of the end entity |
|
Return
ResultSet
Examples
Basic search
package main
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVector := []float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"quick_setup", // collectionName
3, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
}
}
Search with binary vectors
package main
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVector := []byte{0b10011011, 0b01010100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
annSearchParams := index.NewCustomAnnParam()
annSearchParams.WithExtraParam("nprobe", 10)
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"my_binary_collection", // collectionName
5, // limit
[]entity.Vector{entity.BinaryVector(queryVector)},
).WithOutputFields("pk").WithAnnParam(annSearchParams))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
log.Println("Pks: ", resultSet.GetColumn("pk"))
}
}
Search with JSON expressions
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVector := []float32{0.3, -0.6, -0.1}
annParam := index.NewCustomAnnParam()
annParam.WithExtraParam("nprobe", 10)
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"my_json_collection", // collectionName
5, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithOutputFields("metadata").WithAnnParam(annParam))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
}
Search with multiple vectors
package main
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVectors := []entity.Vector{
entity.FloatVector([]float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}),
entity.FloatVector([]float32{0.19886812562848388, 0.06023560599112088, 0.6976963061752597, 0.2614474506242501, 0.838729485096104}),
}
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"quick_setup", // collectionName
3, // limit
queryVectors,
))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
}
}
Search with offset and limit
package main
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVector := []float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"quick_setup", // collectionName
3, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithOffset(10))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
}
}
Search with output fields
package main
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVector := []float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"quick_setup", // collectionName
3, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithOutputFields("color"))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
log.Println("Colors: ", resultSet.GetColumn("color"))
}
}
Search within partitions
package main
import (
"context"
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
token := "YOUR_CLUSTER_TOKEN"
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
APIKey: token,
})
if err != nil {
log.Fatal("failed to connect to milvus server: ", err.Error())
}
defer cli.Close(ctx)
queryVector := []float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}
resultSets, err := cli.Search(ctx, milvusclient.NewSearchOption(
"quick_setup", // collectionName
3, // limit
[]entity.Vector{entity.FloatVector(queryVector)},
).WithPartitions("partitionA"))
if err != nil {
log.Fatal("failed to perform basic ANN search collection: ", err.Error())
}
for _, resultSet := range resultSets {
log.Println("IDs: ", resultSet.IDs)
log.Println("Scores: ", resultSet.Scores)
}
}