Browse Source

搜索优化分页

xingzai 2 years ago
parent
commit
1caa578e07
2 changed files with 166 additions and 2 deletions
  1. 2 2
      controllers/search.go
  2. 164 0
      services/elastic.go

+ 2 - 2
controllers/search.go

@@ -436,12 +436,12 @@ func (this *SearchController) ListHomeArtAndChart() {
 	var total int64
 	if listType == 1 || listType == 2 {
 		if orderColumn == "PublishDate" {
-			tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSort(indexName, keyWord, startSize, 100, user.UserId)
+			tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQueryTimeSortPage(indexName, keyWord, startSize, pageSize, user.UserId)
 			result = tmpResult
 			total = tmpTotal
 			err = tmpErr
 		} else {
-			tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySort(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
+			tmpResult, tmpTotal, tmpErr := services.EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord, startSize, pageSize, user.UserId, orderColumn)
 			result = tmpResult
 			total = tmpTotal
 			err = tmpErr

+ 164 - 0
services/elastic.go

@@ -963,3 +963,167 @@ func EsSearchReport(indexName, keyWord string, startSize, pageSize, userId int)
 	total = int64(len(result))
 	return
 }
+
+//分页
+func EsMultiMatchFunctionScoreQueryTimeSortPage(indexName, keyWord string, startSize, pageSize, userId int) (result []*models.SearchItem, total int64, err error) {
+	client := utils.Client
+	keyWordArr, err := GetIndustryMapNameSliceV2(keyWord)
+	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
+	boolquery := elastic.NewBoolQuery()
+	matchArr := make([]elastic.Query, 0)
+	n := 0
+	keyWordLen := len(keyWordArr)
+	if keyWordLen <= 0 {
+		keyWordArr = append(keyWordArr, keyWord)
+		keyWordLen = len(keyWordArr)
+	}
+	for _, v := range keyWordArr {
+		if v != "" {
+			multiMatch := elastic.NewMultiMatchQuery(v, "Title", "BodyText")
+			bodyFunctionQuery := elastic.NewFunctionScoreQuery()
+			bodyFunctionQuery.Query(multiMatch)
+			matchArr = append(matchArr, bodyFunctionQuery)
+		}
+		n++
+	}
+	boolquery.Should(matchArr...)
+	highlight := elastic.NewHighlight()
+	highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
+	highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+	request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
+	searchByMatch, err := request.Do(context.Background())
+	if searchByMatch != nil {
+		if searchByMatch.Hits != nil {
+			for _, v := range searchByMatch.Hits.Hits {
+				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["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
+				result = append(result, searchItem)
+			}
+		}
+		total = searchByMatch.Hits.TotalHits.Value
+	}
+	return
+}
+
+func EsMultiMatchFunctionScoreQuerySortPage(indexName, keyWord string, startSize, pageSize, userId int, orderColumn string) (result []*models.SearchItem, total int64, err error) {
+	client := utils.Client
+	keyWordArr, err := GetIndustryMapNameSliceV3(keyWord)
+	keyWordArr = RemoveDuplicatesAndEmpty(keyWordArr)
+
+	keyWordLen := len(keyWordArr)
+	if keyWordLen <= 0 {
+		keyWordArr = append(keyWordArr, keyWord)
+		keyWordLen = len(keyWordArr)
+	}
+	var keyWords string
+	for _, v := range keyWordArr {
+		keyWords += v + " "
+	}
+	// @Param   OrderColumn   query   int  true       "排序字段 ,Comprehensive综合 ,Matching匹配度 ,PublishDate 发布时间 "
+	//keyWordWeight := GetWeight(keyWordLen)
+
+	matchArr := make([]elastic.Query, 0)
+	boolquery := elastic.NewBoolQuery()
+	bodyFunctionQuery := elastic.NewFunctionScoreQuery()
+	bodyFunctionQuery2 := elastic.NewFunctionScoreQuery()
+	bodyFunctionQuery3 := elastic.NewFunctionScoreQuery()
+	multiMatch := elastic.NewMultiMatchQuery(keyWords, "Title").Analyzer("ik_smart").Boost(100)
+	bodyFunctionQuery.Query(multiMatch)
+	matchArr = append(matchArr, bodyFunctionQuery)
+	multiMatch = elastic.NewMultiMatchQuery(keyWords, "BodyText").Analyzer("ik_smart").Boost(1)
+	bodyFunctionQuery2.Query(multiMatch)
+	matchArr = append(matchArr, bodyFunctionQuery2)
+	bodyFunctionQuery3.Query(multiMatch)
+	matchArr = append(matchArr, bodyFunctionQuery3)
+	boolquery.Should(matchArr...)
+	highlight := elastic.NewHighlight()
+	highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
+	highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
+	request := client.Search(indexName).Highlight(highlight).Sort("PublishDate", false).From(startSize).Size(pageSize).Query(boolquery)
+	if orderColumn == "Matching" {
+		request = client.Search(indexName).Highlight(highlight).From(startSize).Size(pageSize).Query(boolquery)
+	}
+	searchByMatch, err := request.Do(context.Background())
+	if err != nil {
+		return nil, 0, err
+	}
+	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["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
+}