Milvus
Zilliz
< Docs
  • Go

SearchIterator()

This method creates an iterator that walks through the search results.

func (c *Client) SearchIterator(ctx context.Context, option SearchIteratorOption, callOptions ...grpc.CallOption) (SearchIterator, error)

Request Parameters

Parameter

Description

Type

ctx

Context for the current call to work.

context.Context

option

Optional parameters of the methods.

SearchIteratorOption

callOptions

Optional parameters for calling the methods.

grpc.CallOption

SearchIteratorOption

This is an interface type. The searchIteratorOption struct type implements this interface.

You can use the NewSearchIteratorOption method to get its concrete implementation.

NewSearchIteratorOption

The signature of this method is as follows:

func NewSearchIteratorOption(collectionName string, vector entity.Vector) *searchIteratorOption

Parameter

Description

Type

collectionName

Name of the target collection.

string

vectors

Query vectors

[]entity.Vector

You can chain the following methods to append more parameters to the searchIteratorOption struct type.

WithBatchSize

This method appends the settings regarding the batchSize parameter to the searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithBatchSize(batchSize int) *searchIteratorOption

Parameter

Description

Type

batchSize

The number of entities to return in each iteration.

int

WithPartitions

This method appends the settings regarding the partitionNames parameter to the searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithPartitions(partitionNames ...string) *searchIteratorOption

Parameter

Description

Type

partitionNames

The names of the target partitions

...string

WithFilter

This method appends the settings regarding the expr parameter to the searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithFilter(expr string) *searchIteratorOption

Parameter

Description

Type

expr

The filtering expression.

string

WithTemplateParam

This method appends the settings regarding the arguments used in the expr parameter to the searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithTemplateParam(key string, val any) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithOffset(offset int) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithOutputFields(fieldNames ...string) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithConsistencyLevel(consistencyLevel entity.ConsistencyLevel) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithANNSField(annsField string) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithGroupByField(groupByField string) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithGroupSize(groupSize int) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithStrictGroupSize(strictGroupSize bool) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithIgnoreGrowing(ignoreGrowing bool) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithAnnParam(ap index.AnnParam) *searchIteratorOption

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 searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithSearchParam(key, value string) *searchIteratorOption

Parameter

Description

Type

key

The name of the argument used in the searchParams parameter

string

value

The value of the specified argument.

any

WithIteratorLimit

This method appends the settings regarding the limit parameter to the searchIteratorOption struct. The signature of this method is as follows:

func (opt *searchIteratorOption) WithIteratorLimit(limit int64) *searchIteratorOption

Parameter

Description

Type

limit

When set, this option limits the total number of entities returned.

Setting this to a value less than 0 removes the limit and returns all entities that match the specified filter expressions.

bool

SearchIterator

This is an interface type. The searchIteratorV2 struct type implement this interface.

You can use the Next method to retrieve search results iteratively.

Next

The signature of this method is as follows:

func (it *searchIteratorV2) Next(ctx context.Context) (ResultSet, error)

Every time you call Next(), a ResultSet will be returned.

Return

SearchIterator

Example

import (
    "context"
    "errors"
    "fmt"
    "io"
    "log"
    "strings"
    "time"

    "golang.org/x/exp/rand"

    "github.com/milvus-io/milvus/client/v2/entity"
    "github.com/milvus-io/milvus/client/v2/index"
    "github.com/milvus-io/milvus/client/v2/milvusclient"
)

c, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
    Address: milvusAddr,
    APIKey:  "root:Milvus",
})

vec := []float32{0.3580376395471989, -0.6023495712049978, 0.18414012509913835, -0.26286205330961354, 0.9029438446296592}
iter, err := c.SearchIterator(ctx, milvusclient.NewSearchIteratorOption(collectionName, entity.FloatVector(vec)).
    WithANNSField("vector").
    WithAnnParam(index.NewIvfAnnParam(16)).
    WithBatchSize(50).
    WithOutputFields("color").
    WithIteratorLimit(20000))
if err != nil {
    // handle error
}

// use the iterator
for {
    rs, err := iter.Next(ctx)
    // end of iterator
    if errors.Is(err, io.EOF) {
        break
    }
    if err != nil {
        // handler error
    }
    fmt.Println(rs)
}

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?