xingzai 2 éve
szülő
commit
3a0d064c16
2 módosított fájl, 120 hozzáadás és 10 törlés
  1. 30 4
      controllers/search.go
  2. 90 6
      services/elastic.go

+ 30 - 4
controllers/search.go

@@ -1,6 +1,7 @@
 package controllers
 package controllers
 
 
 import (
 import (
+	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"hongze/hongze_clpt/models"
 	"hongze/hongze_clpt/models"
 	"hongze/hongze_clpt/services"
 	"hongze/hongze_clpt/services"
@@ -174,15 +175,40 @@ func (this *MobileSearchController) ListHomeArtAndChart() {
 		//	total = tmpTotal
 		//	total = tmpTotal
 		//	err = tmpErr
 		//	err = tmpErr
 		//}
 		//}
-		tmpResult, tmpTotal, tmpErr := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn)
-		result = tmpResult
-		total = tmpTotal
-		err = tmpErr
+		_, tmpTotal, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 0)
+		if err != nil {
+			br.Msg = "检索失败"
+			br.ErrMsg = "检索失败,Err:" + err.Error()
+			return
+		}
+		tmpResult, tmpTotalResult, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 1)
 		if err != nil {
 		if err != nil {
 			br.Msg = "检索失败"
 			br.Msg = "检索失败"
 			br.ErrMsg = "检索失败,Err:" + err.Error()
 			br.ErrMsg = "检索失败,Err:" + err.Error()
 			return
 			return
 		}
 		}
+		result = tmpResult
+		if int(tmpTotalResult) < currentIndex*pageSize {
+			fmt.Println(currentIndex * pageSize)
+			fmt.Println(startSize)
+			fmt.Println(tmpTotalResult)
+			startSizeIk := startSize - int(tmpTotalResult)
+			if startSizeIk < 0 {
+				startSizeIk = 0
+			}
+			var pageSizeIk int
+			pageSizeIk = pageSize - len(tmpResult)
+			tmpResultIk, _, err := services.EsArticleSearch(keyWord, startSizeIk, pageSizeIk, orderColumn, 2)
+			if err != nil {
+				br.Msg = "检索失败"
+				br.ErrMsg = "检索失败,Err:" + err.Error()
+				return
+			}
+			for _, v := range tmpResultIk {
+				result = append(result, v)
+			}
+		}
+		total = tmpTotal
 		if len(result) == 0 {
 		if len(result) == 0 {
 			result = make([]*models.SearchItem, 0)
 			result = make([]*models.SearchItem, 0)
 		}
 		}

+ 90 - 6
services/elastic.go

@@ -240,7 +240,7 @@ func EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord string, startSize
 	return
 	return
 }
 }
 
 
-func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
+func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string, ikType int) (result []*models.SearchItem, total int64, err error) {
 	indexName := utils.IndexName
 	indexName := utils.IndexName
 	client := utils.Client
 	client := utils.Client
 	keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
 	keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
@@ -250,9 +250,15 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 		keyWordArr = append(keyWordArr, keyWord)
 		keyWordArr = append(keyWordArr, keyWord)
 		keyWordLen = len(keyWordArr)
 		keyWordLen = len(keyWordArr)
 	}
 	}
+	//Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
+	//ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
 	mustMap := make([]interface{}, 0)
 	mustMap := make([]interface{}, 0)
 	shouldMap := make(map[string]interface{}, 0)
 	shouldMap := make(map[string]interface{}, 0)
 	shouldMapquery := make([]interface{}, 0)
 	shouldMapquery := make([]interface{}, 0)
+
+	mustNotMap := make([]interface{}, 0)
+	shouldNotMap := make(map[string]interface{}, 0)
+	shouldNotMapquery := make([]interface{}, 0)
 	// @Param   OrderColumn   query   int  true       "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
 	// @Param   OrderColumn   query   int  true       "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
 	//keyWordWeight := GetWeight(keyWordLen)
 	//keyWordWeight := GetWeight(keyWordLen)
 	var boost int
 	var boost int
@@ -263,6 +269,65 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 		} else {
 		} else {
 			boost = 1
 			boost = 1
 		}
 		}
+
+		//如果是 2:查询除了查询键入词之外的联想词
+		if k == 0 && ikType == 2 {
+			if v != "" {
+				shouldNotMapquery = append(shouldNotMapquery, 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,
+							},
+						},
+					},
+				})
+				shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
+					"function_score": map[string]interface{}{
+						"query": map[string]interface{}{
+							"multi_match": map[string]interface{}{
+								"boost":  boost, //给查询的值赋予权重
+								"fields": []interface{}{"Abstract"},
+								"query":  v,
+							},
+						},
+					},
+				})
+				shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
+					"function_score": map[string]interface{}{
+						"query": map[string]interface{}{
+							"multi_match": map[string]interface{}{
+								"boost":  boost, //给查询的值赋予权重
+								"fields": []interface{}{"Annotation"},
+								"query":  v,
+							},
+						},
+					},
+				})
+
+				shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
+					"function_score": map[string]interface{}{
+						"query": map[string]interface{}{
+							"multi_match": map[string]interface{}{
+								//"boost":  (lenkeyWordArr-k)*boost - 1, //给查询的值赋予权重
+								"boost":  boost, //给查询的值赋予权重
+								"fields": []interface{}{"BodyText"},
+								"query":  v,
+							},
+						},
+					},
+				})
+			}
+			continue
+		}
+		//如果是 1:查询键入词
+		if k > 0 && ikType == 1 {
+			continue
+		}
+
 		if v != "" {
 		if v != "" {
 			shouldMapquery = append(shouldMapquery, map[string]interface{}{
 			shouldMapquery = append(shouldMapquery, map[string]interface{}{
 				"function_score": map[string]interface{}{
 				"function_score": map[string]interface{}{
@@ -316,20 +381,24 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	shouldMap = map[string]interface{}{
 	shouldMap = map[string]interface{}{
 		"should": shouldMapquery,
 		"should": shouldMapquery,
 	}
 	}
+
+	shouldNotMap = map[string]interface{}{
+		"should": shouldNotMapquery,
+	}
 	//排序
 	//排序
 	sortMap := make([]interface{}, 0)
 	sortMap := make([]interface{}, 0)
 
 
-	sortMap = append(sortMap, map[string]interface{}{
-		"_score": map[string]interface{}{
-			"order": "desc",
-		},
-	})
 	//时间
 	//时间
 	sortMap = append(sortMap, map[string]interface{}{
 	sortMap = append(sortMap, map[string]interface{}{
 		"PublishDate": map[string]interface{}{
 		"PublishDate": map[string]interface{}{
 			"order": "desc",
 			"order": "desc",
 		},
 		},
 	})
 	})
+	//sortMap = append(sortMap, map[string]interface{}{
+	//	"_score": map[string]interface{}{
+	//		"order": "desc",
+	//	},
+	//})
 	//高亮
 	//高亮
 	highlightMap := make(map[string]interface{}, 0)
 	highlightMap := make(map[string]interface{}, 0)
 	highlightMap = map[string]interface{}{
 	highlightMap = map[string]interface{}{
@@ -347,6 +416,10 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	mustMap = append(mustMap, map[string]interface{}{
 	mustMap = append(mustMap, map[string]interface{}{
 		"bool": shouldMap,
 		"bool": shouldMap,
 	})
 	})
+	mustNotMap = append(mustNotMap, map[string]interface{}{
+		"bool": shouldNotMap,
+	})
+
 	queryMap := map[string]interface{}{
 	queryMap := map[string]interface{}{
 		"query": map[string]interface{}{
 		"query": map[string]interface{}{
 			"bool": map[string]interface{}{
 			"bool": map[string]interface{}{
@@ -354,6 +427,17 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 			},
 			},
 		},
 		},
 	}
 	}
+	//把第一次键入词的筛选条件过滤掉
+	if ikType == 2 {
+		queryMap = map[string]interface{}{
+			"query": map[string]interface{}{
+				"bool": map[string]interface{}{
+					"must":     mustMap,
+					"must_not": mustNotMap,
+				},
+			},
+		}
+	}
 	if orderColumn == "Matching" {
 	if orderColumn == "Matching" {
 		queryMap["sort"] = sortMap
 		queryMap["sort"] = sortMap
 	}
 	}