modelRT/model/object.go

82 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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"
"github.com/redis/go-redis/v9"
)
// RedisSearch define func of redis search by input string
func RedisSearch(ctx context.Context, input string) ([]string, error) {
var rdb redis.Client
if input == "" {
// 返回所有 grid 名
return getAllGridKeys(ctx, constants.RedisAllGridSetKey)
}
// 处理 input 不为空但是不含有.的情况
if strings.Contains(input, ".") == false {
setKey := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, input)
return getSpecificZoneKeys(ctx, setKey)
}
// 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
}