diff --git a/client/index/index.go b/client/index/index.go index e04b92b3f6..95553dd2ca 100644 --- a/client/index/index.go +++ b/client/index/index.go @@ -68,8 +68,12 @@ func (gi GenericIndex) Params() map[string]string { return m } +func (gi GenericIndex) WithMetricType(metricType MetricType) { + gi.baseIndex.metricType = metricType +} + // NewGenericIndex create generic index instance -func NewGenericIndex(name string, params map[string]string) Index { +func NewGenericIndex(name string, params map[string]string) GenericIndex { return GenericIndex{ baseIndex: baseIndex{ name: name, diff --git a/client/milvusclient/collection_example_test.go b/client/milvusclient/collection_example_test.go index ec6420d200..3621f698f0 100644 --- a/client/milvusclient/collection_example_test.go +++ b/client/milvusclient/collection_example_test.go @@ -154,6 +154,67 @@ func ExampleClient_CreateCollection_ttl() { } } +func ExampleClient_CreateCollection_quickSetup() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + collectionName := `quick_setup_1` + cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{ + Address: milvusAddr, + }) + if err != nil { + // handle err + } + + err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512)) + if err != nil { + // handle error + } +} + +func ExampleClient_CreateCollection_quickSetupWithIndexParams() { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + collectionName := `quick_setup_2` + cli, err := milvusclient.New(ctx, &milvusclient.ClientConfig{ + Address: milvusAddr, + }) + if err != nil { + // handle err + } + + err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512).WithIndexOptions( + milvusclient.NewCreateIndexOption(collectionName, "vector", index.NewHNSWIndex(entity.L2, 64, 128)), + )) + if err != nil { + log.Println(err.Error()) + // handle error + } +} + +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, + }) + if err != nil { + // handle err + } + + err = cli.CreateCollection(ctx, milvusclient.SimpleCreateCollectionOptions(collectionName, 512). + WithVarcharPK(true, 64). + WithShardNum(1), + ) + if err != nil { + log.Println(err.Error()) + // handle error + } +} + func ExampleClient_CreateCollection_consistencyLevel() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -245,7 +306,7 @@ func ExampleClient_RenameCollection() { } } -func ExampleClient_AlterCollection_setTTL() { +func ExampleClient_AlterCollectionProperties_setTTL() { ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/client/milvusclient/collection_options.go b/client/milvusclient/collection_options.go index 1bd8029d63..fc271aa1f2 100644 --- a/client/milvusclient/collection_options.go +++ b/client/milvusclient/collection_options.go @@ -88,7 +88,7 @@ func (opt *createCollectionOption) WithVarcharPK(varcharPK bool, maxLen int) *cr } func (opt *createCollectionOption) WithIndexOptions(indexOpts ...CreateIndexOption) *createCollectionOption { - opt.indexOptions = append(opt.indexOptions, indexOpts...) + opt.indexOptions = indexOpts return opt } @@ -102,6 +102,26 @@ func (opt *createCollectionOption) WithConsistencyLevel(cl entity.ConsistencyLev return opt } +func (opt *createCollectionOption) WithMetricType(metricType entity.MetricType) *createCollectionOption { + opt.metricType = metricType + return opt +} + +func (opt *createCollectionOption) WithPKFieldName(name string) *createCollectionOption { + opt.pkFieldName = name + return opt +} + +func (opt *createCollectionOption) WithVectorFieldName(name string) *createCollectionOption { + opt.vectorFieldName = name + return opt +} + +func (opt *createCollectionOption) WithNumPartitions(numPartitions int64) *createCollectionOption { + opt.numPartitions = numPartitions + return opt +} + func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest { // fast create collection if opt.isFast { @@ -140,12 +160,12 @@ func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest { func (opt *createCollectionOption) Indexes() []CreateIndexOption { // fast create - if opt.isFast { + if opt.isFast && opt.indexOptions == nil { return []CreateIndexOption{ NewCreateIndexOption(opt.name, opt.vectorFieldName, index.NewGenericIndex("", map[string]string{})), } } - return nil + return opt.indexOptions } func (opt *createCollectionOption) IsFast() bool { diff --git a/client/milvusclient/collection_test.go b/client/milvusclient/collection_test.go index fc207ae825..327467ad97 100644 --- a/client/milvusclient/collection_test.go +++ b/client/milvusclient/collection_test.go @@ -104,7 +104,9 @@ func (s *CollectionSuite) TestCreateCollectionOptions() { s.True(collSchema.GetEnableDynamicField()) collectionName = fmt.Sprintf("test_collection_%s", s.randString(6)) - opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false).WithDynamicSchema(false) + opt = SimpleCreateCollectionOptions(collectionName, 128).WithVarcharPK(true, 64).WithAutoID(false). + WithPKFieldName("pk").WithVectorFieldName("embedding").WithMetricType(entity.L2). + WithDynamicSchema(false) req = opt.Request() s.Equal(collectionName, req.GetCollectionName()) s.EqualValues(1, req.GetShardsNum()) diff --git a/client/milvusclient/rbac_options.go b/client/milvusclient/rbac_options.go index cacb72718d..188d77a793 100644 --- a/client/milvusclient/rbac_options.go +++ b/client/milvusclient/rbac_options.go @@ -18,6 +18,7 @@ package milvusclient import ( "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" + "github.com/milvus-io/milvus/pkg/v2/util/crypto" ) type ListUserOption interface { @@ -71,7 +72,7 @@ type createUserOption struct { func (opt *createUserOption) Request() *milvuspb.CreateCredentialRequest { return &milvuspb.CreateCredentialRequest{ Username: opt.userName, - Password: opt.password, + Password: crypto.Base64Encode(opt.password), } } @@ -95,8 +96,8 @@ type updatePasswordOption struct { func (opt *updatePasswordOption) Request() *milvuspb.UpdateCredentialRequest { return &milvuspb.UpdateCredentialRequest{ Username: opt.userName, - OldPassword: opt.oldPassword, - NewPassword: opt.newPassword, + OldPassword: crypto.Base64Encode(opt.oldPassword), + NewPassword: crypto.Base64Encode(opt.newPassword), } } diff --git a/client/milvusclient/rbac_test.go b/client/milvusclient/rbac_test.go index 5a91c28944..8779899b62 100644 --- a/client/milvusclient/rbac_test.go +++ b/client/milvusclient/rbac_test.go @@ -26,6 +26,7 @@ import ( "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" + "github.com/milvus-io/milvus/pkg/v2/util/crypto" "github.com/milvus-io/milvus/pkg/v2/util/merr" ) @@ -102,7 +103,7 @@ func (s *UserSuite) TestCreateUser() { password := s.randString(12) s.mock.EXPECT().CreateCredential(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ccr *milvuspb.CreateCredentialRequest) (*commonpb.Status, error) { s.Equal(userName, ccr.GetUsername()) - s.Equal(password, ccr.GetPassword()) + s.Equal(crypto.Base64Encode(password), ccr.GetPassword()) return merr.Success(), nil }).Once() @@ -121,8 +122,8 @@ func (s *UserSuite) TestUpdatePassword() { newPassword := s.randString(12) s.mock.EXPECT().UpdateCredential(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ucr *milvuspb.UpdateCredentialRequest) (*commonpb.Status, error) { s.Equal(userName, ucr.GetUsername()) - s.Equal(oldPassword, ucr.GetOldPassword()) - s.Equal(newPassword, ucr.GetNewPassword()) + s.Equal(crypto.Base64Encode(oldPassword), ucr.GetOldPassword()) + s.Equal(crypto.Base64Encode(newPassword), ucr.GetNewPassword()) return merr.Success(), nil }).Once()