Przeglądaj źródła

Es搜索权重处理

xingzai 2 lat temu
rodzic
commit
1f584fd11a
1 zmienionych plików z 9 dodań i 4 usunięć
  1. 9 4
      services/elastic.go

+ 9 - 4
services/elastic.go

@@ -906,15 +906,20 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	shouldMapquery := make([]interface{}, 0)
 	// @Param   OrderColumn   query   int  true       "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
 	//keyWordWeight := GetWeight(keyWordLen)
-
+	var boost int
 	lenkeyWordArr := len(keyWordArr)
 	for k, v := range keyWordArr {
+		if k == 0 {
+			boost = 2 * 1000
+		} else {
+			boost = 1000
+		}
 		if v != "" {
 			shouldMapquery = append(shouldMapquery, map[string]interface{}{
 				"function_score": map[string]interface{}{
 					"query": map[string]interface{}{
 						"multi_match": map[string]interface{}{
-							"boost":  (lenkeyWordArr - k) * 100, //给查询的值赋予权重
+							"boost":  (lenkeyWordArr - k) * boost, //给查询的值赋予权重
 							"fields": []interface{}{"Title"},
 							"query":  v,
 						},
@@ -926,7 +931,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 				"function_score": map[string]interface{}{
 					"query": map[string]interface{}{
 						"multi_match": map[string]interface{}{
-							"boost":  (lenkeyWordArr-k)*100 - 1, //给查询的值赋予权重
+							"boost":  (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
 							"fields": []interface{}{"BodyText"},
 							"query":  v,
 						},
@@ -976,7 +981,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	queryMap["highlight"] = highlightMap
 	jsonBytes, _ := json.Marshal(queryMap)
 	fmt.Println(string(jsonBytes))
-	utils.FileLog.Info(string(jsonBytes))
+	//utils.FileLog.Info(string(jsonBytes))
 	request := client.Search(indexName).Source(queryMap) // sets the JSON request
 	searchByMatch, err := request.Do(context.Background())
 	if searchByMatch != nil {