Browse Source

no message

xingzai 2 years ago
parent
commit
21ed97f41e
2 changed files with 311 additions and 6 deletions
  1. 21 5
      controllers/search.go
  2. 290 1
      services/elastic.go

+ 21 - 5
controllers/search.go

@@ -1,7 +1,6 @@
 package controllers
 
 import (
-	"fmt"
 	"github.com/rdlucklib/rdluck_tools/paging"
 	"hongze/hongze_clpt/models"
 	"hongze/hongze_clpt/services"
@@ -181,17 +180,34 @@ func (this *MobileSearchController) ListHomeArtAndChart() {
 			br.ErrMsg = "检索失败,Err:" + err.Error()
 			return
 		}
-		tmpResult, tmpTotalResult, err := services.EsArticleSearch(keyWord, startSize, pageSize, orderColumn, 1)
+		tmpResult, tmpTotalResult, err := services.EsArticleSearchBody(keyWord, startSize, pageSize, orderColumn, 1)
 		if err != nil {
 			br.Msg = "检索失败"
 			br.ErrMsg = "检索失败,Err:" + err.Error()
 			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 {
-			fmt.Println(currentIndex * pageSize)
-			fmt.Println(startSize)
-			fmt.Println(tmpTotalResult)
 			startSizeIk := startSize - int(tmpTotalResult)
 			if startSizeIk < 0 {
 				startSizeIk = 0

+ 290 - 1
services/elastic.go

@@ -464,9 +464,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 ikType == 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 {
@@ -475,6 +752,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]