Browse Source

ES 权重排序处理

xingzai 2 years ago
parent
commit
b23d19bea8
2 changed files with 22 additions and 12 deletions
  1. 4 3
      controllers/search.go
  2. 18 9
      services/elastic.go

+ 4 - 3
controllers/search.go

@@ -89,9 +89,10 @@ func (this *MobileSearchController) ListHomeArtAndChart() {
 		imgUrlChart = vslice[len(vslice)-1]
 		mapCategoryUrl[categoryId] = imgUrlChart
 	}
-	//if orderColumn == "" {
-	//	orderColumn = "Matching"
-	//}
+	if orderColumn == "" {
+		orderColumn = "Matching"
+	}
+
 	orderColumn = "Matching"
 	//indexName := utils.IndexName
 	//pageSize = 20

+ 18 - 9
services/elastic.go

@@ -255,20 +255,21 @@ 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
+	var boost int
 	//lenkeyWordArr := len(keyWordArr)
-	for _, v := range keyWordArr {
-		//if k == 0 {
-		//	boost = 2 * 1000
-		//} else {
-		//	boost = 1000
-		//}
+	for k, v := range keyWordArr {
+		if k == 0 {
+			boost = 2 * 1000
+		} else {
+			boost = 1
+		}
 		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) * boost, //给查询的值赋予权重
+							"boost":  boost, //给查询的值赋予权重
 							"fields": []interface{}{"Title"},
 							"query":  v,
 						},
@@ -279,7 +280,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) * boost, //给查询的值赋予权重
+							"boost":  boost, //给查询的值赋予权重
 							"fields": []interface{}{"Abstract"},
 							"query":  v,
 						},
@@ -290,7 +291,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) * boost, //给查询的值赋予权重
+							"boost":  boost, //给查询的值赋予权重
 							"fields": []interface{}{"Annotation"},
 							"query":  v,
 						},
@@ -303,6 +304,7 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 					"query": map[string]interface{}{
 						"multi_match": map[string]interface{}{
 							//"boost":  (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
+							"boost":  boost, //给查询的值赋予权重
 							"fields": []interface{}{"BodyText"},
 							"query":  v,
 						},
@@ -316,6 +318,12 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	}
 	//排序
 	sortMap := make([]interface{}, 0)
+
+	sortMap = append(sortMap, map[string]interface{}{
+		"_score": map[string]interface{}{
+			"order": "desc",
+		},
+	})
 	//时间
 	sortMap = append(sortMap, map[string]interface{}{
 		"PublishDate": map[string]interface{}{
@@ -354,6 +362,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))
 	request := client.Search(indexName).Source(queryMap) // sets the JSON request
 	searchByMatch, err := request.Do(context.Background())
 	if searchByMatch != nil {