From cb08c725ca671fa8a97412f1fde41d911dc0cca7 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Thu, 26 Mar 2020 00:30:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E5=80=99?= =?UTF-8?q?=E8=AE=BE=E7=BD=AEelasticsearch=20mapping=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=88=90=E5=8A=9F=EF=BC=8Celk=E5=B0=81=E8=A3=85=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=B7=BB=E5=8A=A0=E5=88=86=E9=A1=B5query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/hardware.go | 11 +++++++++++ db/elasticEngine.go | 7 ++++--- main.go | 10 ++++++++++ model/hardware.go | 34 +++++++++++++++++++++++++++------- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/controller/hardware.go b/controller/hardware.go index 87ec3ca..2369812 100644 --- a/controller/hardware.go +++ b/controller/hardware.go @@ -38,5 +38,16 @@ func UpdateHardWare(c *gin.Context) { } func ReadHardWare(c *gin.Context) { + var req model.Hardware + rsp := RespBase{"ERR",-1,nil} + defer func() { + c.JSON(200,rsp) + }() + e := c.BindJSON(&req) + if nil != e{ + log.Error(e.Error()) + return + } + //limit,offset := GetPageParaFromQuery(c) } diff --git a/db/elasticEngine.go b/db/elasticEngine.go index e5ed5e3..ae30c22 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -66,13 +66,14 @@ func (p *ElkEngine)Delete(index string,types string,id string) error{ /* */ -func (p *ElkEngine)Query(index string,types string,query elastic.Query,data interface{}) ([]interface{},error) { +func (p *ElkEngine)Query(index string, + types string,query elastic.Query,data interface{}, + limit int,offset int) ([]interface{},error) { if nil != p{ res, err := p.cli. Search(index). Type(types). - Query(query). - Do() + Query(query).Size(limit).From(limit*offset).Do() if err != nil { print(err) return nil,err diff --git a/main.go b/main.go index 7f8ddcc..c79abff 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "background/controller/middle" "background/db" "background/logs" + "background/model" "github.com/gin-gonic/gin" "github.com/tommy351/gin-sessions" "log" @@ -39,6 +40,12 @@ func InitRedis() { return } } +func InitElasticSearch(){ + e := db.GetElastic().InitMapping("hardware","0",model.HardwareTypeMapping()) + if nil != e{ + log.Print(e.Error()) + } +} func InitLogs() { logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile) } @@ -73,6 +80,7 @@ func main() { InitLogs() InitRedis() InitMysql() + InitElasticSearch() //o := db.GetMongoDb() //mgo = mgo @@ -118,6 +126,8 @@ func main() { api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 api.POST("/hardware",controller.AddHardware) // 新增硬件 + api.GET("/hardware",controller.ReadHardWare) // 新增硬件 + } e := r.Run(":" + strconv.Itoa(config.GetPort())) diff --git a/model/hardware.go b/model/hardware.go index e3c460b..4c98596 100644 --- a/model/hardware.go +++ b/model/hardware.go @@ -6,14 +6,29 @@ import ( "qiniupkg.com/x/log.v7" ) +func HardwareTypeMapping() (string){ + return `"mappings":{ + "hardware":{ + "properties":{ + "id":{"type":"keyword"}, + "name":{"type":"text"}, + "desc":{"type":"text"}, + "id":{"type":"interger"}, + "pic":{"type":"doc"}, + "doc":{"type":"doc"} + } + } + }` +} + // this api is based on elasticsearch type Hardware struct { - ID int32 `json:"id"` - BuyDate string `json:"buy_date"` //购入时间 - Name string `json:"name"` // 名字 - Desc string `json:"desc"` // 描述 - Pic string `json:"pic"` // 图片 - Doc string `json:"doc"` //文档资料 + ID int32 `json:"id,omitempty"` + BuyDate string `json:"buy_date,omitempty"` //购入时间 + Name string `json:"name,omitempty"` // 名字 + Desc string `json:"desc,omitempty"` // 描述 + Pic string `json:"pic,omitempty"` // 图片 + Doc string `json:"doc,omitempty"` //文档资料 } func (this *Hardware )CreateHardware( ) error{ @@ -26,4 +41,9 @@ func (this *Hardware )CreateHardware( ) error{ return e } return nil; -} \ No newline at end of file +} + +func (this *Hardware)Hardwares() ([]Hardware,error){ + + return nil,nil +}