optimize code of RedissearchRecommend func
This commit is contained in:
parent
5f5eb22b39
commit
0d7890f6aa
|
|
@ -25,8 +25,6 @@ func RedisSearchRecommend(ctx context.Context, input string) ([]string, error) {
|
|||
inputs := strings.Split(input, ".")
|
||||
inputLen := len(inputs)
|
||||
|
||||
ac := redisearch.NewAutocompleter("localhost:6379", "my-autocomplete-dict")
|
||||
|
||||
switch inputLen {
|
||||
case 1:
|
||||
gridExist, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, input).Result()
|
||||
|
|
@ -36,6 +34,8 @@ func RedisSearchRecommend(ctx context.Context, input string) ([]string, error) {
|
|||
}
|
||||
|
||||
if !gridExist {
|
||||
// TODO 检测name是不是 tire 树名
|
||||
ac := redisearch.NewAutocompleter("localhost:6379", "my-autocomplete-dict")
|
||||
results, err := ac.SuggestOpts(input, redisearch.SuggestOptions{
|
||||
// TODO 测试如何返回全部的可能模糊结果
|
||||
Num: 5,
|
||||
|
|
@ -44,7 +44,7 @@ func RedisSearchRecommend(ctx context.Context, input string) ([]string, error) {
|
|||
WithPayloads: true,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query info by fuzzy failed", "grid_key", input, "error", err)
|
||||
logger.Error(ctx, "query info by fuzzy failed", "query_key", input, "error", err)
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
|
|
@ -68,12 +68,17 @@ func RedisSearchRecommend(ctx context.Context, input string) ([]string, error) {
|
|||
}
|
||||
default:
|
||||
lastToken := inputs[inputLen-1]
|
||||
// TODO 判断queryKey是否是空值,空值则返回上一级别下的所有key
|
||||
// 判断 queryKey 是否是空值,空值则返回上一级别下的所有key
|
||||
if lastToken == "" {
|
||||
setKey := getConstantsKeyByLength(inputLen - 1)
|
||||
targetSet := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||
// TODO 将 SMembers 返回的值处理后返回
|
||||
targetSet.SMembers(setKey)
|
||||
|
||||
results, err := targetSet.SMembers(setKey)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "get all recommend key by setKey failed", "set_key", setKey, "error", err)
|
||||
return []string{}, fmt.Errorf("get all recommend key by setKey failed,%w", err)
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
querykey := lastToken
|
||||
|
|
@ -84,8 +89,29 @@ func RedisSearchRecommend(ctx context.Context, input string) ([]string, error) {
|
|||
logger.Error(ctx, "check keys exist failed", "set_key", setKey, "query_key", querykey, "error", err)
|
||||
return []string{}, fmt.Errorf("check keys failed,%w", err)
|
||||
}
|
||||
|
||||
if !exist {
|
||||
fmt.Println("use fuzzy query")
|
||||
// TODO 检测name是不是 tire 树名
|
||||
ac := redisearch.NewAutocompleter("localhost:6379", "my-autocomplete-dict")
|
||||
results, err := ac.SuggestOpts(input, redisearch.SuggestOptions{
|
||||
// TODO 测试如何返回全部的可能模糊结果
|
||||
Num: 5,
|
||||
Fuzzy: true,
|
||||
WithScores: true,
|
||||
WithPayloads: true,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query info by fuzzy failed", "query_key", input, "error", err)
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
var terms []string
|
||||
for _, result := range results {
|
||||
terms = append(terms, result.Term)
|
||||
}
|
||||
// 返回模糊查询结果
|
||||
return terms, nil
|
||||
}
|
||||
return []string{}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue