From ab40d156b2288cc912c03ff2cef32859a9aae171 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Sun, 1 Aug 2021 19:50:48 +0800 Subject: [PATCH] no message --- controller/blog.go | 30 ++++++++++++++++++ db/elasticEngine.go | 77 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + model/port.go | 3 +- 4 files changed, 109 insertions(+), 2 deletions(-) diff --git a/controller/blog.go b/controller/blog.go index 079816b..9d9bb57 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -215,6 +215,36 @@ func UpdateArtilce(c *gin.Context) { rsp.Status = 0 } + +func SearchArticleESHightLight(c *gin.Context) { + rsp := RespBase{Msg: "FAIL", Status: 210} + defer func() { + c.JSON(200, rsp) + }() + type Req struct{ + Term string `json:"term"` + } + var req Req + e := c.BindJSON(&req) + if nil != e{ + log.Print(e.Error()) + return + } + + query := elastic.NewMatchQuery("content", req.Term) + + x := []model.Doc{} + _, e = db.GetElastic().Query("doc", query, &x, 10, 0) + if nil != e { + log.Print(e.Error()) + return + } + + rsp.Data = x + rsp.Msg = "OK" + rsp.Status = 0 +} + func SearchArticleES(c *gin.Context) { rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 1c4e25b..8b5c3bd 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -60,6 +60,83 @@ func (p *ElkEngine) Delete(query elastic.Query, index string) error { return nil } + +func (p *ElkEngine) QueryHighlight(index string, query elastic.Query, v interface{},hightlight *elastic.Highlight, + limit int, offset int) ([]string, error) { + + if reflect.ValueOf(v).Kind() != reflect.Ptr { + return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Ptr") + } + if reflect.ValueOf(v).Elem().Kind() != reflect.Slice { + return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Slice") + } + if reflect.ValueOf(v).Elem().Type().Elem().Kind() != reflect.Struct { + return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Struct") + } + eletype := reflect.ValueOf(v).Elem().Type().Elem() + obj := reflect.ValueOf(v).Elem() + objAdd := make([]reflect.Value, 0) + + if nil != p { + if limit == 0 { + res, err := p.cli.Search().Index(index).Query(query).Highlight(hightlight).Do(context.Background()) + if err != nil { + print(err) + return nil, err + } + id := []string{} + for _, vs := range res.Hits.Hits { + id = append(id, vs.Id) + data, e := vs.Source.MarshalJSON() + if nil != e { + log.Print(e.Error()) + } + mapobj := map[string]interface{}{} + e = json.Unmarshal(data, &mapobj) + if nil != e { + log.Print(e.Error()) + } + obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj) + if nil != e { + log.Print(e.Error()) + } + objAdd = append(objAdd, reflect.ValueOf(obj)) + } + return id, nil + } else { + res, err := p.cli.Search(index).Query(query).Highlight(hightlight).Size(limit).From(limit * offset).Do(context.Background()) + if err != nil { + print(err) + return nil, err + } + id := []string{} + for _, vs := range res.Hits.Hits { + id = append(id, vs.Id) + data, e := vs.Source.MarshalJSON() + log.Print(string(data)) + if nil != e { + log.Print(e.Error()) + } + mapobj := map[string]interface{}{} + e = json.Unmarshal(data, &mapobj) + if nil != e { + log.Print(e.Error()) + } + obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj) + if nil != e { + log.Print(e.Error()) + } + objAdd = append(objAdd, reflect.ValueOf(obj)) + } + addOp := reflect.Append(obj, objAdd...) + obj.Set(addOp) + return id, nil + } + } else { + return nil, errors.New(ERROR_PTR) + } +} + func (p *ElkEngine) Query(index string, query elastic.Query, v interface{}, limit int, offset int) ([]string, error) { diff --git a/main.go b/main.go index eeb0a7d..8ec7b70 100644 --- a/main.go +++ b/main.go @@ -139,6 +139,7 @@ func main() { api.PUT("/article_type", controller.AddArticleType) // 添加文章分类 api.DELETE("/article_type", controller.DeleteArticleType) // 删除文章分类 api.POST("/doc_search_term",controller.SearchArticleES) // 文章内容搜索,基于es的倒排 + api.POST("/doc_search_hightlight",controller.SearchArticleESHightLight) // 文章内容搜索,基于es的倒排 api.POST("/article_update", controller.UpdateArtilce) //更新文章 api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数 diff --git a/model/port.go b/model/port.go index 10d0376..12d453a 100644 --- a/model/port.go +++ b/model/port.go @@ -157,7 +157,7 @@ func PortDocumentToElasticsearch(tblname string) error { "analysis": map[string]interface{}{ "analyzer": map[string]interface{}{ "default": map[string]interface{}{ - "type": "smartcn", + "type": "ik_max_word", }, }, }, @@ -186,6 +186,5 @@ func PortDocumentToElasticsearch(tblname string) error { } } } - return nil }