Bläddra i källkod

Es搜索引擎优化

xingzai 2 år sedan
förälder
incheckning
d7c93bec87
2 ändrade filer med 320 tillägg och 1 borttagningar
  1. 20 0
      controllers/search.go
  2. 300 1
      services/elastic.go

+ 20 - 0
controllers/search.go

@@ -611,6 +611,26 @@ func (this *SearchController) ListHomeArtAndChartPage() {
 			return
 		}
 		result = tmpResult
+
+		if int(tmpTotalResult) < currentIndex*pageSize {
+			startSizeBody := startSize - int(tmpTotalResult)
+			if startSizeBody < 0 {
+				startSizeBody = 0
+			}
+			var pageSizeBody int
+			pageSizeBody = pageSize - len(tmpResult)
+			tmpResultBody, tmpTotalBody, err := services.EsArticleSearchBody(keyWord, startSizeBody, pageSizeBody, orderColumn, 2)
+			if err != nil {
+				br.Msg = "检索失败"
+				br.ErrMsg = "检索失败,Err:" + err.Error()
+				return
+			}
+			for _, v := range tmpResultBody {
+				result = append(result, v)
+			}
+			tmpTotalResult += tmpTotalBody
+		}
+
 		if int(tmpTotalResult) < currentIndex*pageSize {
 			startSizeIk := startSize - int(tmpTotalResult)
 			if startSizeIk < 0 {

+ 300 - 1
services/elastic.go

@@ -901,6 +901,11 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 		keyWordArr = append(keyWordArr, keyWord)
 		keyWordLen = len(keyWordArr)
 	}
+	fmt.Println(keyWordArr)
+	//如果没有联想词,而且查询的还是联想词就返回
+	if ikType == 2 && keyWordLen == 1 {
+		return
+	}
 	//Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
 	//ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
 	mustMap := make([]interface{}, 0)
@@ -1045,6 +1050,11 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 			"order": "desc",
 		},
 	})
+	//sortMap = append(sortMap, map[string]interface{}{
+	//	"_score": map[string]interface{}{
+	//		"order": "desc",
+	//	},
+	//})
 	//高亮
 	highlightMap := make(map[string]interface{}, 0)
 	highlightMap = map[string]interface{}{
@@ -1110,9 +1120,286 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 				}
 				searchItem := new(models.SearchItem)
 				searchItem.ArticleId, _ = strconv.Atoi(v.Id)
+				if len(v.Highlight["Annotation"]) > 0 {
+					for _, vText := range v.Highlight["Annotation"] {
+						searchItem.Body = append(searchItem.Body, vText)
+					}
+				}
+				if len(v.Highlight["Abstract"]) > 0 {
+					for _, vText := range v.Highlight["Abstract"] {
+						searchItem.Body = append(searchItem.Body, vText)
+					}
+				}
 				if len(v.Highlight["BodyText"]) > 0 {
-					searchItem.Body = v.Highlight["BodyText"]
+					for _, vText := range v.Highlight["BodyText"] {
+						searchItem.Body = append(searchItem.Body, vText)
+					}
+				}
+
+				if len(searchItem.Body) == 0 {
+					bodyRune := []rune(article.BodyText)
+					bodyRuneLen := len(bodyRune)
+					if bodyRuneLen > 100 {
+						bodyRuneLen = 100
+					}
+					body := string(bodyRune[:bodyRuneLen])
+					searchItem.Body = []string{body}
+				}
+
+				//if len(v.Highlight["BodyText"]) > 0 {
+				//	searchItem.Body = v.Highlight["BodyText"]
+				//} else {
+				//	bodyRune := []rune(article.BodyText)
+				//	bodyRuneLen := len(bodyRune)
+				//	if bodyRuneLen > 100 {
+				//		bodyRuneLen = 100
+				//	}
+				//	body := string(bodyRune[:bodyRuneLen])
+				//	searchItem.Body = []string{body}
+				//}
+				var title string
+				if len(v.Highlight["Title"]) > 0 {
+					title = v.Highlight["Title"][0]
 				} else {
+					title = article.Title
+				}
+				searchItem.Title = title
+				searchItem.PublishDate = article.PublishDate
+				searchItem.ExpertBackground = article.ExpertBackground
+				searchItem.CategoryId = article.CategoryId
+				for _, v_result := range result {
+					if v_result.ArticleId == searchItem.ArticleId {
+						isAppend = true
+					}
+				}
+				if !isAppend {
+					result = append(result, searchItem)
+				}
+			}
+		}
+		total = searchByMatch.Hits.TotalHits.Value
+	}
+	return
+}
+
+func EsArticleSearchBody(keyWord string, startSize, pageSize int, orderColumn string, searchType int) (result []*models.SearchItem, total int64, err error) {
+	if keyWord == "" {
+		return
+	}
+	indexName := utils.IndexName
+	client := utils.Client
+
+	//Es 的高级查询有 自定义排序 文档一时半会儿撸不懂,先做多次查询手动过滤 2023.2.2
+	//ikType 查询方式 ,0:查所有 、 1:查询键入词 、 2:查询除了查询键入词之外的联想词
+	mustMap := make([]interface{}, 0)
+	shouldMap := make(map[string]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 发布时间 "
+	//keyWordWeight := GetWeight(keyWordLen)
+	var boost int
+
+	//如果是 2:查询标题,摘要,核心观点的词
+	if searchType == 1 {
+		shouldMapquery = append(shouldMapquery, map[string]interface{}{
+			"function_score": map[string]interface{}{
+				"query": map[string]interface{}{
+					"multi_match": map[string]interface{}{
+						"boost":  boost, //给查询的值赋予权重
+						"fields": []interface{}{"Title"},
+						"query":  keyWord,
+					},
+				},
+			},
+		})
+		shouldMapquery = append(shouldMapquery, map[string]interface{}{
+			"function_score": map[string]interface{}{
+				"query": map[string]interface{}{
+					"multi_match": map[string]interface{}{
+						"boost":  boost, //给查询的值赋予权重
+						"fields": []interface{}{"Abstract"},
+						"query":  keyWord,
+					},
+				},
+			},
+		})
+		shouldMapquery = append(shouldMapquery, map[string]interface{}{
+			"function_score": map[string]interface{}{
+				"query": map[string]interface{}{
+					"multi_match": map[string]interface{}{
+						"boost":  boost, //给查询的值赋予权重
+						"fields": []interface{}{"Annotation"},
+						"query":  keyWord,
+					},
+				},
+			},
+		})
+	}
+
+	//如果是 2:查询body的相关词
+	if searchType == 2 {
+
+		shouldNotMapquery = append(shouldNotMapquery, map[string]interface{}{
+			"function_score": map[string]interface{}{
+				"query": map[string]interface{}{
+					"multi_match": map[string]interface{}{
+						"boost":  boost, //给查询的值赋予权重
+						"fields": []interface{}{"Title"},
+						"query":  keyWord,
+					},
+				},
+			},
+		})
+		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":  keyWord,
+					},
+				},
+			},
+		})
+		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":  keyWord,
+					},
+				},
+			},
+		})
+
+		//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":  keyWord,
+		//			},
+		//		},
+		//	},
+		//})
+		shouldMapquery = append(shouldMapquery, 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":  keyWord,
+					},
+				},
+			},
+		})
+	}
+
+	shouldMap = map[string]interface{}{
+		"should": shouldMapquery,
+	}
+
+	shouldNotMap = map[string]interface{}{
+		"should": shouldNotMapquery,
+	}
+	//排序
+	sortMap := make([]interface{}, 0)
+
+	//时间
+	sortMap = append(sortMap, map[string]interface{}{
+		"PublishDate": map[string]interface{}{
+			"order": "desc",
+		},
+	})
+	//高亮
+	highlightMap := make(map[string]interface{}, 0)
+	highlightMap = map[string]interface{}{
+		"fields": map[string]interface{}{
+			"BodyText":   map[string]interface{}{},
+			"Title":      map[string]interface{}{},
+			"Abstract":   map[string]interface{}{},
+			"Annotation": map[string]interface{}{},
+		},
+		//样式 红色
+		"post_tags": []interface{}{"</font>"},
+		"pre_tags":  []interface{}{"<font color='red'>"},
+	}
+
+	mustMap = append(mustMap, map[string]interface{}{
+		"bool": shouldMap,
+	})
+	mustNotMap = append(mustNotMap, map[string]interface{}{
+		"bool": shouldNotMap,
+	})
+
+	queryMap := map[string]interface{}{
+		"query": map[string]interface{}{
+			"bool": map[string]interface{}{
+				"must": mustMap,
+			},
+		},
+	}
+	//把第一次的筛选条件过滤掉
+	if searchType == 2 {
+		queryMap = map[string]interface{}{
+			"query": map[string]interface{}{
+				"bool": map[string]interface{}{
+					"must":     mustMap,
+					"must_not": mustNotMap,
+				},
+			},
+		}
+	}
+	if orderColumn == "Matching" {
+		queryMap["sort"] = sortMap
+	}
+	queryMap["from"] = startSize
+	queryMap["size"] = pageSize
+	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 {
+		if searchByMatch.Hits != nil {
+			for _, v := range searchByMatch.Hits.Hits {
+				var isAppend bool
+				articleJson, err := v.Source.MarshalJSON()
+				if err != nil {
+					return nil, 0, err
+				}
+				article := new(models.CygxArticleEs)
+				err = json.Unmarshal(articleJson, &article)
+				if err != nil {
+					return nil, 0, err
+				}
+				searchItem := new(models.SearchItem)
+				searchItem.ArticleId, _ = strconv.Atoi(v.Id)
+				if len(v.Highlight["Annotation"]) > 0 {
+					for _, vText := range v.Highlight["Annotation"] {
+						searchItem.Body = append(searchItem.Body, vText)
+					}
+				}
+				if len(v.Highlight["Abstract"]) > 0 {
+					for _, vText := range v.Highlight["Abstract"] {
+						searchItem.Body = append(searchItem.Body, vText)
+					}
+				}
+				if len(v.Highlight["BodyText"]) > 0 {
+					for _, vText := range v.Highlight["BodyText"] {
+						searchItem.Body = append(searchItem.Body, vText)
+					}
+				}
+
+				if len(searchItem.Body) == 0 {
 					bodyRune := []rune(article.BodyText)
 					bodyRuneLen := len(bodyRune)
 					if bodyRuneLen > 100 {
@@ -1121,6 +1408,18 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 					body := string(bodyRune[:bodyRuneLen])
 					searchItem.Body = []string{body}
 				}
+
+				//if len(v.Highlight["BodyText"]) > 0 {
+				//	searchItem.Body = v.Highlight["BodyText"]
+				//} else {
+				//	bodyRune := []rune(article.BodyText)
+				//	bodyRuneLen := len(bodyRune)
+				//	if bodyRuneLen > 100 {
+				//		bodyRuneLen = 100
+				//	}
+				//	body := string(bodyRune[:bodyRuneLen])
+				//	searchItem.Body = []string{body}
+				//}
 				var title string
 				if len(v.Highlight["Title"]) > 0 {
 					title = v.Highlight["Title"][0]