mirror of
https://github.com/milvus-io/milvus.git
synced 2026-07-21 10:15:43 +00:00
enhance: [GOSDK] support run analyzer for go client (#39973)
relate: https://github.com/milvus-io/milvus/issues/39705 --------- Signed-off-by: aoiasd <zhicheng.yue@zilliz.com>
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package entity
|
||||
|
||||
type Token struct {
|
||||
Text string
|
||||
StartOffset int64
|
||||
EndOffset int64
|
||||
Position int64
|
||||
PositionLength int64
|
||||
Hash uint32
|
||||
}
|
||||
|
||||
type AnalyzerResult struct {
|
||||
Tokens []*Token
|
||||
}
|
||||
@@ -229,6 +229,40 @@ func (c *Client) HybridSearch(ctx context.Context, option HybridSearchOption, ca
|
||||
return resultSets, err
|
||||
}
|
||||
|
||||
func (c *Client) RunAnalyzer(ctx context.Context, option RunAnalyzerOption, callOptions ...grpc.CallOption) ([]*entity.AnalyzerResult, error) {
|
||||
req, err := option.Request()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []*entity.AnalyzerResult
|
||||
err = c.callService(func(milvusService milvuspb.MilvusServiceClient) error {
|
||||
resp, err := milvusService.RunAnalyzer(ctx, req, callOptions...)
|
||||
err = merr.CheckRPCCall(resp, err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result = lo.Map(resp.Results, func(result *milvuspb.AnalyzerResult, _ int) *entity.AnalyzerResult {
|
||||
return &entity.AnalyzerResult{
|
||||
Tokens: lo.Map(result.Tokens, func(token *milvuspb.AnalyzerToken, _ int) *entity.Token {
|
||||
return &entity.Token{
|
||||
Text: token.GetToken(),
|
||||
StartOffset: token.GetStartOffset(),
|
||||
EndOffset: token.GetEndOffset(),
|
||||
Position: token.GetPosition(),
|
||||
PositionLength: token.GetPositionLength(),
|
||||
Hash: token.GetHash(),
|
||||
}
|
||||
}),
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func expandWildcard(schema *entity.Schema, outputFields []string) ([]string, bool) {
|
||||
wildcard := false
|
||||
for _, outputField := range outputFields {
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/cockroachdb/errors"
|
||||
"github.com/samber/lo"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
||||
@@ -492,8 +493,7 @@ func (opt *hybridSearchOption) HybridRequest() (*milvuspb.HybridSearchRequest, e
|
||||
|
||||
func NewHybridSearchOption(collectionName string, limit int, annRequests ...*annRequest) *hybridSearchOption {
|
||||
return &hybridSearchOption{
|
||||
collectionName: collectionName,
|
||||
|
||||
collectionName: collectionName,
|
||||
reqs: annRequests,
|
||||
useDefaultConsistency: true,
|
||||
limit: limit,
|
||||
@@ -610,3 +610,42 @@ func NewQueryOption(collectionName string) *queryOption {
|
||||
templateParams: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
type RunAnalyzerOption interface {
|
||||
Request() (*milvuspb.RunAnalyzerRequest, error)
|
||||
}
|
||||
|
||||
type runAnalyzerOption struct {
|
||||
text []string
|
||||
analyzerParams string
|
||||
withDetail bool
|
||||
withHash bool
|
||||
}
|
||||
|
||||
func (opt *runAnalyzerOption) Request() (*milvuspb.RunAnalyzerRequest, error) {
|
||||
return &milvuspb.RunAnalyzerRequest{
|
||||
Placeholder: lo.Map(opt.text, func(str string, _ int) []byte { return []byte(str) }),
|
||||
AnalyzerParams: opt.analyzerParams,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (opt *runAnalyzerOption) WithAnalyzerParams(params string) *runAnalyzerOption {
|
||||
opt.analyzerParams = params
|
||||
return opt
|
||||
}
|
||||
|
||||
func (opt *runAnalyzerOption) WithDetail() *runAnalyzerOption {
|
||||
opt.withDetail = true
|
||||
return opt
|
||||
}
|
||||
|
||||
func (opt *runAnalyzerOption) WithHash() *runAnalyzerOption {
|
||||
opt.withHash = true
|
||||
return opt
|
||||
}
|
||||
|
||||
func NewRunAnalyzerOption(text []string) *runAnalyzerOption {
|
||||
return &runAnalyzerOption{
|
||||
text: text,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -453,3 +453,9 @@ func (mc *MilvusClient) OperatePrivilegeGroup(ctx context.Context, option client
|
||||
err := mc.mClient.OperatePrivilegeGroup(ctx, option, callOptions...)
|
||||
return err
|
||||
}
|
||||
|
||||
// RunAnalyzer run analyzer with params
|
||||
func (mc *MilvusClient) RunAnalyzer(ctx context.Context, option client.RunAnalyzerOption, callOptions ...grpc.CallOption) ([]*entity.AnalyzerResult, error) {
|
||||
tokenSets, err := mc.mClient.RunAnalyzer(ctx, option, callOptions...)
|
||||
return tokenSets, err
|
||||
}
|
||||
|
||||
@@ -1208,3 +1208,28 @@ func TestQueryWithTemplateParamInvalid(t *testing.T) {
|
||||
_, err = mc.Query(ctx, client.NewQueryOption(schema.CollectionName).WithFilter("{_123} > 10").WithTemplateParam("_123", common.DefaultInt64FieldName))
|
||||
common.CheckErr(t, err, false, "cannot parse expression")
|
||||
}
|
||||
|
||||
func TestRunAnalyzer(t *testing.T) {
|
||||
ctx := hp.CreateContext(t, time.Second*common.DefaultTimeout)
|
||||
mc := hp.CreateDefaultMilvusClient(ctx, t)
|
||||
|
||||
// run analyzer with default analyzer
|
||||
tokens, err := mc.RunAnalyzer(ctx, client.NewRunAnalyzerOption([]string{"test doc"}))
|
||||
require.NoError(t, err)
|
||||
for i, text := range []string{"test", "doc"} {
|
||||
require.Equal(t, text, tokens[0].Tokens[i].Text)
|
||||
}
|
||||
|
||||
// run analyzer with invalid params
|
||||
_, err = mc.RunAnalyzer(ctx, client.NewRunAnalyzerOption([]string{"text doc"}).WithAnalyzerParams("invalid params}"))
|
||||
common.CheckErr(t, err, false, "JsonError")
|
||||
|
||||
// run analyzer with custom analyzer
|
||||
tokens, err = mc.RunAnalyzer(ctx, client.NewRunAnalyzerOption([]string{"test doc"}).
|
||||
WithAnalyzerParams(`{"type": "standard", "stop_words": ["test"]}`))
|
||||
|
||||
require.NoError(t, err)
|
||||
for i, text := range []string{"doc"} {
|
||||
require.Equal(t, text, tokens[0].Tokens[i].Text)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user