|
@@ -2,6 +2,7 @@ package services
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"hongze/hongze_cygx/models"
|
|
|
"hongze/hongze_cygx/utils"
|
|
@@ -150,20 +151,39 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
|
|
|
var esIndex = "cygx_article"
|
|
|
boolquery := elastic.NewBoolQuery()
|
|
|
|
|
|
- boolquery.Should(elastic.NewMatchQuery("Body", keyWord))
|
|
|
+ boolquery.Should(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("Body", keyWord))
|
|
|
+
|
|
|
highlight := elastic.NewHighlight()
|
|
|
- highlight = highlight.Fields(elastic.NewHighlighterField("Body"))
|
|
|
+ highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("Body"))
|
|
|
highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
|
|
|
|
searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
|
|
|
+
|
|
|
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["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
|
|
|
result = append(result, searchItem)
|
|
|
}
|
|
|
}
|