enhance: [2.5][GoSDK] Cherry-pick commits and bump version to 2.5.2 (#41235)

Cherry-pick from master
pr: #41161 #41197 #41234

Related to #41108 #40928

Also this PR bump milvusclient go SDK version to v2.5.2

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/41258/head client/v2.5.2
congqixia 2025-04-11 13:02:26 +08:00 committed by GitHub
parent de6687e27d
commit 89a1159ed5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 572 additions and 7 deletions

View File

@ -18,5 +18,5 @@ package common
const (
// SDKVersion const value for current version
SDKVersion = `2.5.1`
SDKVersion = `2.5.2`
)

View File

@ -0,0 +1,121 @@
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// nolint
package entity_test
import (
"log"
"github.com/milvus-io/milvus/client/v2/entity"
)
func ExampleNewSchema() {
schema := entity.NewSchema()
log.Println(schema)
}
func ExampleSchema_WithField_primaryKey() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_id").
WithDataType(entity.FieldTypeInt64).
WithIsPrimaryKey(true).
WithIsAutoID(false),
)
}
func ExampleSchema_WithField_varcharPK() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_id").
WithDataType(entity.FieldTypeVarChar).
WithIsPrimaryKey(true).
WithMaxLength(100),
)
}
func ExampleSchema_WithField_vectorField() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_vector").
WithDataType(entity.FieldTypeFloatVector).
WithDim(5),
)
}
func ExampleSchema_WithField_varcharField() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_varchar").
WithDataType(entity.FieldTypeVarChar).
WithMaxLength(512),
)
}
func ExampleSchema_WithField_int64Field() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_int64").
WithDataType(entity.FieldTypeInt64),
)
}
func ExampleSchema_WithField_boolField() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_bool").
WithDataType(entity.FieldTypeBool),
)
}
func ExampleSchema_WithField_jsonField() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_json").
WithDataType(entity.FieldTypeJSON),
)
}
func ExampleSchema_WithField_arrayField() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().WithName("my_array").
WithDataType(entity.FieldTypeArray).
WithElementType(entity.FieldTypeInt64).
WithMaxLength(512).
WithMaxCapacity(5),
)
}
func ExampleSchema_WithField_pkAndVector() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("pk").
WithDataType(entity.FieldTypeVarChar).
WithMaxLength(100),
).WithField(entity.NewField().
WithName("dense_vector").
WithDataType(entity.FieldTypeFloatVector).
WithDim(4),
)
}
func ExampleSchema_WithField_binaryVector() {
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("pk").
WithDataType(entity.FieldTypeVarChar).
WithMaxLength(100).
WithIsAutoID(true),
).WithField(entity.NewField().
WithName("binary_vector").
WithDataType(entity.FieldTypeBinaryVector).
WithDim(128),
)
}

View File

@ -0,0 +1,30 @@
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// nolint
package index_test
import (
"log"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
)
func ExampleNewAutoIndex() {
index := index.NewAutoIndex(entity.L2)
log.Println(index)
}

View File

@ -197,7 +197,6 @@ func ExampleClient_CreateCollection_quickSetupCustomize() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
collectionName := `quick_setup_3`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
@ -205,9 +204,13 @@ func ExampleClient_CreateCollection_quickSetupCustomize() {
// handle err
}
err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512).
WithVarcharPK(true, 64).
WithShardNum(1),
err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions("custom_quick_setup", 512).
WithPKFieldName("my_id").
WithVarcharPK(true, 512).
WithVectorFieldName("my_vector").
WithMetricType(entity.L2).
WithShardNum(5).
WithAutoID(true),
)
if err != nil {
log.Println(err.Error())
@ -239,6 +242,132 @@ func ExampleClient_CreateCollection_consistencyLevel() {
}
}
func ExampleClient_CreateCollection_withIndexes() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
collectionName := `customized_setup_5`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}
schema := entity.NewSchema().WithDynamicFieldEnabled(true).
WithField(entity.NewField().WithName("my_id").WithIsAutoID(true).WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)).
WithField(entity.NewField().WithName("my_vector").WithDataType(entity.FieldTypeFloatVector).WithDim(5)).
WithField(entity.NewField().WithName("my_varchar").WithDataType(entity.FieldTypeVarChar).WithMaxLength(512))
idx := index.NewAutoIndex(entity.IP)
indexOption := milvusclient.NewCreateIndexOption("my_dense_collection", "dense_vector", idx)
err = cli.CreateCollection(ctx,
milvusclient.NewCreateCollectionOption(collectionName, schema).
WithIndexOptions(indexOption))
if err != nil {
// handle error
}
}
func ExampleClient_CreateCollection_binaryVector() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
collectionName := `my_binary_collection`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("pk").
WithDataType(entity.FieldTypeVarChar).
WithMaxLength(100).
WithIsAutoID(true),
).WithField(entity.NewField().
WithName("binary_vector").
WithDataType(entity.FieldTypeBinaryVector).
WithDim(128),
)
idx := index.NewAutoIndex(entity.HAMMING)
indexOption := milvusclient.NewCreateIndexOption("my_binary_collection", "binary_vector", idx)
err = cli.CreateCollection(ctx,
milvusclient.NewCreateCollectionOption(collectionName, schema).
WithIndexOptions(indexOption))
if err != nil {
// handle error
}
}
func ExampleClient_CreateCollection_jsonField() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}
schema := entity.NewSchema()
schema.WithField(entity.NewField().
WithName("pk").
WithDataType(entity.FieldTypeInt64).
WithIsAutoID(true),
).WithField(entity.NewField().
WithName("embedding").
WithDataType(entity.FieldTypeFloatVector).
WithDim(3),
).WithField(entity.NewField().
WithName("metadata").
WithDataType(entity.FieldTypeJSON),
)
jsonIndex1 := index.NewJSONPathIndex(index.Inverted, "varchar", `metadata["product_info"]["category"]`)
jsonIndex2 := index.NewJSONPathIndex(index.Inverted, "double", `metadata["price"]`)
indexOpt1 := milvusclient.NewCreateIndexOption("my_json_collection", "meta", jsonIndex1)
indexOpt2 := milvusclient.NewCreateIndexOption("my_json_collection", "meta", jsonIndex2)
vectorIndex := index.NewAutoIndex(entity.COSINE)
indexOpt := milvusclient.NewCreateIndexOption("my_json_collection", "embedding", vectorIndex)
err = cli.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("my_json_collection", schema).
WithIndexOptions(indexOpt1, indexOpt2, indexOpt))
if err != nil {
// handler err
}
}
func ExampleClient_CreateCollection_dynamicSchema() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// collectionName := `my_dynamic_collection`
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}
err = cli.CreateCollection(ctx,
milvusclient.SimpleCreateCollectionOptions("my_dynamic_collection", 5).
WithDynamicSchema(true))
if err != nil {
// handle error
}
}
func ExampleClient_ListCollections() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

View File

@ -49,6 +49,32 @@ func ExampleClient_CreateIndex() {
}
}
func ExampleClient_CreateIndex_jsonPathIndex_dynamicField() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle err
}
jsonPathIndex := index.NewJSONPathIndex(index.Inverted,
"varchar", // cast type
"color", // json path
)
indexTask, err := cli.CreateIndex(ctx, milvusclient.NewCreateIndexOption("my_dynamic_collection", "color", jsonPathIndex))
if err != nil {
// handler err
}
err = indexTask.Await(ctx)
if err != nil {
// handler err
}
}
func ExampleClient_DescribeIndex() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

View File

@ -0,0 +1,71 @@
// Licensed to the LF AI & Data foundation under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// nolint
package milvusclient_test
import (
"context"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
func ExampleClient_GrantPrivilege() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle error
}
defer cli.Close(ctx)
readOnlyPrivileges := []*entity.RoleGrants{
{Object: "Global", ObjectName: "*", PrivilegeName: "DescribeCollection"},
{Object: "Global", ObjectName: "*", PrivilegeName: "ShowCollections"},
{Object: "Collection", ObjectName: "quick_setup", PrivilegeName: "Search"},
}
for _, grantItem := range readOnlyPrivileges {
err := cli.GrantPrivilege(ctx, milvusclient.NewGrantPrivilegeOption("my_role", grantItem.Object, grantItem.PrivilegeName, grantItem.ObjectName))
if err != nil {
// handle error
}
}
}
func ExampleClient_GrantPrivilegeV2() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle error
}
defer cli.Close(ctx)
err = cli.GrantPrivilegeV2(ctx, milvusclient.NewGrantPrivilegeV2Option("my_role", "Search", "quick_setup"))
if err != nil {
// handle error
}
}

View File

@ -186,6 +186,7 @@ func (c *Client) Query(ctx context.Context, option QueryOption, callOptions ...g
return err
}
resultSet = ResultSet{
sch: collection.Schema,
Fields: columns,
}
if len(columns) > 0 {

View File

@ -24,6 +24,7 @@ import (
"github.com/milvus-io/milvus/client/v2/column"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
@ -201,6 +202,75 @@ func ExampleClient_Search_offsetLimit() {
}
}
func ExampleClient_Search_jsonExpr() {
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)
}
}
func ExampleClient_Search_binaryVector() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "127.0.0.1:19530"
token := "root:Milvus"
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"))
}
}
func ExampleClient_Get() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@ -250,6 +320,54 @@ func ExampleClient_Query() {
fmt.Println(rs.GetColumn("id"))
}
func ExampleClient_Query_jsonExpr_notnull() {
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)
rs, err := cli.Query(ctx, milvusclient.NewQueryOption("my_json_collection").
WithFilter("metadata is not null").
WithOutputFields("metadata", "pk"))
if err != nil {
// handle error
}
fmt.Println(rs.GetColumn("pk"))
fmt.Println(rs.GetColumn("metadata"))
}
func ExampleClient_Query_jsonExpr_leafChild() {
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)
rs, err := cli.Query(ctx, milvusclient.NewQueryOption("my_json_collection").
WithFilter(`metadata["product_info"]["category"] == "electronics"`).
WithOutputFields("metadata", "pk"))
if err != nil {
// handle error
}
fmt.Println(rs.GetColumn("pk"))
fmt.Println(rs.GetColumn("metadata"))
}
func ExampleClient_HybridSearch() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

View File

@ -223,8 +223,9 @@ func (s *ReadSuite) TestQuery() {
return &milvuspb.QueryResults{}, nil
}).Once()
_, err := s.client.Query(ctx, NewQueryOption(collectionName).WithPartitions(partitionName))
rs, err := s.client.Query(ctx, NewQueryOption(collectionName).WithPartitions(partitionName))
s.NoError(err)
s.NotNil(rs.sch)
})
s.Run("bad_request", func() {

View File

@ -21,6 +21,7 @@ import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/column"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
@ -59,6 +60,73 @@ func ExampleClient_Insert_columnbase() {
fmt.Println(resp)
}
func ExampleClient_Insert_binaryVector() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle error
}
defer cli.Close(ctx)
resp, err := cli.Insert(ctx, milvusclient.NewColumnBasedInsertOption("quick_setup").
WithBinaryVectorColumn("binary_vector", 128, [][]byte{
{0b10011011, 0b01010100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0b10011011, 0b01010101, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}))
if err != nil {
// handle err
}
fmt.Println(resp)
}
func ExampleClient_Insert_jsonData() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
// handle error
}
defer cli.Close(ctx)
resp, err := cli.Insert(ctx, milvusclient.NewColumnBasedInsertOption("my_json_collection").
WithInt64Column("pk", []int64{1, 2, 3, 4}).
WithFloatVectorColumn("embedding", 3, [][]float32{
{0.12, 0.34, 0.56},
{0.56, 0.78, 0.90},
{0.91, 0.18, 0.23},
{0.56, 0.38, 0.21},
}).WithColumns(
column.NewColumnJSONBytes("metadata", [][]byte{
[]byte(`{
"product_info": {"category": "electronics", "brand": "BrandA"},
"price": 99.99,
"in_stock": True,
"tags": ["summer_sale"]
}`),
[]byte(`null`),
[]byte(`null`),
[]byte(`"metadata": {
"product_info": {"category": None, "brand": "BrandB"},
"price": 59.99,
"in_stock": None
}`),
}),
))
if err != nil {
// handle err
}
fmt.Println(resp)
}
func ExampleClient_Upsert_columnBase() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

View File

@ -119,7 +119,7 @@ func AnyToColumns(rows []interface{}, schemas ...*entity.Schema) ([]column.Colum
data := make([][]byte, 0, rowsLen)
col = column.NewColumnJSONBytes(field.Name, data)
case entity.FieldTypeArray:
col := NewArrayColumn(field)
col = NewArrayColumn(field)
if col == nil {
return nil, errors.Newf("unsupported element type %s for Array", field.ElementType.String())
}