跳到主要内容

Search()

Addedv2.5.xModifiedv2.6.x

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

ctx

Context for the current call to work.

context.Context

option

Optional parameters of the methods.

SearchOption

callOptions

Optional parameters for calling the methods.

grpc.CallOption

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

collectionName

Name of the target collection.

string

limit

Number of entities included in the result set.

int

vectors

Query vectors

[]entity.Vector

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

partitionNames

The names of the target partitions

...string

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

expr

The filtering expression.

string

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

key

The name of the argument used in the expr parameter

string

val

The value of the specified argument.

any

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

offset

The number of entities to skip before the search results are returned.

int

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

outputFields

The names of fields to include in the search results

...string

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

consistencyLevel

Consistency level for the search.

For details, refer to Consistency Level.

entity.ConsistencyLevel

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

annsField

The name of the target vector field in the current operation.

string

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

groupByField

The name of the field, according to which the search results are grouped, ensures diversity and avoids returning multiple results from the same group.

string

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

groupSize

The target number of entities to return within each group in a grouping search.

For example, setting groupSize to 2 instructs the system to return up to 2 of the most similar entities (e.g., document passages or vector representations) within each group. Without setting groupSize, the system defaults to returning only 1 entity per group.

int

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

strictGroupSize

This Boolean parameter dictates whether groupSize should be strictly enforced.

When you set it to True, the system will attempt to fill each group with exactly groupSize results, provided there is sufficient data within each group. If there is an insufficient number of entities in a group, it will return only the available entities, ensuring that groups with adequate data meet the specified groupSize.

bool

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

ignoreGrowing

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.

bool

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

ap

Specifies the parameters for the approximate nearest neighbor (ANN) search.

index.AnnParam

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

key

The name of the argument used in the searchParams parameter

string

value

The value of the specified argument.

any

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

fr

A function that serves as the reranker that reorders the entities in the search results.

entity.Function

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

fieldName

Name of the target field.

string

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

start

The ID of the start entity

int

end

The ID of the end entity

int

Return

ResultSet

Examples

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)
}
}