Skip to main content

HybridSearch()

This method performs a vector search.

func (c *Client) HybridSearch(ctx context.Context, option HybridSearchOption, 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.

HybridSearchOption

callOptions

Optional parameters for calling the methods.

grpc.CallOption

HybridSearchOption

This is an interface type. The hybridSearchOption struct types implement this interface type.

You can use the NewHybridSearchOption function to get the concrete implementation.

NewHybridSearchOption

The signature of this method is as follows:

func NewHybridSearchOption(collectionName string, limit int, annRequests ...*AnnRequest) *hybridSearchOption

Parameter

Description

Type

collectionName

Name of the target collection.

string

limit

Number of entities included in the result set.

int

annRequests

One or multiple search requests.

...*AnnRequest

AnnRequest

This is a struct type. The index.CustomAnnParam struct type implements this interface. You can use the NewAnnRequest method to create a search request.

NewAnnRequest

This method prepares a search request. The signature is as follows:

func NewAnnRequest(annField string, limit int, vectors ...entity.Vector) *annRequest

Parameter

Description

Type

annField

Name of the target vector field.

string

limit

Number of entities to be included in the result set.

int

vectors

Query vectors

[]entity.Vector

You can chain the following methods to append extra parameter settings to the annRequest struct.

WithANNSField

This method appends the settings regarding the vector field to use in the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithANNSField(annsField string) *AnnRequest

Parameter

Description

Type

annsField

The name of the vector field to use in the current field.

bool

WithAnnParam

This method appends the settings regarding the search parameters to the current AnnRequest struct. The signature of this method is as follows:

func (r *AnnRequest) WithAnnParam(ap index.AnnParam) *AnnRequest

Parameter

Description

Type

ap

The index-specific search parameter settings.

index.AnnParam

WithFilter

This method appends the settings regarding the filter to use in the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithFilter(expr string) *AnnRequest

Parameter

Description

Type

expr

The metadata-filtering expression. For details, refer to .

string

WithGroupByField

This method appends the settings regarding the group-by field to use in the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithGroupByField(groupByField string) *AnnRequest

Parameter

Description

Type

groupByField

The name of the scalar field that serves as the group-by field. Once set, the search results will be grouped by the specified field.

string

WithGroupSize

This method appends the settings regarding the group size to use in the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithGroupSize(groupSize int) *AnnRequest

Parameter

Description

Type

groupSize

The desired number of entities to return per group.

string

WithIgnoreGrowing

This method appends the settings regarding whether to ignore growing segments to the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithIgnoreGrowing(ignoreGrowing bool) *AnnRequest

Parameter

Description

Type

ignoreGrowing

Whether to ignore growing segments from the search results.

bool

WIthOffset

This method appends the settings regarding the offset to the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithOffset(offset int) *AnnRequest

Parameter

Description

Type

offset

The number of results to skip from the search results.

The value of this parameter and that of limit should be less than 16,384.

int

WithSearchParam

This method appends the settings regarding the search parameters to the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithSearchParam(key, value string) *AnnRequest

Parameter

Description

Type

key

The name of a search parameter.

string

value

The value of the above search parameter.

string

WithStrictGroupSize

This method appends the settings regarding whether to strictly abide by the specified group size to the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithStrictGroupSize(strictGroupSize bool) *AnnRequest

Parameter

Description

Type

strictGroupSize

controls whether the system should strictly enforce the count set by group_size

bool

WithTemplateParam

This method appends the settings regarding the template parameters to the current AnnRequest struct. The signature of the method is as follows:

func (r *AnnRequest) WithTemplateParam(key string, val any) *AnnRequest

Parameter

Description

Type

key

The name of a template parameter.

string

value

The value of the above template parameter.

string

index.AnnParam

This is an interface type. The following struct types implement this interface.

autoAnnParam

This is a struct type that implements the index.AnnParam interface. You can use NewAutoAnnParam() to get its concrete implementation.

NewAutoAnnParam

This method prepares the search parameters specific to AUTOINDEX. The signature is as follows:

func NewAutoAnnParam(level int) autoAnnParam

Parameter

Description

Type

level

An integer that allows users to balance between search recall and performance. For details, refer to Tune Recall Rate.

int

diskANNParam

This method prepares the search parameters specific to DiskANN. The signature is as follows:

func NewDiskAnnParam(searchList int) diskANNParam

Parameter

Description

Type

searchList

Determines the number of candidate neighbors considered for each data point during graph construction.

int

hsnwAnnParam

This method prepares the search parameters specific to HNSW. The signature is as follows:

func NewHNSWAnnParam(ef int) hsnwAnnParam

Parameter

Description

Type

ef

Controls the breadth of search during nearest neighbor retrieval. It determines how many nodes are visited and evaluated as potential nearest neighbors. This parameter affects only the search process and applies exclusively to the bottom layer of the graph.

int

ivfAnnParam

This method prepares the search parameters specific to IVF. The signature is as follows:

func NewIvfAnnParam(nprobe int) ivfAnnParam

Parameter

Description

Type

nprobe

The number of clusters to search for candidates. Higher values allow more clusters to be searched, improving recall by expanding the search scope but at the cost of increased query latency.

int

scannAnnParam

This method prepares the search parameters specific to SCANN. The signature is as follows:

func NewSCANNAnnParam(nprobe int, reorderK int) scannAnnParam

Parameter

Description

Type

nprobe

The number of clusters to search for candidates. Higher values allow more clusters to be searched, improving recall by expanding the search scope but at the cost of increased query latency.

int

sparseAnnParam

This method prepares the search parameters specific to SPARSE_INVERTED. The signature is as follows:

func NewSparseAnnParam() sparseAnnParam

This method has no mandatory parameters.

index.CustomAnnParam

This is a struct type that implements the AnnRequest interface. You can use NewCustomAnnParam() to get its concrete implementation.

NewCustomAnnParam

This method prepares the custom ANN search parameters for the hybrid search request. The signature of this method is as follows:

func NewCustomAnnParam() CustomAnnParam

This method has no parameters.

You can chain the following methods to append extra settings to the CustomAnnParam struct.

WithExtraParam

This method appends extra search parameters to the current AnnRequest struct. The signature is as follows:

func (b CustomAnnParam) WithExtraParam(key string, value any)

Parameter

Description

Type

key

The name of an extra search parameter.

string

value

The value of the above extra search parameter.

string

WithRadius

This method appends the radius parameters to the current AnnRequest struct. The signature is as follows:

func (b CustomAnnParam) WithRadius(radius float64)

Parameter

Description

Type

radius

The radius for a range search.

float64

WithRangeFilter

This method appends the range filter parameters to the current AnnRequest struct. The signature is as follows:

func (b CustomAnnParam) WithRangeFilter(rangeFilter float64)

Parameter

Description

Type

radiusFilter

The range filter for a range search.

float64

ResultSet

This is a struct type. You can use the GetColumn method to get the result values in a specific field.

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

Return

[]ResultSet

Example

queryVector := []float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}
sparseVector, _ := entity.NewSliceSparseEmbedding([]uint32{1, 21, 100}, []float32{0.1, 0.2, 0.3})

resultSets, err := cli.HybridSearch(ctx, milvusclient.NewHybridSearchOption(
"quick_setup",
3,
milvusclient.NewAnnRequest("dense_vector", 10, entity.FloatVector(queryVector)),
milvusclient.NewAnnRequest("sparse_vector", 10, sparseVector),
).WithReranker(milvusclient.NewRRFReranker()))
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)
}