congqixia 6c8e11da4f
feat: [GoSDK] add QueryIterator support for Go client (#46633)
Related to #31293

Implement QueryIterator for the Go SDK to enable efficient iteration
over large query result sets using PK-based pagination.

Key changes:
- Add QueryIterator interface and implementation with PK-based
pagination
- Support Int64 and VarChar primary key types for pagination filtering
- Add QueryIteratorOption with batchSize, limit, filter, outputFields
config
- Fix ResultSet.Slice to handle Query results without IDs/Scores
- Add comprehensive unit tests and integration tests

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
- Core invariant: the iterator requires the collection primary key (PK)
to be present in outputFields so PK-based pagination and accurate row
counting work. The constructor enforces this by appending the PK to
outputFields when absent, and all pagination (lastPK tracking, PK-range
filters) and ResultCount calculations depend on that guaranteed PK
column.

- New capability: adds a public QueryIterator API (Client.QueryIterator,
QueryIterator interface, QueryIteratorOption) that issues server-side
Query RPCs in configurable batches and implements PK-based pagination
supporting Int64 and VarChar PKs, with options for batchSize, limit,
filter, outputFields and an upfront first-batch validation to fail fast
on invalid params.

- Removed/simplified logic: ResultSet.Slice no longer assumes IDs and
Scores are always present — it branches on presence of IDs (use IDs
length when non-nil; otherwise derive row count from Fields[0]) and
guards Scores slicing. This eliminates redundant/unsafe assumptions and
centralizes correct row-count logic based on actual returned fields.

- No data loss or behavior regression: pagination composes the user
filter with a PK-range filter and always requests the PK field, so
lastPK is extracted from a real column and fetchNextBatch only advances
when rows are returned; EOF is returned only when the server returns no
rows or iterator limit is reached. ResultSet.Slice guards prevent panics
for queries that lack IDs/Scores; Query RPC → ResultSet.Fields remains
the authoritative path for row data, so rows are not dropped and
existing query behavior is preserved.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
2025-12-27 01:43:20 +08:00
..

Go MilvusClient

license Go Reference

Go MilvusClient for Milvus. To contribute code to this project, please read our contribution guidelines first.

Getting started

Prerequisites

Go 1.24.11 or higher

Install Milvus Go SDK

  1. Use go get to install the latest version of the Milvus Go SDK and dependencies:

    go get -u github.com/milvus-io/milvus/client/v2
    
  2. Include the Go MilvusClient in your application:

     import "github.com/milvus-io/milvus/client/v2/milvusclient"
    
     //...other snippet ...
     ctx, cancel := context.WithCancel(context.Background())
     defer cancel()
    
     milvusAddr := "YOUR_MILVUS_ENDPOINT"
    
     cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
     	Address: milvusAddr,
     })
     if err != nil {
     	// handle error
     }
    
     // Do your work with milvus client
    

API Documentation

Refer to https://milvus.io/api-reference/go/v2.5.x/About.md for the Go SDK API documentation.

Code format

The Go source code is formatted using gci & gofumpt. Please run make lint-fix before sumbit a PR.