Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions client/milvusclient/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ type DeleteResult struct {
}

func (c *Client) Delete(ctx context.Context, option DeleteOption, callOptions ...grpc.CallOption) (DeleteResult, error) {
req := option.Request()

result := DeleteResult{}
err := c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
req, err := option.Request()
if err != nil {
return result, err
}

err = c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
resp, err := milvusService.Delete(ctx, req, callOptions...)
if err = merr.CheckRPCCall(resp, err); err != nil {
return err
Expand Down
25 changes: 24 additions & 1 deletion client/milvusclient/write_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,31 @@ type DeleteOptionSuite struct {
func (s *DeleteOptionSuite) TestBasic() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
opt := NewDeleteOption(collectionName)
req, err := opt.Request()
s.NoError(err)

s.Equal(collectionName, req.CollectionName)
}

func (s *DeleteOptionSuite) TestWithExpr() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
expr := "id > 100"
opt := NewDeleteOption(collectionName).WithExpr(expr)
req, err := opt.Request()
s.NoError(err)

s.Equal(expr, req.Expr)
}

func (s *DeleteOptionSuite) TestWithTemplateParam() {
collectionName := fmt.Sprintf("coll_%s", s.randString(6))
opt := NewDeleteOption(collectionName).WithExpr("id > {v}").WithTemplateParam("v", 100)
req, err := opt.Request()
s.NoError(err)

s.Equal(collectionName, opt.Request().GetCollectionName())
s.Equal("id > {v}", req.Expr)
s.Equal(1, len(req.ExprTemplateValues))
s.Equal(int64(100), req.ExprTemplateValues["v"].GetInt64Val())
}

func TestDeleteOption(t *testing.T) {
Expand Down
48 changes: 41 additions & 7 deletions client/milvusclient/write_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,21 +394,37 @@ func (opt *rowBasedDataOption) WithKeepAutoIDPk(keepPk bool) *rowBasedDataOption
}

type DeleteOption interface {
Request() *milvuspb.DeleteRequest
Request() (*milvuspb.DeleteRequest, error)
}

type deleteOption struct {
collectionName string
partitionName string
expr string
collectionName string
partitionName string
expr string
templateParams map[string]any
hashKeys []uint32
consistencyLevel entity.ConsistencyLevel
}

func (opt *deleteOption) Request() *milvuspb.DeleteRequest {
return &milvuspb.DeleteRequest{
func (opt *deleteOption) Request() (*milvuspb.DeleteRequest, error) {
req := &milvuspb.DeleteRequest{
CollectionName: opt.collectionName,
PartitionName: opt.partitionName,
Expr: opt.expr,
}
req.ExprTemplateValues = make(map[string]*schemapb.TemplateValue)
for key, value := range opt.templateParams {
tmplVal, err := any2TmplValue(value)
if err != nil {
return nil, err
}
req.ExprTemplateValues[key] = tmplVal
}
if len(opt.hashKeys) > 0 {
req.HashKeys = opt.hashKeys
}
req.ConsistencyLevel = opt.consistencyLevel.CommonConsistencyLevel()
return req, nil
}

func (opt *deleteOption) WithExpr(expr string) *deleteOption {
Expand All @@ -431,6 +447,24 @@ func (opt *deleteOption) WithPartition(partitionName string) *deleteOption {
return opt
}

func (opt *deleteOption) WithTemplateParam(key string, val any) *deleteOption {
opt.templateParams[key] = val
return opt
}

func (opt *deleteOption) WithHashKeys(hashKeys []uint32) *deleteOption {
opt.hashKeys = hashKeys
return opt
}

func (opt *deleteOption) WithConsistencyLevel(consistencyLevel entity.ConsistencyLevel) *deleteOption {
opt.consistencyLevel = consistencyLevel
return opt
}

func NewDeleteOption(collectionName string) *deleteOption {
return &deleteOption{collectionName: collectionName}
return &deleteOption{
collectionName: collectionName,
templateParams: make(map[string]any),
}
}
Loading