Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add WithCollectionProperties option and support the back field name for PKs2Expr #400

Merged
merged 1 commit into from
Feb 20, 2023
Merged
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
2 changes: 1 addition & 1 deletion client/client_grpc_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *GrpcClient) connect(ctx context.Context, addr string, opts ...grpc.Dial
if addr == "" {
return fmt.Errorf("address is empty")
}
conn, err := grpc.Dial(addr, opts...)
conn, err := grpc.DialContext(ctx, addr, opts...)
if err != nil {
return err
}
Expand Down
16 changes: 10 additions & 6 deletions client/client_grpc_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ func (c *GrpcClient) DeleteByPks(ctx context.Context, collName string, partition

pkf := getPKField(coll.Schema)
// pkf shall not be nil since is returned from milvus
if pkf.Name != ids.Name() {
if ids.Name() != "" && pkf.Name != ids.Name() {
return errors.New("only delete by primary key is supported now")
}

expr := PKs2Expr(ids)
expr := PKs2Expr(pkf.Name, ids)

req := &server.DeleteRequest{
DbName: "",
Expand Down Expand Up @@ -287,17 +287,21 @@ func (c *GrpcClient) Search(ctx context.Context, collName string, partitions []s
return sr, nil
}

func PKs2Expr(ids entity.Column) string {
func PKs2Expr(backName string, ids entity.Column) string {
var expr string
var pkName = ids.Name()
if ids.Name() == "" {
pkName = backName
}
switch ids.Type() {
case entity.FieldTypeInt64:
expr = fmt.Sprintf("%s in %s", ids.Name(), strings.Join(strings.Fields(fmt.Sprint(ids.FieldData().GetScalars().GetLongData().GetData())), ","))
expr = fmt.Sprintf("%s in %s", pkName, strings.Join(strings.Fields(fmt.Sprint(ids.FieldData().GetScalars().GetLongData().GetData())), ","))
case entity.FieldTypeVarChar:
data := ids.FieldData().GetScalars().GetData().(*schema.ScalarField_StringData).StringData.Data
for i := range data {
data[i] = fmt.Sprintf("\"%s\"", data[i])
}
expr = fmt.Sprintf("%s in %s", ids.Name(), strings.Join(strings.Fields(fmt.Sprint(data)), ","))
expr = fmt.Sprintf("%s in %s", pkName, strings.Join(strings.Fields(fmt.Sprint(data)), ","))
}
return expr
}
Expand All @@ -315,7 +319,7 @@ func (c *GrpcClient) QueryByPks(ctx context.Context, collectionName string, part
return nil, errors.New("only int64 and varchar column can be primary key for now")
}

expr := PKs2Expr(ids)
expr := PKs2Expr("", ids)

return c.Query(ctx, collectionName, partitionNames, expr, outputFields, opts...)
}
Expand Down
7 changes: 7 additions & 0 deletions client/client_grpc_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"errors"
"fmt"

common "github.com/milvus-io/milvus-proto/go-api/commonpb"
server "github.com/milvus-io/milvus-proto/go-api/milvuspb"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
)
Expand All @@ -29,6 +30,12 @@ func WithConsistencyLevel(cl entity.ConsistencyLevel) CreateCollectionOption {
}
}

func WithCollectionProperty(key string, value string) CreateCollectionOption {
return func(req *server.CreateCollectionRequest) {
req.Properties = append(req.Properties, &common.KeyValuePair{Key: key, Value: value})
}
}

// LoadCollectionOption is an option that is used to modify LoadCollectionRequest
type LoadCollectionOption func(*server.LoadCollectionRequest)

Expand Down