From fe0602f67cd8e0f5d5465fb60d00ea7a347b0638 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sun, 23 May 2021 01:27:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=86=85=E5=AE=B9=E6=A3=80?= =?UTF-8?q?=E7=B4=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/blog.go | 46 ++++++++++++++++++++++++++++++++++++++++++- db/elasticEngine.go | 2 +- main.go | 6 ++++-- test/portData_test.go | 15 ++++++++------ 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/controller/blog.go b/controller/blog.go index 97b545f..613a2cf 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -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() { diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 7a489d1..1c4e25b 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -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()) } diff --git a/main.go b/main.go index 57f7215..823f76f 100644 --- a/main.go +++ b/main.go @@ -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) // 文件列表 diff --git a/test/portData_test.go b/test/portData_test.go index 07ceab2..308f5c3 100644 --- a/test/portData_test.go +++ b/test/portData_test.go @@ -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) } }