diff --git a/deploy/deploy.md b/deploy/deploy.md index 8f06fbc..b07f573 100644 --- a/deploy/deploy.md +++ b/deploy/deploy.md @@ -128,12 +128,12 @@ VALUES ( CURRENT_TIMESTAMP ); -INSERT INTO public.component (global_uuid, nspath, tag, name, model_name, description, grid, zone, station, type, in_service, state, status, connection, label, context, op, ts) +INSERT INTO public.component (global_uuid, nspath, tag, name, model_name, description, grid, zone, station, station_id, type, in_service, state, status, connection, label, context, op, ts) VALUES ( '968dd6e6-faec-4f78-b58a-d6e68426b09e', 'ns1', 'tag1', 'component1', '', '', - 'grid1', 'zone1', 'station1', + 'grid1', 'zone1', 'station1', 1, -1, false, -1, -1, @@ -145,8 +145,21 @@ VALUES ), ( '968dd6e6-faec-4f78-b58a-d6e68426b08e', - 'ns1', 'tag2', 'component2', '', '', - 'grid1', 'zone1', 'station1', + 'ns2', 'tag2', 'component2', '', '', + 'grid1', 'zone1', 'station1', 1, + -1, + false, + -1, -1, + '{}', + '{}', + '{}', + -1, + CURRENT_TIMESTAMP +), +( + '968dd6e6-faec-4f78-b58a-d6e88426b09e', + 'ns3', 'tag3', 'component3', '', '', + 'grid1', 'zone1', 'station2', 2, -1, false, -1, -1, diff --git a/model/measurement_group_recommend_model.go b/model/measurement_group_recommend_model.go index c90c0cc..eda5062 100644 --- a/model/measurement_group_recommend_model.go +++ b/model/measurement_group_recommend_model.go @@ -45,10 +45,12 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement } compTagToNSPathPath := make(map[string]string) + isLocalCompTagToNSPathPath := make(map[string]string) for nsPath, compTags := range measSet.CompNSPathToCompTags { stationPath := compNSPathToStationPath[nsPath] for _, compTag := range compTags { compTagToNSPathPath[compTag] = fmt.Sprintf("%s.%s", stationPath, nsPath) + isLocalCompTagToNSPathPath[compTag] = nsPath } } @@ -78,6 +80,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement sug := make([]redisearch.Suggestion, 0, len(zoneTags)) for _, zoneTag := range zoneTags { term := fmt.Sprintf("%s.%s", gridTag, zoneTag) + // add redis fuzzy search suggestion for token1-token7 type sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore}) } safeSAdd(fmt.Sprintf(constants.RedisSpecGridZoneSetKey, gridTag), zoneTags) @@ -95,6 +98,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement } for _, stationTag := range stationTags { + // add redis fuzzy search suggestion for token1-token7 type term := fmt.Sprintf("%s.%s.%s", gridTag, zoneTag, stationTag) sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore}) } @@ -105,7 +109,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement // building the station -> component nspaths hierarchy for stationTag, compNSPaths := range measSet.StationToCompNSPaths { - sug := make([]redisearch.Suggestion, 0, len(compNSPaths)) + sug := make([]redisearch.Suggestion, 0, len(compNSPaths)*2) parentPath, exists := stationToZonePath[stationTag] if !exists { err := fmt.Errorf("station tag to zone tag mapping not found for stationTag: %s", stationTag) @@ -114,8 +118,11 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement } for _, nsPath := range compNSPaths { + // add redis fuzzy search suggestion for token1-token7 type term := fmt.Sprintf("%s.%s.%s", parentPath, stationTag, nsPath) sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore}) + // add redis fuzzy search suggestion for token4-token7 type + sug = append(sug, redisearch.Suggestion{Term: nsPath, Score: constants.DefaultScore}) } safeSAdd(fmt.Sprintf(constants.RedisSpecStationCompNSPATHSetKey, stationTag), compNSPaths) ac.AddTerms(sug...) @@ -123,7 +130,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement // building the component nspath -> component tags hierarchy for compNSPath, compTags := range measSet.CompNSPathToCompTags { - sug := make([]redisearch.Suggestion, 0, len(compTags)) + sug := make([]redisearch.Suggestion, 0, len(compTags)*2) parentPath, exists := compNSPathToStationPath[compNSPath] if !exists { err := fmt.Errorf("component nspath tag to station tag mapping not found for compNSPath: %s", compNSPath) @@ -135,8 +142,12 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement fullPath := fmt.Sprintf("%s.%s.%s", parentPath, compNSPath, compTag) compTagToFullPath[compTag] = fullPath + // add redis fuzzy search suggestion for token1-token7 type term := fullPath sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore}) + // add redis fuzzy search suggestion for token4-token7 type + term = fmt.Sprintf("%s.%s", compNSPath, compTag) + sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore}) } safeSAdd(fmt.Sprintf(constants.RedisSpecCompNSPathCompTagSetKey, compNSPath), compTags) ac.AddTerms(sug...) @@ -144,7 +155,7 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement // building the component tag -> measurement tags hierarchy for compTag, measTags := range measSet.CompTagToMeasTags { - sug := make([]redisearch.Suggestion, 0, len(measTags)) + sug := make([]redisearch.Suggestion, 0, len(measTags)*4) parentPath, exists := compTagToNSPathPath[compTag] if !exists { err := fmt.Errorf("component tag to component nspath mapping not found for compTag: %s", compTag) @@ -152,12 +163,26 @@ func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.Measurement return nil, err } + isLocalParentPath, exists := isLocalCompTagToNSPathPath[compTag] + if !exists { + err := fmt.Errorf("component tag to component nspath is local mapping not found for compTag: %s", compTag) + logger.Error(ctx, "component tag to component nspath is local mapping not found", "compTag", compTag, "error", err) + return nil, err + } + for _, measTag := range measTags { + // add redis fuzzy search suggestion for token1-token7 type configTerm := fmt.Sprintf("%s.%s.%s", parentPath, compTag, "bay") sug = append(sug, redisearch.Suggestion{Term: configTerm, Score: constants.DefaultScore}) measTerm := fmt.Sprintf("%s.%s.%s.%s", parentPath, compTag, "bay", measTag) sug = append(sug, redisearch.Suggestion{Term: measTerm, Score: constants.DefaultScore}) + // add redis fuzzy search suggestion for token4-token7 type + configTerm = fmt.Sprintf("%s.%s.%s", isLocalParentPath, compTag, "bay") + sug = append(sug, redisearch.Suggestion{Term: configTerm, Score: constants.DefaultScore}) + + measTerm = fmt.Sprintf("%s.%s.%s.%s", isLocalParentPath, compTag, "bay", measTag) + sug = append(sug, redisearch.Suggestion{Term: measTerm, Score: constants.DefaultScore}) } safeSAdd(fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, compTag), measTags) ac.AddTerms(sug...) diff --git a/model/redis_recommend.go b/model/redis_recommend.go index 7203f3b..e0ac507 100644 --- a/model/redis_recommend.go +++ b/model/redis_recommend.go @@ -34,7 +34,7 @@ func InitAutocompleterWithPool(pool *redigo.Pool) { ac = redisearch.NewAutocompleterFromPool(pool, constants.RedisSearchDictName) } -func levelOneRedisSearch(ctx context.Context, rdb *redis.Client, hierarchy constants.RecommendHierarchyType, searchInput string, searchRedisKey string, fanInChan chan SearchResult) { +func levelOneRedisSearch(ctx context.Context, rdb *redis.Client, hierarchy constants.RecommendHierarchyType, recommendLenType string, searchInput string, searchRedisKey string, fanInChan chan SearchResult) { defer func() { if r := recover(); r != nil { logger.Error(ctx, "searchFunc panicked", "panic", r) @@ -72,7 +72,7 @@ func levelOneRedisSearch(ctx context.Context, rdb *redis.Client, hierarchy const } // process fuzzy search result - recommends, err := runFuzzySearch(ctx, searchInput, "", hierarchy) + recommends, err := runFuzzySearch(ctx, rdb, searchInput, "", hierarchy, recommendLenType) if err != nil { logger.Error(ctx, fmt.Sprintf("fuzzy search failed for %s hierarchical", util.GetLevelStrByRdsKey(searchRedisKey)), "search_input", searchInput, "error", err) fanInChan <- SearchResult{ @@ -151,9 +151,9 @@ func RedisSearchRecommend(ctx context.Context, input string) map[string]SearchRe case 1: searchInput := inputSlice[0] // grid tagname search - go levelOneRedisSearch(ctx, rdb, constants.GridRecommendHierarchyType, searchInput, constants.RedisAllGridSetKey, fanInChan) + go levelOneRedisSearch(ctx, rdb, constants.GridRecommendHierarchyType, constants.FullRecommendLength, searchInput, constants.RedisAllGridSetKey, fanInChan) // component nspath search - go levelOneRedisSearch(ctx, rdb, constants.CompNSPathRecommendHierarchyType, searchInput, constants.RedisAllCompNSPathSetKey, fanInChan) + go levelOneRedisSearch(ctx, rdb, constants.CompNSPathRecommendHierarchyType, constants.IsLocalRecommendLength, searchInput, constants.RedisAllCompNSPathSetKey, fanInChan) results := make(map[string]SearchResult) // TODO 后续根据支持的数据标识语法长度,进行值的变更 @@ -168,6 +168,11 @@ func RedisSearchRecommend(ctx context.Context, input string) map[string]SearchRe filterResults := make([]string, 0, len(result.QueryDatas)) // TODO 增加 nspath 过滤 for _, queryData := range result.QueryDatas { + if queryData == "." { + filterResults = append(filterResults, queryData) + continue + } + var nsPath string if lastDotIndex := strings.LastIndex(queryData, "."); lastDotIndex == -1 { nsPath = queryData @@ -523,7 +528,7 @@ func handleLevelFuzzySearch(ctx context.Context, rdb *redis.Client, hierarchy co } // start redis fuzzy search - recommends, err := runFuzzySearch(ctx, searchInput, searchPrefix, hierarchy) + recommends, err := runFuzzySearch(ctx, rdb, searchInput, searchPrefix, hierarchy, recommendLenType) if err != nil { logger.Error(ctx, "fuzzy search failed by hierarchy", "hierarchy", hierarchy, "search_input", searchInput, "error", err) fanInChan <- SearchResult{ @@ -549,7 +554,7 @@ func handleLevelFuzzySearch(ctx context.Context, rdb *redis.Client, hierarchy co } // runFuzzySearch define func to process redis fuzzy search -func runFuzzySearch(ctx context.Context, searchInput string, searchPrefix string, hierarchy constants.RecommendHierarchyType) ([]string, error) { +func runFuzzySearch(ctx context.Context, rdb *redis.Client, searchInput string, searchPrefix string, hierarchy constants.RecommendHierarchyType, recommendLenType string) ([]string, error) { var configToken string var comparePrefix string searchInputLen := len(searchInput) @@ -567,7 +572,10 @@ func runFuzzySearch(ctx context.Context, searchInput string, searchPrefix string } for searchInputLen != 0 { - fuzzyInput := strings.Join([]string{comparePrefix, searchInput}, ".") + fuzzyInput := searchInput + if comparePrefix != "" { + fuzzyInput = comparePrefix + "." + searchInput + } results, err := ac.SuggestOpts(fuzzyInput, redisearch.SuggestOptions{ Num: math.MaxInt16, Fuzzy: true, @@ -603,10 +611,26 @@ func runFuzzySearch(ctx context.Context, searchInput string, searchPrefix string termLastPart = term[lastDotIndex+1:] } - if result.Term == "" { - termHierarchyLen = 1 - } else { - termHierarchyLen = strings.Count(result.Term, ".") + 1 + if recommendLenType == constants.FullRecommendLength && hierarchy == constants.GridRecommendHierarchyType { + exists, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, termLastPart).Result() + if err != nil || !exists { + logger.Info(ctx, "query key by redis fuzzy search failed or term not in redis all grid set", "query_key", fuzzyInput, "exists", exists, "error", err) + continue + } + } + + if recommendLenType == constants.FullRecommendLength { + if result.Term == "" { + termHierarchyLen = 1 + } else { + termHierarchyLen = strings.Count(result.Term, ".") + 1 + } + } else if recommendLenType == constants.IsLocalRecommendLength { + if result.Term == "" { + termHierarchyLen = 4 + } else { + termHierarchyLen = strings.Count(result.Term, ".") + 4 + } } if termHierarchyLen == compareHierarchyLen && termPrefix == comparePrefix && strings.HasPrefix(termLastPart, searchInput) {