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 |
|---|---|---|
|
Context for the current call to work. |
|
|
Optional parameters of the methods. |
|
|
Optional parameters for calling the methods. |
|
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 |
|---|---|---|
|
Name of the target collection. |
|
|
Query vectors |
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 |
|---|---|---|
|
The number of entities to return in each iteration. |
|
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 |
|---|---|---|
|
The names of the target partitions |
|
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 |
|---|---|---|
|
The filtering expression. |
|
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 |
|---|---|---|
|
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithOffset(offset int) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithOutputFields(fieldNames ...string) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithConsistencyLevel(consistencyLevel entity.ConsistencyLevel) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithANNSField(annsField string) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithGroupByField(groupByField string) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithGroupSize(groupSize int) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithStrictGroupSize(strictGroupSize bool) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithIgnoreGrowing(ignoreGrowing bool) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithAnnParam(ap index.AnnParam) *searchIteratorOption
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 searchIteratorOption struct. The signature of this method is as follows:
func (opt *searchIteratorOption) WithSearchParam(key, value string) *searchIteratorOption
Parameter |
Description |
Type |
|---|---|---|
|
The name of the argument used in the |
|
|
The value of the specified argument. |
|
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 |
|---|---|---|
|
When set, this option limits the total number of entities returned. Setting this to a value less than |
|
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)
}