es添加全文索引highlight接口

master
zcy 2021-08-01 23:33:55 +08:00
parent ab40d156b2
commit cfc782385c
3 changed files with 24 additions and 10 deletions

View File

@ -8,7 +8,7 @@ import (
"strconv" "strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/olivere/elastic" "gopkg.in/olivere/elastic.v7"
"qiniupkg.com/x/log.v7" "qiniupkg.com/x/log.v7"
) )
@ -165,7 +165,7 @@ func UpdateArtilce(c *gin.Context) {
ID int64 `json:"id"` ID int64 `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Content string `json:"content"` Content string `json:"content"`
author string `json:"author"` Author string `json:"author"`
Type int64 `json:"type"` Type int64 `json:"type"`
IsPublic int `json:"is_public"` IsPublic int `json:"is_public"`
} }
@ -201,7 +201,7 @@ func UpdateArtilce(c *gin.Context) {
Type: int32(req.Type), Type: int32(req.Type),
Title: req.Title, Title: req.Title,
Content: req.Content, Content: req.Content,
Author: req.author, Author: req.Author,
ID: req.ID, ID: req.ID,
IsPublic: int32(req.IsPublic), IsPublic: int32(req.IsPublic),
Version: (docs[0].Version + 1), Version: (docs[0].Version + 1),
@ -217,6 +217,12 @@ func UpdateArtilce(c *gin.Context) {
} }
func SearchArticleESHightLight(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} rsp := RespBase{Msg: "FAIL", Status: 210}
defer func() { defer func() {
c.JSON(200, rsp) c.JSON(200, rsp)
@ -232,9 +238,11 @@ func SearchArticleESHightLight(c *gin.Context) {
} }
query := elastic.NewMatchQuery("content", req.Term) query := elastic.NewMatchQuery("content", req.Term)
highlight := elastic.NewHighlight()
x := []model.Doc{} highlight = highlight.Fields(elastic.NewHighlighterField("content"))
_, e = db.GetElastic().Query("doc", query, &x, 10, 0) highlight = highlight.PreTags("<span>").PostTags("</span>")
x := []RetHighlight{}
_, e = db.GetElastic().QueryHighlight("doc", query, &x,highlight, 10, 0)
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())
return return

View File

@ -79,19 +79,21 @@ func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interfac
if nil != p { if nil != p {
if limit == 0 { 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 { if err != nil {
print(err) print(err)
return nil, err return nil, err
} }
id := []string{} id := []string{}
for _, vs := range res.Hits.Hits { for _, vs := range res.Hits.Hits {
log.Print(vs.Highlight)
id = append(id, vs.Id) id = append(id, vs.Id)
data, e := vs.Source.MarshalJSON() data, e := vs.Source.MarshalJSON()
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())
} }
mapobj := map[string]interface{}{} mapobj := map[string]interface{}{}
mapobj["highlight"] = vs.Highlight["content"]
e = json.Unmarshal(data, &mapobj) e = json.Unmarshal(data, &mapobj)
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())
@ -104,16 +106,17 @@ func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interfac
} }
return id, nil return id, nil
} else { } 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 { if err != nil {
print(err) print(err)
return nil, err return nil, err
} }
id := []string{} id := []string{}
for _, vs := range res.Hits.Hits { for _, vs := range res.Hits.Hits {
log.Print(vs.Highlight)
id = append(id, vs.Id) id = append(id, vs.Id)
data, e := vs.Source.MarshalJSON() data, e := vs.Source.MarshalJSON()
log.Print(string(data)) // log.Print(string(data))
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())
} }
@ -122,6 +125,8 @@ func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interfac
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())
} }
mapobj["content"] = ""
mapobj["highlight"] = vs.Highlight["content"]
obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj) obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj)
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())

View File

@ -249,12 +249,13 @@ func UnmarshalJson2StructGen(t reflect.Type, maps map[string]interface{}) (inter
if !ok { if !ok {
return nil, errors.New(TagNoExisted) return nil, errors.New(TagNoExisted)
} }
log.Print(remap["json"])
ret := reflect.New(t) ret := reflect.New(t)
// log.Print(ret.Kind()) // log.Print(ret.Kind())
opStruct := ret.Elem() opStruct := ret.Elem()
// log.Print(opStruct.Kind()) // log.Print(opStruct.Kind())
for k, v := range maps { for k, v := range maps {
log.Print(k) // log.Print(k)
if filedName, ok := remap["json"][k]; ok { if filedName, ok := remap["json"][k]; ok {
// log.Print(filedName, " ", opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind().String()) // log.Print(filedName, " ", opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind().String())
if SameKind(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind()) { if SameKind(opStruct.FieldByName(filedName).Kind(), reflect.TypeOf(v).Kind()) {