From cfc782385c26183cde3567ca829923c42151ff4f Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 1 Aug 2021 23:33:55 +0800 Subject: [PATCH] =?UTF-8?q?es=E6=B7=BB=E5=8A=A0=E5=85=A8=E6=96=87=E7=B4=A2?= =?UTF-8?q?=E5=BC=95highlight=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/blog.go | 20 ++++++++++++++------ db/elasticEngine.go | 11 ++++++++--- utils/reflect.go | 3 ++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/controller/blog.go b/controller/blog.go index 9d9bb57..300b42b 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -8,7 +8,7 @@ import ( "strconv" "github.com/gin-gonic/gin" - "github.com/olivere/elastic" + "gopkg.in/olivere/elastic.v7" "qiniupkg.com/x/log.v7" ) @@ -165,7 +165,7 @@ func UpdateArtilce(c *gin.Context) { ID int64 `json:"id"` Title string `json:"title"` Content string `json:"content"` - author string `json:"author"` + Author string `json:"author"` Type int64 `json:"type"` IsPublic int `json:"is_public"` } @@ -201,7 +201,7 @@ func UpdateArtilce(c *gin.Context) { Type: int32(req.Type), Title: req.Title, Content: req.Content, - Author: req.author, + Author: req.Author, ID: req.ID, IsPublic: int32(req.IsPublic), Version: (docs[0].Version + 1), @@ -217,6 +217,12 @@ func UpdateArtilce(c *gin.Context) { } func SearchArticleESHightLight(c *gin.Context) { + type RetHighlight struct{ + ID int64 `json:"id" gorm:"column:id" sql:"id"` + Title string `json:"title" gorm:"column:title" sql:"title"` + Type int32 `json:"type" gorm:"column:type" sql:"type"` + Highlight []string `json:"highlight" gorm:"column:highlight" sql:"highlight"` + } rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { c.JSON(200, rsp) @@ -232,9 +238,11 @@ func SearchArticleESHightLight(c *gin.Context) { } query := elastic.NewMatchQuery("content", req.Term) - - x := []model.Doc{} - _, e = db.GetElastic().Query("doc", query, &x, 10, 0) + highlight := elastic.NewHighlight() + highlight = highlight.Fields(elastic.NewHighlighterField("content")) + highlight = highlight.PreTags("").PostTags("") + x := []RetHighlight{} + _, e = db.GetElastic().QueryHighlight("doc", query, &x,highlight, 10, 0) if nil != e { log.Print(e.Error()) return diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 8b5c3bd..bf0f402 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -79,19 +79,21 @@ func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interfac if nil != p { if limit == 0 { - res, err := p.cli.Search().Index(index).Query(query).Highlight(hightlight).Do(context.Background()) + res, err := p.cli.Search().Index(index).Query(query).Highlight(hightlight).Do(context.TODO()) if err != nil { print(err) return nil, err } id := []string{} for _, vs := range res.Hits.Hits { + log.Print(vs.Highlight) id = append(id, vs.Id) data, e := vs.Source.MarshalJSON() if nil != e { log.Print(e.Error()) } mapobj := map[string]interface{}{} + mapobj["highlight"] = vs.Highlight["content"] e = json.Unmarshal(data, &mapobj) if nil != e { log.Print(e.Error()) @@ -104,16 +106,17 @@ func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interfac } return id, nil } else { - res, err := p.cli.Search(index).Query(query).Highlight(hightlight).Size(limit).From(limit * offset).Do(context.Background()) + res, err := p.cli.Search(index).Query(query).Highlight(hightlight).Size(limit).From(limit * offset).Do(context.TODO()) if err != nil { print(err) return nil, err } id := []string{} for _, vs := range res.Hits.Hits { + log.Print(vs.Highlight) id = append(id, vs.Id) data, e := vs.Source.MarshalJSON() - log.Print(string(data)) + // log.Print(string(data)) if nil != e { log.Print(e.Error()) } @@ -122,6 +125,8 @@ func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interfac if nil != e { log.Print(e.Error()) } + mapobj["content"] = "" + mapobj["highlight"] = vs.Highlight["content"] obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj) if nil != e { log.Print(e.Error()) diff --git a/utils/reflect.go b/utils/reflect.go index c20af90..2943d1d 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -249,12 +249,13 @@ func UnmarshalJson2StructGen(t reflect.Type, maps map[string]interface{}) (inter if !ok { return nil, errors.New(TagNoExisted) } + log.Print(remap["json"]) ret := reflect.New(t) // log.Print(ret.Kind()) opStruct := ret.Elem() // log.Print(opStruct.Kind()) for k, v := range maps { - log.Print(k) + // log.Print(k) if filedName, ok := remap["json"][k]; ok { // log.Print(filedName, " ", opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind().String()) if SameKind(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind()) {