基于elasticsearch作为数据存储的硬件管理接口实现
parent
9e7b384836
commit
9f36826bc0
|
@ -4,6 +4,7 @@ import (
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gopkg.in/olivere/elastic.v3"
|
"gopkg.in/olivere/elastic.v3"
|
||||||
|
"qiniupkg.com/x/log.v7"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
const(
|
const(
|
||||||
|
@ -21,21 +22,21 @@ type ElkEngine struct {
|
||||||
|
|
||||||
func (p *ElkEngine)Create(index string,types string,id string,data interface{}) (error) {
|
func (p *ElkEngine)Create(index string,types string,id string,data interface{}) (error) {
|
||||||
if nil != p{
|
if nil != p{
|
||||||
if (reflect.TypeOf(data).Kind() != reflect.String) && (reflect.TypeOf(data).Kind() != reflect.Struct){
|
if (reflect.TypeOf(data).Kind() == reflect.String) || (reflect.TypeOf(data).Kind() == reflect.Struct){
|
||||||
resp, err := p.cli.Index().
|
resp, err := p.cli.Index().
|
||||||
Index(index).
|
Index(index).
|
||||||
Type(types).
|
Type(types).
|
||||||
Id(id).
|
|
||||||
BodyJson(data).
|
BodyJson(data).
|
||||||
Do()
|
Do()
|
||||||
if !resp.Created{
|
if !resp.Created{
|
||||||
return errors.New(CREATED_ERROR)
|
return errors.New(CREATED_ERROR)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
log.Print(reflect.TypeOf(data).Kind())
|
||||||
return errors.New(INPUT_TYPE_ERROR)
|
return errors.New(INPUT_TYPE_ERROR)
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
|
|
@ -149,6 +149,5 @@ func InitMongoDb() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func GetElastic() *ElkEngine{
|
func GetElastic() *ElkEngine{
|
||||||
|
|
||||||
return &gElkEngine
|
return &gElkEngine
|
||||||
}
|
}
|
|
@ -4,13 +4,12 @@ import (
|
||||||
"background/db"
|
"background/db"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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"`
|
||||||
BuyDate time.Time `json:"buy_date"` //购入时间
|
BuyDate string `json:"buy_date"` //购入时间
|
||||||
Name string `json:"name"` // 名字
|
Name string `json:"name"` // 名字
|
||||||
Desc string `json:"desc"` // 描述
|
Desc string `json:"desc"` // 描述
|
||||||
Pic string `json:"pic"` // 图片
|
Pic string `json:"pic"` // 图片
|
||||||
|
@ -21,7 +20,7 @@ func (this *Hardware )CreateHardware( ) error{
|
||||||
if nil == this{
|
if nil == this{
|
||||||
return errors.New("null pointer")
|
return errors.New("null pointer")
|
||||||
}
|
}
|
||||||
e := db.GetElastic().Create("hardware","0","",*this)
|
e := db.GetElastic().Create("hardware","0","sdfasdfasd",*this)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
return e
|
return e
|
||||||
|
|
Loading…
Reference in New Issue