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"
|
2025-09-25 16:39:45 +08:00
|
|
|
|
"modelRT/logger"
|
2025-09-24 16:43:11 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/RediSearch/redisearch-go/redisearch"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
// RedisSearchRecommend define func of redis search by input string and return recommend results
|
|
|
|
|
|
func RedisSearchRecommend(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-25 16:39:45 +08:00
|
|
|
|
inputs := strings.Split(input, ".")
|
|
|
|
|
|
inputLen := len(inputs)
|
2025-09-24 17:26:46 +08:00
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
ac := redisearch.NewAutocompleter("localhost:6379", "my-autocomplete-dict")
|
2025-09-24 17:26:46 +08:00
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
switch inputLen {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
gridExist, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, input).Result()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Error(ctx, "check grid key exist failed ", "grid_key", input, "error", err)
|
|
|
|
|
|
return []string{}, err
|
|
|
|
|
|
}
|
2025-09-24 16:43:11 +08:00
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
if !gridExist {
|
|
|
|
|
|
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", "grid_key", input, "error", err)
|
|
|
|
|
|
return []string{}, err
|
|
|
|
|
|
}
|
2025-09-24 16:43:11 +08:00
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
var grids []string
|
|
|
|
|
|
for _, result := range results {
|
|
|
|
|
|
grids = append(grids, result.Term)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 返回模糊查询结果
|
|
|
|
|
|
return grids, nil
|
|
|
|
|
|
}
|
2025-09-24 16:43:11 +08:00
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
// 处理 input 不为空、不含有.并且 input 是一个完整的 grid key 的情况
|
|
|
|
|
|
if strings.HasSuffix(input, ".") == false {
|
|
|
|
|
|
return []string{"."}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理 input 不为空并且以.结尾的情况
|
|
|
|
|
|
if strings.HasSuffix(input, ".") == true {
|
|
|
|
|
|
setKey := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, input)
|
|
|
|
|
|
return getSpecificZoneKeys(ctx, setKey)
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
|
|
|
|
|
lastToken := inputs[inputLen-1]
|
|
|
|
|
|
// TODO 判断queryKey是否是空值,空值则返回上一级别下的所有key
|
|
|
|
|
|
if lastToken == "" {
|
|
|
|
|
|
setKey := getConstantsKeyByLength(inputLen - 1)
|
|
|
|
|
|
targetSet := diagram.NewRedisSet(ctx, setKey, 10, true)
|
|
|
|
|
|
// TODO 将 SMembers 返回的值处理后返回
|
|
|
|
|
|
targetSet.SMembers(setKey)
|
|
|
|
|
|
}
|
2025-09-24 16:43:11 +08:00
|
|
|
|
|
2025-09-25 16:39:45 +08:00
|
|
|
|
querykey := lastToken
|
|
|
|
|
|
setKey := getConstantsKeyByLength(inputLen)
|
|
|
|
|
|
targetSet := diagram.NewRedisSet(ctx, setKey, 10, true)
|
|
|
|
|
|
exist, err := targetSet.SIsMember(setKey, querykey)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
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")
|
|
|
|
|
|
}
|
|
|
|
|
|
return []string{}, nil
|
2025-09-24 16:43:11 +08:00
|
|
|
|
}
|
2025-09-25 16:39:45 +08:00
|
|
|
|
return []string{}, nil
|
2025-09-24 16:43:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getAllGridKeys(ctx context.Context, setKey string) ([]string, error) {
|
2025-09-25 16:39:45 +08:00
|
|
|
|
// 从redis set 中获取所有的 grid key
|
2025-09-24 16:43:11 +08:00
|
|
|
|
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) {
|
2025-09-25 16:39:45 +08:00
|
|
|
|
// TODO 从redis set 中获取指定 grid 下的 zone key
|
2025-09-24 16:43:11 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2025-09-25 16:39:45 +08:00
|
|
|
|
|
|
|
|
|
|
func getConstantsKeyByLength(inputLen int) string {
|
|
|
|
|
|
switch inputLen {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
return constants.RedisAllGridSetKey
|
|
|
|
|
|
case 2:
|
|
|
|
|
|
return constants.RedisAllZoneSetKey
|
|
|
|
|
|
case 3:
|
|
|
|
|
|
return constants.RedisAllStationSetKey
|
|
|
|
|
|
case 4:
|
|
|
|
|
|
return constants.RedisAllComponentSetKey
|
|
|
|
|
|
default:
|
|
|
|
|
|
return constants.RedisAllGridSetKey
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|