no message

master
zcy 2021-08-01 19:50:48 +08:00
parent 9c71c7b022
commit ab40d156b2
4 changed files with 109 additions and 2 deletions

View File

@ -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() {

View File

@ -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) {

View File

@ -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) //获取所有文章个数

View File

@ -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
}