|
@@ -5,12 +5,14 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"hongze/hongze_cygx/models"
|
|
|
- "hongze/hongze_cygx/utils"
|
|
|
+ "html"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
|
|
|
//"gopkg.in/olivere/elastic.v5"
|
|
|
//"gopkg.in/olivere/elastic.v5/config"
|
|
|
|
|
|
+ "github.com/PuerkitoBio/goquery"
|
|
|
"github.com/olivere/elastic/v7"
|
|
|
"github.com/olivere/elastic/v7/config"
|
|
|
)
|
|
@@ -54,19 +56,35 @@ func SaveData() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //item, err := models.GetArticleDetailById(3138)
|
|
|
- //if err != nil {
|
|
|
- // fmt.Println("GetArticleDetailById Err:" + err.Error())
|
|
|
- // return
|
|
|
- //}
|
|
|
- //
|
|
|
- ////插入一条数据,id指定为对应的name,若不指定则随机生成
|
|
|
- //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)
|
|
|
+ /*
|
|
|
+ 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 := elastic.NewBoolQuery()
|
|
|
|
|
|
- boolquery.Should(elastic.NewMatchQuery("Body", "专家"))
|
|
|
- highlight := elastic.NewHighlight()
|
|
|
- highlight = highlight.Fields(elastic.NewHighlighterField("Body"))
|
|
|
- highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
|
|
|
+ // 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
|
|
|
+ // var pageSize int
|
|
|
+ // pageSize = 20
|
|
|
|
|
|
- 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())
|
|
|
|
|
|
- var result string
|
|
|
- if searchByMatch.Hits != nil {
|
|
|
+ // var result string
|
|
|
+ // if searchByMatch.Hits != nil {
|
|
|
|
|
|
- }
|
|
|
- //fmt.Println(string(result))
|
|
|
+ // }
|
|
|
+ // //fmt.Println(string(result))
|
|
|
|
|
|
- utils.FileLog.Info("%s", string(result))
|
|
|
+ // utils.FileLog.Info("%s", string(result))
|
|
|
|
|
|
//var resultType models.CygxArticle
|
|
|
//for k,item := range searchByMatch.Each(reflect.TypeOf(resultType)) {
|
|
@@ -151,10 +169,10 @@ func SearchByKeyWord(keyWord string, pageSize int) (result []*models.SearchItem,
|
|
|
var esIndex = "cygx_article"
|
|
|
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 = 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>")
|
|
|
|
|
|
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.ArticleId, _ = strconv.Atoi(v.Id)
|
|
|
- searchItem.Body = v.Highlight["Body"]
|
|
|
+ searchItem.Body = v.Highlight["BodyText"]
|
|
|
var title string
|
|
|
if len(v.Highlight["Title"]) > 0 {
|
|
|
title = v.Highlight["Title"][0]
|