optimize code of redis search query
This commit is contained in:
parent
151f7f22c5
commit
5f5eb22b39
|
|
@ -5,5 +5,20 @@ const (
|
||||||
// RedisAllGridSetKey define redis set key which store all grid keys
|
// RedisAllGridSetKey define redis set key which store all grid keys
|
||||||
RedisAllGridSetKey = "grid_keys"
|
RedisAllGridSetKey = "grid_keys"
|
||||||
// RedisSpecGridZoneSetKey define redis set key which store all zone keys under specific grid
|
// RedisSpecGridZoneSetKey define redis set key which store all zone keys under specific grid
|
||||||
RedisSpecGridZoneSetKey = "grid_%s_zones_keys" // grid_{grid}_zones_keys
|
RedisSpecGridZoneSetKey = "grid_%s_zones_keys"
|
||||||
|
|
||||||
|
// RedisAllZoneSetKey define redis set key which store all zone keys
|
||||||
|
RedisAllZoneSetKey = "zone_keys"
|
||||||
|
// RedisSpecZoneStationSetKey define redis set key which store all station keys under specific zone
|
||||||
|
RedisSpecZoneStationSetKey = "zone_%s_stations_keys"
|
||||||
|
|
||||||
|
// RedisAllStationSetKey define redis set key which store all station keys
|
||||||
|
RedisAllStationSetKey = "station_keys"
|
||||||
|
// RedisSpecStationComponentSetKey define redis set key which store all component keys under specific station
|
||||||
|
RedisSpecStationComponentSetKey = "station_%s_components_keys"
|
||||||
|
|
||||||
|
// RedisAllComponentSetKey define redis set key which store all component keys
|
||||||
|
RedisAllComponentSetKey = "component_keys"
|
||||||
|
// RedisSpecComponentSetKey define redis set key which store all component keys under specific zone
|
||||||
|
RedisSpecComponentSetKey = "zone_%s_components_keys"
|
||||||
)
|
)
|
||||||
|
|
|
||||||
135
model/object.go
135
model/object.go
|
|
@ -8,12 +8,13 @@ import (
|
||||||
|
|
||||||
"modelRT/constants"
|
"modelRT/constants"
|
||||||
"modelRT/diagram"
|
"modelRT/diagram"
|
||||||
|
"modelRT/logger"
|
||||||
|
|
||||||
"github.com/RediSearch/redisearch-go/redisearch"
|
"github.com/RediSearch/redisearch-go/redisearch"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RedisSearch define func of redis search by input string
|
// RedisSearchRecommend define func of redis search by input string and return recommend results
|
||||||
func RedisSearch(ctx context.Context, input string) ([]string, error) {
|
func RedisSearchRecommend(ctx context.Context, input string) ([]string, error) {
|
||||||
rdb := diagram.GetRedisClientInstance()
|
rdb := diagram.GetRedisClientInstance()
|
||||||
|
|
||||||
if input == "" {
|
if input == "" {
|
||||||
|
|
@ -21,63 +22,78 @@ func RedisSearch(ctx context.Context, input string) ([]string, error) {
|
||||||
return getAllGridKeys(ctx, constants.RedisAllGridSetKey)
|
return getAllGridKeys(ctx, constants.RedisAllGridSetKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
gridExist, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, input).Result()
|
inputs := strings.Split(input, ".")
|
||||||
// TODO 处理err
|
inputLen := len(inputs)
|
||||||
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 {
|
|
||||||
setKey := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, input)
|
|
||||||
return getSpecificZoneKeys(ctx, setKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// // 处理 input 不为空、不含有.并且 input 是一个完整的 grid key 的情况
|
|
||||||
// if strings.HasSuffix(input, ".") == false && gridExist {
|
|
||||||
// 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")
|
ac := redisearch.NewAutocompleter("localhost:6379", "my-autocomplete-dict")
|
||||||
|
|
||||||
results, err := ac.SuggestOpts(input, redisearch.SuggestOptions{
|
switch inputLen {
|
||||||
Num: 5,
|
case 1:
|
||||||
Fuzzy: true,
|
gridExist, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, input).Result()
|
||||||
WithScores: true,
|
if err != nil {
|
||||||
WithPayloads: true,
|
logger.Error(ctx, "check grid key exist failed ", "grid_key", input, "error", err)
|
||||||
})
|
return []string{}, err
|
||||||
if err != nil {
|
}
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var terms []string
|
if !gridExist {
|
||||||
for _, result := range results {
|
results, err := ac.SuggestOpts(input, redisearch.SuggestOptions{
|
||||||
terms = append(terms, result.Term)
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
var grids []string
|
||||||
|
for _, result := range results {
|
||||||
|
grids = append(grids, result.Term)
|
||||||
|
}
|
||||||
|
// 返回模糊查询结果
|
||||||
|
return grids, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
// 返回结果
|
return []string{}, nil
|
||||||
return terms, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllGridKeys(ctx context.Context, setKey string) ([]string, error) {
|
func getAllGridKeys(ctx context.Context, setKey string) ([]string, error) {
|
||||||
// TODO 从redis set中获取所有的 grid key
|
// 从redis set 中获取所有的 grid key
|
||||||
gridSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
gridSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||||
keys, err := gridSets.SMembers("grid_keys")
|
keys, err := gridSets.SMembers("grid_keys")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -87,7 +103,7 @@ func getAllGridKeys(ctx context.Context, setKey string) ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSpecificZoneKeys(ctx context.Context, setKey string) ([]string, error) {
|
func getSpecificZoneKeys(ctx context.Context, setKey string) ([]string, error) {
|
||||||
// TODO 从redis set中获取所有的 grid key
|
// TODO 从redis set 中获取指定 grid 下的 zone key
|
||||||
zoneSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
zoneSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||||
keys, err := zoneSets.SMembers("grid_keys")
|
keys, err := zoneSets.SMembers("grid_keys")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -95,3 +111,18 @@ func getSpecificZoneKeys(ctx context.Context, setKey string) ([]string, error) {
|
||||||
}
|
}
|
||||||
return keys, nil
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue