Bladeren bron

feat:搜索逻辑调整

wangshan 2 jaren geleden
bovenliggende
commit
f942f008dc

+ 69 - 20
jyBXCore/rpc/entity/search.go

@@ -109,7 +109,7 @@ func (kws *KeyWordsSearch) SaveKeyWordsToHistory(in *bxcore.SearchReq) {
 	if in.KeyWords != "" {
 		//历史记录
 		history := redis.GetStr("other", "s_"+in.UserId)
-		keys := util.SearchHistory(history, in.KeyWords)
+		keys := util.SearchHistory(history, in.KeyWords, in.AdditionalWords)
 		if len(keys) > 0 {
 			if b := redis.Put("other", "s_"+in.UserId, strings.Join(keys, ","), -1); !b {
 				logx.Info("保存搜索记录异常,用户id:", in.UserId)
@@ -118,8 +118,62 @@ func (kws *KeyWordsSearch) SaveKeyWordsToHistory(in *bxcore.SearchReq) {
 	}
 }
 
+// GetSearchKeyWordsQueryStr 关键词和附加词处理 获取关键词查询条件;是否进行分词查询
+func (kws *KeyWordsSearch) GetSearchKeyWordsQueryStr(in *bxcore.SearchReq) (words []string) {
+	// in.SearchMode 搜索模式:0:精准搜索;1:模糊搜索
+	// 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
+	// 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
+	if in.SearchMode == 1 {
+		var (
+			keyWords, addWords []string
+		)
+		//主关键词词组
+		if in.KeyWords != "" {
+			for _, mv := range strings.Split(in.KeyWords, IC.C.JYKeyMark) {
+				if strings.TrimSpace(mv) != "" {
+					if ikWords := util.HttpEs(mv, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
+						keyWords = append(keyWords, strings.Split(ikWords, IC.C.JYKeyMark)...)
+					}
+				}
+			}
+			in.KeyWords = strings.Join(keyWords, IC.C.JYKeyMark)
+			words = append(words, in.KeyWords)
+		}
+		//附加词组
+		//多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
+		if in.AdditionalWords != "" {
+			for _, awv := range strings.Split(in.AdditionalWords, ",") {
+				var (
+					addWord []string
+				)
+				for _, av := range strings.Split(awv, IC.C.JYKeyMark) {
+					if strings.TrimSpace(av) != "" {
+						if ikWords := util.HttpEs(av, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
+							addWord = append(addWord, strings.Split(ikWords, IC.C.JYKeyMark)...)
+						}
+					}
+				}
+				addWords = append(addWords, strings.Join(addWord, IC.C.JYKeyMark))
+				addWord = []string{}
+			}
+			words = append(words, addWords...)
+			in.AdditionalWords = strings.Join(addWords, ",")
+		}
+	} else {
+		words = append(words, strings.Split(in.KeyWords, IC.C.JYKeyMark)...)
+		for _, awv := range strings.Split(in.AdditionalWords, ",") {
+			for _, av := range strings.Split(awv, IC.C.JYKeyMark) {
+				if strings.TrimSpace(av) != "" {
+					words = append(words, strings.Split(in.KeyWords, IC.C.JYKeyMark)...)
+				}
+			}
+		}
+	}
+	return
+}
+
 // SearchParamsHandle 搜索条件 处理
-func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) {
+func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) []string {
 	//判断用户身份
 	userInfo := util.GetVipState(IC.MainMysql, IC.Mgo, in.UserId, in.EntId)
 	//是否是付费用户
@@ -200,28 +254,22 @@ func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) {
 	if in.KeyWords != "" {
 		//关键词处理
 		in.KeyWords = strings.TrimSpace(in.KeyWords)
-		//以后可能会出现 关键词 C++ 等带+的关键词
 		in.InterceptKeyWords, in.InterceptOtherWords, in.KeyWords = util.InterceptSearchKW(in.KeyWords, MC.IntAllDef(IC.C.KeywordsLimit, 35), len(in.Industry) == 0)
-		//附加词在关键词的基础上 有效
-		//附加词 每组附加词不能超过15个字符
-		if in.AdditionalWords != "" {
-			var additionalWords []string
-			for _, ak := range strings.Split(in.AdditionalWords, ",") {
-				if len([]rune(ak)) > 15 {
-					additionalWords = append(additionalWords, string([]rune(ak)[:15]))
-				} else {
-					additionalWords = append(additionalWords, ak)
-				}
+	}
+	//附加词 每组附加词不能超过15个字符
+	if in.AdditionalWords != "" {
+		var additionalWords []string
+		for _, ak := range strings.Split(in.AdditionalWords, ",") {
+			if len([]rune(ak)) > 15 {
+				additionalWords = append(additionalWords, string([]rune(ak)[:15]))
+			} else {
+				additionalWords = append(additionalWords, ak)
 			}
-			in.AdditionalWords = strings.ReplaceAll(strings.Join(additionalWords, IC.C.JYKeyMark), " ", IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.AdditionalWords, IC.C.JYKeyMark)
 		}
-		//格式化关键词
-		in.KeyWords = util.GetSearchKeyWordsQueryStr(in)
-		//更新关键词搜索历史记录
-		go kws.SaveKeyWordsToHistory(in)
-	} else {
-		in.AdditionalWords = ""
+		in.AdditionalWords = strings.Join(additionalWords, ",") //分组不变
 	}
+	//更新关键词搜索历史记录
+	go kws.SaveKeyWordsToHistory(in)
 	//排除词  每组排除词不能超过15个字符
 	if in.ExclusionWords != "" {
 		var exclusionWords []string
@@ -234,6 +282,7 @@ func (kws *KeyWordsSearch) SearchParamsHandle(in *bxcore.SearchReq) {
 		}
 		in.ExclusionWords = strings.Join(exclusionWords, IC.C.JYKeyMark) //util.MatchSpace.ReplaceAllString(in.ExclusionWords, IC.C.JYKeyMark)
 	}
+	return kws.GetSearchKeyWordsQueryStr(in) //格式化关键词
 }
 
 // GetBidSearchList 非空搜索 查询

+ 1 - 1
jyBXCore/rpc/etc/bxcore.yaml

@@ -33,7 +33,7 @@ PaySearchLimit:
   PageSize: 20
 KeywordsLimit: 35 #主搜索框 关键词长度限制
 DefaultBidInfo: #默认搜索
-  Total: 1800000000 #数据量提示
+  Total: 180000000 #数据量提示
   PageNum: 10 #总页数
   Count: 500 #查询数据量
   PayCount: 5000 #付费用户查询数据量

+ 2 - 2
jyBXCore/rpc/internal/logic/getsearchlistlogic.go

@@ -48,7 +48,7 @@ func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.Search
 	//初始化搜索对象
 	ks := entity.NewKeyWordsSearch()
 	//处理搜索条件
-	ks.SearchParamsHandle(in)
+	heightWods := ks.SearchParamsHandle(in)
 	//判断是否是空搜索,如果是空搜索,查缓存数据
 	if ks.IsEmptySearch(in) {
 		res.List, res.Count = ks.GetBidSearchListByCache(in)
@@ -93,7 +93,7 @@ func (l *GetSearchListLogic) GetSearchList(in *bxcore.SearchReq) (*bxcore.Search
 			res.Count, res.Total, res.List = ks.GetBidSearchList(in) //util.GetBidSearchData(in)
 			logx.Info("2查询耗时:", time.Since(t2))
 		}
-		res.KeyWords = in.KeyWords
+		res.KeyWords = strings.Join(heightWods, " ")
 		res.InterceptOtherWords = in.InterceptOtherWords
 		res.InterceptKeyWords = in.InterceptKeyWords
 	}

+ 56 - 6
jyBXCore/rpc/model/es/search.go

@@ -29,17 +29,21 @@ func GetSearchQuery(in *bxcore.SearchReq, mustQuery string) (qstr string) {
 	} else {
 		findFields = fmt.Sprintf(`"%s"`, strings.Join(selectTypeArr, "\",\""))
 	}
-	//此时关键词中间有+进行隔离
+	var (
+		wordsMusts, wordsShoulds []string
+		switchBool               = strings.Contains(findFields, "detail") && strings.Contains(findFields, "title") && IC.C.SearchTypeSwitch
+	)
+	//此时关键词中间有IC.C.JYKeyMark进行隔离
 	if in.KeyWords != "" {
 		var (
-			keyMusts   []string
-			switchBool = strings.Contains(findFields, "detail") && strings.Contains(findFields, "title") && IC.C.SearchTypeSwitch
+			keyWordsMusts []string
 		)
 		for _, v := range strings.Split(in.KeyWords, IC.C.JYKeyMark) {
 			if elastic.ReplaceYH(v) == "" {
 				continue
 			}
 			//单个字  搜索范围 有全文或者附件 无标题 例如:学 虚拟机 detail  搜索的时候加上标题
+			//detail 正文不支持单字查询
 			if len([]rune(elastic.ReplaceYH(v))) == 1 && DetailFileORTitle(findFields) {
 				findFields += `,"title"`
 			} else if switchBool && len([]rune(elastic.ReplaceYH(v))) > 1 {
@@ -50,15 +54,61 @@ func GetSearchQuery(in *bxcore.SearchReq, mustQuery string) (qstr string) {
 					findFields = strings.Replace(findFields, `,"title"`, ``, -1)
 				}
 			}
-			keyMusts = append(keyMusts, fmt.Sprintf(fmt.Sprintf(multiMatch, "%s", findFields), elastic.ReplaceYH(v)))
+			keyWordsMusts = append(keyWordsMusts, fmt.Sprintf(fmt.Sprintf(multiMatch, "%s", findFields), elastic.ReplaceYH(v)))
 		}
 		//搜索关键词模式;默认0:包含所有,1:包含任意
 		if in.WordsMode == 1 {
-			musts = append(musts, fmt.Sprintf(queryBoolShould, strings.Join(keyMusts, ",")))
+			wordsShoulds = append(wordsShoulds, fmt.Sprintf(elastic.NgramMust, strings.Join(keyWordsMusts, ",")))
 		} else {
-			musts = append(musts, fmt.Sprintf(elastic.NgramMust, strings.Join(keyMusts, ",")))
+			wordsMusts = append(wordsMusts, keyWordsMusts...)
+		}
+	}
+	if in.AdditionalWords != "" {
+		//多组附加词,每组间,号隔开。每组内如果关键词中间有空格,自动分词
+		var (
+			addWordsMusts []string
+		)
+		for _, aws := range strings.Split(in.AdditionalWords, ",") {
+			var (
+				addWordsMust []string
+			)
+			for _, v := range strings.Split(aws, IC.C.JYKeyMark) {
+				if elastic.ReplaceYH(v) == "" {
+					continue
+				}
+				//单个字  搜索范围 有全文或者附件 无标题 例如:学 虚拟机 detail  搜索的时候加上标题
+				//detail 正文不支持单字查询
+				if len([]rune(elastic.ReplaceYH(v))) == 1 && DetailFileORTitle(findFields) {
+					findFields += `,"title"`
+				} else if switchBool && len([]rune(elastic.ReplaceYH(v))) > 1 {
+					//标题 全文搜索 搜索类型开关打开 默认搜索全文;(全文包含标题)(单字排除)
+					if strings.Contains(findFields, `"title",`) {
+						findFields = strings.Replace(findFields, `"title",`, ``, -1)
+					} else if strings.Contains(findFields, `,"title"`) {
+						findFields = strings.Replace(findFields, `,"title"`, ``, -1)
+					}
+				}
+				addWordsMust = append(addWordsMust, fmt.Sprintf(fmt.Sprintf(multiMatch, "%s", findFields), elastic.ReplaceYH(v)))
+				addWordsMusts = append(addWordsMusts, addWordsMust...)
+				if in.WordsMode == 0 {
+					wordsMusts = append(wordsMusts, addWordsMust...)
+				}
+				addWordsMust = []string{}
+			}
+			//搜索关键词模式;默认0:包含所有,1:包含任意
+			if in.WordsMode == 1 {
+				wordsShoulds = append(wordsShoulds, fmt.Sprintf(elastic.NgramMust, strings.Join(addWordsMusts, ",")))
+				addWordsMusts = []string{}
+			}
 		}
 	}
+	//搜索关键词模式;默认0:包含所有,1:包含任意
+	//包含任意一组
+	if len(wordsShoulds) > 0 {
+		musts = append(musts, fmt.Sprintf(queryBoolShould, strings.Join(wordsShoulds, ",")))
+	} else if len(wordsMusts) > 0 {
+		musts = append(musts, fmt.Sprintf(elastic.NgramMust, strings.Join(wordsMusts, ",")))
+	}
 	if in.Industry != "" {
 		musts = append(musts, fmt.Sprintf(queryBoolMust, `"`+strings.ReplaceAll(in.Industry, ",", `","`)+`"`))
 	}

+ 16 - 45
jyBXCore/rpc/util/search.go

@@ -33,9 +33,19 @@ var (
 )
 
 // SearchHistory 格式化 关键词搜索历史记录
-func SearchHistory(history, searchValue string) (arrS []string) {
-	if searchValue != "" {
-		var searchKeys = strings.Split(searchValue, IC.C.JYKeyMark)
+func SearchHistory(history, searchValue, additionalWords string) (arrS []string) {
+	//主关键词
+	var searchKeys = strings.Split(searchValue, IC.C.JYKeyMark)
+	//附加词
+	if additionalWords != "" {
+		for _, aws := range strings.Split(additionalWords, ",") {
+			for _, as := range strings.Split(aws, IC.C.JYKeyMark) {
+				searchKeys = append(searchKeys, as)
+			}
+		}
+	}
+	//关键词 和 附加词 合并,作为新的关键词历史搜索记录
+	if len(searchKeys) > 0 {
 		arrS = strings.Split(history, ",")
 		//新增历史记录
 		if history == "" {
@@ -49,9 +59,9 @@ func SearchHistory(history, searchValue string) (arrS []string) {
 				}
 			}
 		}
-		arrS = append(arrS, searchValue)
+		arrS = append(arrS, searchKeys...)
 		if len(arrS) > 10 {
-			arrS = arrS[len(searchKeys) : len(searchKeys)+10]
+			arrS = arrS[len(arrS)-10:]
 		}
 	}
 	return arrS
@@ -121,7 +131,7 @@ func HttpEs(ques, analyzer, esAddress string) (res string) {
 			tokens := MC.ObjArrToMapArr(resMap["tokens"].([]interface{}))
 			for _, v := range tokens {
 				token := MC.ObjToString(v["token"])
-				if len([]rune(token)) == 1 && unicode.IsLetter([]rune(token)[0]) {
+				if len([]rune(token)) == 1 && !unicode.Is(unicode.Scripts["Han"], []rune(token)[0]) { //(P260保留单个汉字)
 					continue
 				}
 				if res != "" && token != "" {
@@ -345,42 +355,3 @@ func GetPublishTime(y, m int, publishTime string) string {
 	}
 	return ""
 }
-
-//20220114 P260搜索优化
-
-// GetSearchKeyWordsQueryStr 关键词处理 获取关键词查询条件
-func GetSearchKeyWordsQueryStr(in *bxcore.SearchReq) string {
-	var (
-		keyWords, searchWords []string
-	)
-	//主关键词词组
-	for _, mv := range strings.Split(in.KeyWords, IC.C.JYKeyMark) {
-		if strings.TrimSpace(mv) != "" {
-			keyWords = append(keyWords, mv)
-		}
-	}
-	//附加词组
-	//多组附加词,每组间,号隔开,上面已处理成空格分割。每组内如果关键词中间有空格,自动分词
-	if in.AdditionalWords != "" {
-		for _, av := range strings.Split(in.AdditionalWords, IC.C.JYKeyMark) {
-			if strings.TrimSpace(av) != "" {
-				keyWords = append(keyWords, av)
-			}
-		}
-	}
-	// in.SearchMode 搜索模式:0:精准搜索;1:模糊搜索
-	// 精准搜索:不分词,完全匹配;(中间带空格的关键词组自动分词)
-	// 模糊搜索:对用户输入的单个关键词进行分词处理,但必须都存在;
-	if in.SearchMode == 1 {
-		for _, kv := range keyWords {
-			if strings.TrimSpace(kv) != "" {
-				if ikWords := HttpEs(kv, "ik_smart", IC.DB.Es.Addr); ikWords != "" {
-					searchWords = append(searchWords, ikWords)
-				}
-			}
-		}
-	} else {
-		searchWords = append(searchWords, keyWords...)
-	}
-	return strings.Join(searchWords, IC.C.JYKeyMark)
-}