Pārlūkot izejas kodu

搜索引擎添加摘要,核心观点的权重搜索

xingzai 2 gadi atpakaļ
vecāks
revīzija
b12e6bdc37
3 mainītis faili ar 60 papildinājumiem un 13 dzēšanām
  1. 1 0
      models/article.go
  2. 42 8
      services/elastic.go
  3. 17 5
      services/task.go

+ 1 - 0
models/article.go

@@ -160,6 +160,7 @@ type ArticleDetail struct {
 	IsShowLinkButton        int    `description:"这种报告类型是否展示查看报告链接"`
 	ArticleTypeId           int    `description:"文章类型ID"`
 	IsSpecialArticle        bool   `description:"是否属于专项调研报告"`
+	Annotation              string `description:"核心观点"`
 }
 
 type ArticleDetailFileLink struct {

+ 42 - 8
services/elastic.go

@@ -332,6 +332,8 @@ func EsAddOrEditData(indexName, docId string, item *ElasticTestArticleDetail) (e
 			"PublishDate":      item.PublishDate,
 			"CategoryId":       item.CategoryId,
 			"ExpertBackground": item.ExpertBackground,
+			"Abstract":         item.Abstract,
+			"Annotation":       item.Annotation,
 		}).Do(context.Background())
 		if err != nil {
 			return err
@@ -899,7 +901,6 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 		keyWordArr = append(keyWordArr, keyWord)
 		keyWordLen = len(keyWordArr)
 	}
-	//fmt.Println(keyWordArr)
 	mustMap := make([]interface{}, 0)
 	shouldMap := make(map[string]interface{}, 0)
 	//shouldMapquery := make(map[string]interface{}, 0)
@@ -926,7 +927,28 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 					},
 				},
 			})
-
+			shouldMapquery = append(shouldMapquery, map[string]interface{}{
+				"function_score": map[string]interface{}{
+					"query": map[string]interface{}{
+						"multi_match": map[string]interface{}{
+							"boost":  (lenkeyWordArr - k) * boost, //给查询的值赋予权重
+							"fields": []interface{}{"Abstract"},
+							"query":  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, //给查询的值赋予权重
+							"fields": []interface{}{"Annotation"},
+							"query":  v,
+						},
+					},
+				},
+			})
 			shouldMapquery = append(shouldMapquery, map[string]interface{}{
 				"function_score": map[string]interface{}{
 					"query": map[string]interface{}{
@@ -955,8 +977,10 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	highlightMap := make(map[string]interface{}, 0)
 	highlightMap = map[string]interface{}{
 		"fields": map[string]interface{}{
-			"BodyText": map[string]interface{}{},
-			"Title":    map[string]interface{}{},
+			"BodyText":   map[string]interface{}{},
+			"Title":      map[string]interface{}{},
+			"Abstract":   map[string]interface{}{},
+			"Annotation": map[string]interface{}{},
 		},
 		//样式 红色
 		"post_tags": []interface{}{"</font>"},
@@ -979,9 +1003,9 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 	queryMap["from"] = startSize
 	queryMap["size"] = pageSize
 	queryMap["highlight"] = highlightMap
-	jsonBytes, _ := json.Marshal(queryMap)
-	fmt.Println(string(jsonBytes))
-	utils.FileLog.Info(string(jsonBytes))
+	//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 {
@@ -999,8 +1023,18 @@ func EsArticleSearch(keyWord string, startSize, pageSize int, orderColumn string
 				}
 				searchItem := new(models.SearchItem)
 				searchItem.ArticleId, _ = strconv.Atoi(v.Id)
-				if len(v.Highlight["BodyText"]) > 0 {
+				if len(v.Highlight["BodyText"]) > 0 || len(v.Highlight["Abstract"]) > 0 || len(v.Highlight["Annotation"]) > 0 {
 					searchItem.Body = v.Highlight["BodyText"]
+					if len(v.Highlight["Abstract"]) > 0 {
+						for _, vRed := range v.Highlight["Abstract"] {
+							searchItem.Body = append(searchItem.Body, vRed)
+						}
+					}
+					if len(v.Highlight["Annotation"]) > 0 {
+						for _, vRed := range v.Highlight["Annotation"] {
+							searchItem.Body = append(searchItem.Body, vRed)
+						}
+					}
 				} else {
 					bodyRune := []rune(article.BodyText)
 					bodyRuneLen := len(bodyRune)

+ 17 - 5
services/task.go

@@ -9,7 +9,6 @@ import (
 	"html"
 	"strconv"
 	"strings"
-	"time"
 )
 
 func Task() {
@@ -107,7 +106,6 @@ func Task() {
 		getArticleListByApi := task.NewTask("getArticleListByApi", "0 */60 * * * *", GetArticleListByApi) //通过三方接口获取策略平台上的文章
 		task.AddTask("getArticleListByApi", getArticleListByApi)
 	}
-
 	//GetCygxActivityAttendanceDetail()
 	//CreateIndexNameArticleHistory()
 	//AddAllArticleHistory()
@@ -161,6 +159,16 @@ func CreateIndex() {
        "term_vector": "with_positions_offsets",
 		"analyzer": "ik_smart"
      },
+     "Abstract": {
+       "type": "text",
+       "term_vector": "with_positions_offsets",
+		"analyzer": "ik_smart"
+     },
+     "Annotation": {
+       "type": "text",
+       "term_vector": "with_positions_offsets",
+		"analyzer": "ik_smart"
+     },
      "TitleEn": {
        "type": "text",
        "term_vector": "with_positions_offsets"
@@ -181,9 +189,9 @@ func CreateIndex() {
 }
 
 func AddAllArticle() {
-	endDate := time.Now().AddDate(0, 0, -30).Format(utils.FormatDate)
-	//allList, err := models.GetArticleAll()
-	allList, err := models.GetArticleAllDate(endDate)
+	//endDate := time.Now().AddDate(0, 0, -30).Format(utils.FormatDate)
+	allList, err := models.GetArticleAll()
+	//allList, err := models.GetArticleAllDate(endDate)
 	if err != nil {
 		fmt.Println("GetArticleAll Err:", err.Error())
 		return
@@ -203,6 +211,8 @@ func AddAllArticle() {
 		item.PublishDate = v.PublishDate
 		item.BodyText = bodyText
 		item.ExpertBackground = v.ExpertBackground
+		item.Annotation, _ = GetReportContentTextSubNew(v.Annotation)
+		item.Abstract, _ = GetReportContentTextSubNew(v.Abstract)
 		item.CategoryId = strconv.Itoa(v.CategoryId)
 		EsAddOrEditData(indexName, strconv.Itoa(v.ArticleId), item)
 		fmt.Println(v.ArticleId)
@@ -272,6 +282,8 @@ type ElasticTestArticleDetail struct {
 	PublishDate      string `description:"发布时间"`
 	ExpertBackground string `description:"专家背景"`
 	CategoryId       string `description:"文章分类"`
+	Abstract         string `description:"摘要"`
+	Annotation       string `description:"核心观点"`
 }
 
 type ElasticTestArticleDetailV4 struct {