初始化时候设置elasticsearch mapping测试成功,elk封装接口添加分页query

master
a7458969 2020-03-26 00:30:07 +08:00
parent 8657f73f35
commit cb08c725ca
4 changed files with 52 additions and 10 deletions

View File

@ -38,5 +38,16 @@ func UpdateHardWare(c *gin.Context) {
} }
func ReadHardWare(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)
} }

View File

@ -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{ if nil != p{
res, err := p.cli. res, err := p.cli.
Search(index). Search(index).
Type(types). Type(types).
Query(query). Query(query).Size(limit).From(limit*offset).Do()
Do()
if err != nil { if err != nil {
print(err) print(err)
return nil,err return nil,err

10
main.go
View File

@ -6,6 +6,7 @@ import (
"background/controller/middle" "background/controller/middle"
"background/db" "background/db"
"background/logs" "background/logs"
"background/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/tommy351/gin-sessions" "github.com/tommy351/gin-sessions"
"log" "log"
@ -39,6 +40,12 @@ func InitRedis() {
return return
} }
} }
func InitElasticSearch(){
e := db.GetElastic().InitMapping("hardware","0",model.HardwareTypeMapping())
if nil != e{
log.Print(e.Error())
}
}
func InitLogs() { 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)
} }
@ -73,6 +80,7 @@ func main() {
InitLogs() InitLogs()
InitRedis() InitRedis()
InitMysql() InitMysql()
InitElasticSearch()
//o := db.GetMongoDb() //o := db.GetMongoDb()
//mgo = mgo //mgo = mgo
@ -118,6 +126,8 @@ func main() {
api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型
api.POST("/hardware",controller.AddHardware) // 新增硬件 api.POST("/hardware",controller.AddHardware) // 新增硬件
api.GET("/hardware",controller.ReadHardWare) // 新增硬件
} }
e := r.Run(":" + strconv.Itoa(config.GetPort())) e := r.Run(":" + strconv.Itoa(config.GetPort()))

View File

@ -6,14 +6,29 @@ import (
"qiniupkg.com/x/log.v7" "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 // this api is based on elasticsearch
type Hardware struct { type Hardware struct {
ID int32 `json:"id"` ID int32 `json:"id,omitempty"`
BuyDate string `json:"buy_date"` //购入时间 BuyDate string `json:"buy_date,omitempty"` //购入时间
Name string `json:"name"` // 名字 Name string `json:"name,omitempty"` // 名字
Desc string `json:"desc"` // 描述 Desc string `json:"desc,omitempty"` // 描述
Pic string `json:"pic"` // 图片 Pic string `json:"pic,omitempty"` // 图片
Doc string `json:"doc"` //文档资料 Doc string `json:"doc,omitempty"` //文档资料
} }
func (this *Hardware )CreateHardware( ) error{ func (this *Hardware )CreateHardware( ) error{
@ -26,4 +41,9 @@ func (this *Hardware )CreateHardware( ) error{
return e return e
} }
return nil; return nil;
} }
func (this *Hardware)Hardwares() ([]Hardware,error){
return nil,nil
}