|
@@ -5,12 +5,14 @@ import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
"hongze/hongze_cygx/models"
|
|
"hongze/hongze_cygx/models"
|
|
- "hongze/hongze_cygx/utils"
|
|
+ "html"
|
|
"strconv"
|
|
"strconv"
|
|
|
|
+ "strings"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ "github.com/PuerkitoBio/goquery"
|
|
"github.com/olivere/elastic/v7"
|
|
"github.com/olivere/elastic/v7"
|
|
"github.com/olivere/elastic/v7/config"
|
|
"github.com/olivere/elastic/v7/config"
|
|
)
|
|
)
|
|
@@ -54,19 +56,35 @@ func SaveData() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+ 3161,3190,3226,3244,3264,3285,3310,3334,3370,3397,3418,3446,3477,3497,3526,3554
|
|
-
|
|
+ */
|
|
-
|
|
+
|
|
-
|
|
+ idStr := `3161,3190,3226,3244,3264,3285,3310,3334,3370,3397,3418,3446,3477,3497,3526,3554`
|
|
-
|
|
+ idArr := strings.Split(idStr, ",")
|
|
-
|
|
+ for _, v := range idArr {
|
|
-
|
|
+ id, _ := strconv.Atoi(v)
|
|
-
|
|
+ item, err := models.GetArticleDetailById(id)
|
|
-
|
|
+ if err != nil {
|
|
-
|
|
+ fmt.Println("GetArticleDetailById Err:" + err.Error())
|
|
-
|
|
+ return
|
|
-
|
|
+ }
|
|
|
|
+ content := html.UnescapeString(item.Body)
|
|
|
|
+ doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println("create doc err:", err.Error())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ bodyText := doc.Text()
|
|
|
|
+ item.BodyText = bodyText
|
|
|
|
+
|
|
|
|
+ resp, err := client.Index().Index(esIndex).Id(strconv.Itoa(item.ArticleId)).BodyJson(item).Do(context.Background())
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Println("insert es failed", err.Error())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ fmt.Println(resp.Status)
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -94,25 +112,25 @@ func SaveData() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- boolquery := elastic.NewBoolQuery()
|
|
+
|
|
|
|
|
|
- boolquery.Should(elastic.NewMatchQuery("Body", "专家"))
|
|
+
|
|
- highlight := elastic.NewHighlight()
|
|
+
|
|
- highlight = highlight.Fields(elastic.NewHighlighterField("Body"))
|
|
+
|
|
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
+
|
|
|
|
|
|
- var pageSize int
|
|
+
|
|
- pageSize = 20
|
|
+
|
|
|
|
|
|
- searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
|
|
+
|
|
|
|
|
|
- var result string
|
|
+
|
|
- if searchByMatch.Hits != nil {
|
|
+
|
|
|
|
|
|
- }
|
|
+
|
|
-
|
|
+
|
|
|
|
|
|
- utils.FileLog.Info("%s", string(result))
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -151,10 +169,10 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
|
|
var esIndex = "cygx_article"
|
|
var esIndex = "cygx_article"
|
|
boolquery := elastic.NewBoolQuery()
|
|
boolquery := elastic.NewBoolQuery()
|
|
|
|
|
|
- boolquery.Should(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("Body", keyWord))
|
|
+ boolquery.Must(elastic.NewMatchQuery("Title", keyWord), elastic.NewMatchQuery("BodyText", keyWord))
|
|
|
|
|
|
highlight := elastic.NewHighlight()
|
|
highlight := elastic.NewHighlight()
|
|
- highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("Body"))
|
|
+ highlight = highlight.Fields(elastic.NewHighlighterField("Title"), elastic.NewHighlighterField("BodyText"))
|
|
highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
|
|
|
|
searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
|
|
searchByMatch, err := client.Search(esIndex).Highlight(highlight).Size(pageSize).Query(boolquery).Do(context.Background())
|
|
@@ -175,7 +193,7 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
|
|
}
|
|
}
|
|
searchItem := new(models.SearchItem)
|
|
searchItem := new(models.SearchItem)
|
|
searchItem.ArticleId, _ = strconv.Atoi(v.Id)
|
|
searchItem.ArticleId, _ = strconv.Atoi(v.Id)
|
|
- searchItem.Body = v.Highlight["Body"]
|
|
+ searchItem.Body = v.Highlight["BodyText"]
|
|
var title string
|
|
var title string
|
|
if len(v.Highlight["Title"]) > 0 {
|
|
if len(v.Highlight["Title"]) > 0 {
|
|
title = v.Highlight["Title"][0]
|
|
title = v.Highlight["Title"][0]
|