|
@@ -5,6 +5,7 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"hongze/hongze_cygx/models"
|
|
|
+ "hongze/hongze_cygx/utils"
|
|
|
"html"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -167,47 +168,65 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
|
|
|
return
|
|
|
}
|
|
|
var esIndex = "cygx_article"
|
|
|
- boolquery := elastic.NewBoolQuery()
|
|
|
|
|
|
- boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
|
|
|
+ fmt.Println("get start keyWordArr")
|
|
|
+ keyWordArr, err := GetIndustryMapNameSlice(keyWord)
|
|
|
+ if err != nil {
|
|
|
+ go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "GetIndustryMapNameSlice:"+err.Error(), utils.EmailSendToUsers)
|
|
|
+ }
|
|
|
+ keyWordArr = append(keyWordArr, keyWord)
|
|
|
+ fmt.Println(keyWord)
|
|
|
+ fmt.Println(keyWordArr)
|
|
|
+ var newKeyWordArr []string
|
|
|
+ if len(keyWordArr) > 2 {
|
|
|
+ newKeyWordArr = append(keyWordArr[2:], keyWordArr[0:2]...)
|
|
|
+ }
|
|
|
+ fmt.Println(newKeyWordArr)
|
|
|
+ fmt.Println("get end keyWordArr")
|
|
|
+ searchMap := make(map[int]int)
|
|
|
+ for k, v := range newKeyWordArr {
|
|
|
+ fmt.Println(k, v)
|
|
|
+ boolquery := elastic.NewBoolQuery()
|
|
|
|
|
|
- highlight := elastic.NewHighlight()
|
|
|
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
|
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
|
+ boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
|
|
|
|
|
|
- searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
|
|
|
+ highlight := elastic.NewHighlight()
|
|
|
+ highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
|
+ highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
|
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- if searchByMatch.Hits != nil {
|
|
|
- for _, v := range searchByMatch.Hits.Hits {
|
|
|
- articleJson, err := v.Source.MarshalJSON()
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- article := new(models.CygxArticle)
|
|
|
- err = json.Unmarshal(articleJson, &article)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- searchItem := new(models.SearchItem)
|
|
|
- searchItem.ArticleId, _ = strconv.Atoi(v.Id)
|
|
|
- searchItem.Body = v.Highlight["BodyText"]
|
|
|
- var title string
|
|
|
- if len(v.Highlight["Title"]) > 0 {
|
|
|
- title = v.Highlight["Title"][0]
|
|
|
- } else {
|
|
|
- title = article.Title
|
|
|
+ searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return result, err
|
|
|
+ }
|
|
|
+ if searchByMatch.Hits != nil {
|
|
|
+ for _, v := range searchByMatch.Hits.Hits {
|
|
|
+ articleJson, err := v.Source.MarshalJSON()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ article := new(models.CygxArticle)
|
|
|
+ err = json.Unmarshal(articleJson, &article)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if _, ok := searchMap[article.ArticleId]; !ok {
|
|
|
+ searchItem := new(models.SearchItem)
|
|
|
+ searchItem.ArticleId, _ = strconv.Atoi(v.Id)
|
|
|
+ searchItem.Body = v.Highlight["BodyText"]
|
|
|
+ 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
|
|
|
+ result = append(result, searchItem)
|
|
|
+ searchMap[article.ArticleId] = article.ArticleId
|
|
|
+ }
|
|
|
}
|
|
|
- searchItem.Title = title
|
|
|
- searchItem.PublishDate = article.PublishDate
|
|
|
- result = append(result, searchItem)
|
|
|
}
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
-func GetIndustryNameMap(keyWord string) {
|
|
|
-
|
|
|
-}
|