添加内容检索接口

master
DESKTOP-4RNDQIC\29019 2021-05-23 01:27:55 +08:00
parent 7ca204390c
commit fe0602f67c
4 changed files with 59 additions and 10 deletions

View File

@ -8,6 +8,7 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/olivere/elastic"
"qiniupkg.com/x/log.v7"
)
@ -214,12 +215,33 @@ func UpdateArtilce(c *gin.Context) {
rsp.Status = 0
}
func SearchArticle(c *gin.Context) {
func SearchArticleES(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.NewTermQuery("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 AddArticle(c *gin.Context) {
rsp := RespBase{Msg: "FAIL", Status: 210}
type ReqAddArticle struct {
@ -288,6 +310,7 @@ func DeleteArticleType(c *gin.Context) {
rsp.Msg = "OK"
rsp.Status = 0
}
func AddArticleType(c *gin.Context) {
rsp := RespBase{Msg: "Fail", Status: 210}
defer func() {
@ -367,6 +390,27 @@ func ArticlesTypes(c *gin.Context) {
resp.Status = 0
}
func SearchArticles(c *gin.Context) {
type ReqSearch struct{
Term string `json:"term"`
}
resp := RespBase{"unkown error", -231, nil}
defer func() {
c.JSON(200, resp)
}()
docTypes := []model.Doc{}
e := db.GetMysqlClient().Query2("select * from doc_type",
&docTypes)
if nil != e {
log.Print(e.Error())
return
}
resp.Data = docTypes
resp.Msg = "OK"
resp.Status = 0
}
func CreateMemo(c *gin.Context) {
resp := RespBase{"unkown error", -231, nil}
defer func() {

View File

@ -97,7 +97,6 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{},
log.Print(e.Error())
}
obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj)
log.Print(obj)
if nil != e {
log.Print(e.Error())
}
@ -114,6 +113,7 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{},
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())
}

View File

@ -134,10 +134,12 @@ func main() {
api.GET("/article/:id", controller.GetArticle) //获取文章
api.POST("/articles", controller.GetArticles) // 获取所有文章
api.PUT("/article", controller.AddArticle) // 添加文章
api.PUT("/article_search", controller.SearchArticle) // 添加文章
api.PUT("/article_search", controller.SearchArticles) // 添加文章
api.GET("/article_type", controller.ArticlesType) //获取所有文章分类
api.PUT("/article_type", controller.AddArticleType)
api.DELETE("/article_type", controller.DeleteArticleType)
api.POST("/doc_search_term",controller.SearchArticleES)
api.POST("/article_update", controller.UpdateArtilce) //更新文章
api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数
@ -149,7 +151,7 @@ func main() {
api.POST("/hardware", controller.AddHardware) // 新增硬件
api.GET("/hardware", controller.ReadHardWare) // 读取硬件
api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件
api.POST("/doc_search")
api.PUT("/file", fileController.OnFileUploadFile) // 上传文件
api.GET("/file", fileController.DownloadFile) // 下载 文件
api.GET("/filelist", fileController.FileList) // 文件列表

View File

@ -45,7 +45,10 @@ func InitElasticSearch() {
}
func InitLogs() {
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
logs.Init(config.GetLogConfig().Dir,
config.GetLogConfig().File,
config.GetLogConfig().Level,
config.GetLogConfig().SaveFile)
}
// 从mysql数据自动导出数据到es从describe 表新建索引和mapping然后全表导出到es
@ -56,10 +59,10 @@ func TestPortDocToElastic(t *testing.T) {
InitRedisConfig()
InitMysql()
db.InitELK()
e := model.PortDocumentToElasticsearch("doc")
if nil != e {
log.Print(e.Error())
}
// e := model.PortDocumentToElasticsearch("doc")
// if nil != e {
// log.Print(e.Error())
// }
}
func TestReflect(t *testing.T) {
@ -103,7 +106,7 @@ func TestQueryDoc(t *testing.T) {
log.Print(e.Error())
}
for _, v := range x {
log.Print(v.Title, " ", v.ID, " ", v.Type)
t.Logf(v.Title, " ", v.ID, " ", v.Type)
}
}