2025-09-24 16:43:11 +08:00
|
|
|
|
// Package model define model struct of model runtime service
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"modelRT/constants"
|
|
|
|
|
|
"modelRT/diagram"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/RediSearch/redisearch-go/redisearch"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// RedisSearch define func of redis search by input string
|
|
|
|
|
|
func RedisSearch(ctx context.Context, input string) ([]string, error) {
|
2025-09-24 17:26:46 +08:00
|
|
|
|
rdb := diagram.GetRedisClientInstance()
|
|
|
|
|
|
|
2025-09-24 16:43:11 +08:00
|
|
|
|
if input == "" {
|
|
|
|
|
|
// 返回所有 grid 名
|
|
|
|
|
|
return getAllGridKeys(ctx, constants.RedisAllGridSetKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 17:26:46 +08:00
|
|
|
|
gridExist, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, input).Result()
|
|
|
|
|
|
// TODO 处理err
|
|
|
|
|
|
fmt.Println(gridExist, err)
|
|
|
|
|
|
|
|
|
|
|
|
// TODO 判断input是否为一个完整的层级
|
|
|
|
|
|
// 处理 input 不为空、不含有.并且 input 是一个完整的 grid key 的情况
|
|
|
|
|
|
if strings.HasSuffix(input, ".") == false && gridExist {
|
|
|
|
|
|
return []string{"."}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理 input 不为空并且以.结尾的情况
|
|
|
|
|
|
if strings.HasSuffix(input, ".") == true {
|
2025-09-24 16:43:11 +08:00
|
|
|
|
setKey := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, input)
|
|
|
|
|
|
return getSpecificZoneKeys(ctx, setKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 17:26:46 +08:00
|
|
|
|
// // 处理 input 不为空、不含有.并且 input 是一个完整的 grid key 的情况
|
|
|
|
|
|
// if strings.HasSuffix(input, ".") == false && gridExist {
|
|
|
|
|
|
// setKey := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, input)
|
|
|
|
|
|
// return getSpecificZoneKeys(ctx, setKey)
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2025-09-24 16:43:11 +08:00
|
|
|
|
// TODO 默认每次传递的带有.的前缀字符串一定正确
|
|
|
|
|
|
if strings.HasSuffix(input, ".") {
|
|
|
|
|
|
// TODO 首先判断.后是否为空字符串,为空则返回上次层级下的所有key
|
|
|
|
|
|
// TODO 其次进行前缀匹配
|
|
|
|
|
|
parentKey := strings.TrimSuffix(input, ".")
|
|
|
|
|
|
// 直接查询预存的下一级内容
|
|
|
|
|
|
results, err := rdb.ZRange(ctx, "autocomplete:"+parentKey, 0, -1).Result()
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
return results, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO 处理前缀或模糊匹配
|
|
|
|
|
|
// TODO 使用 RediSearch 的 FT.SUGGET 与Fuzzy标志进行前缀或模糊查询
|
|
|
|
|
|
ac := redisearch.NewAutocompleter("localhost:6379", "my-autocomplete-dict")
|
|
|
|
|
|
|
|
|
|
|
|
results, err := ac.SuggestOpts(input, redisearch.SuggestOptions{
|
|
|
|
|
|
Num: 5,
|
|
|
|
|
|
Fuzzy: true,
|
|
|
|
|
|
WithScores: true,
|
|
|
|
|
|
WithPayloads: true,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var terms []string
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
|
terms = append(terms, result.Term)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 返回结果
|
|
|
|
|
|
return terms, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getAllGridKeys(ctx context.Context, setKey string) ([]string, error) {
|
|
|
|
|
|
// TODO 从redis set中获取所有的 grid key
|
|
|
|
|
|
gridSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
|
|
|
|
|
keys, err := gridSets.SMembers("grid_keys")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return []string{}, fmt.Errorf("get all root keys failed, error: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return keys, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getSpecificZoneKeys(ctx context.Context, setKey string) ([]string, error) {
|
|
|
|
|
|
// TODO 从redis set中获取所有的 grid key
|
|
|
|
|
|
zoneSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
|
|
|
|
|
keys, err := zoneSets.SMembers("grid_keys")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return []string{}, fmt.Errorf("get all root keys failed, error: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
return keys, nil
|
|
|
|
|
|
}
|