基于elasticsearch作为数据存储的硬件管理接口实现

master
a7458969 2020-03-25 11:26:32 +08:00
parent 9e7b384836
commit 9f36826bc0
3 changed files with 6 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import (
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v3"
"qiniupkg.com/x/log.v7"
"reflect"
)
const(
@ -21,21 +22,21 @@ type ElkEngine struct {
func (p *ElkEngine)Create(index string,types string,id string,data interface{}) (error) {
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().
Index(index).
Type(types).
Id(id).
BodyJson(data).
Do()
if !resp.Created{
return errors.New(CREATED_ERROR)
}
if err != nil {
print(err)
log.Print(err)
return err
}
}else{
log.Print(reflect.TypeOf(data).Kind())
return errors.New(INPUT_TYPE_ERROR)
}
}else{

View File

@ -149,6 +149,5 @@ func InitMongoDb() {
}
}
func GetElastic() *ElkEngine{
return &gElkEngine
}

View File

@ -4,13 +4,12 @@ import (
"background/db"
"github.com/pkg/errors"
"qiniupkg.com/x/log.v7"
"time"
)
// this api is based on elasticsearch
type Hardware struct {
ID int32 `json:"id"`
BuyDate time.Time `json:"buy_date"` //购入时间
BuyDate string `json:"buy_date"` //购入时间
Name string `json:"name"` // 名字
Desc string `json:"desc"` // 描述
Pic string `json:"pic"` // 图片
@ -21,7 +20,7 @@ func (this *Hardware )CreateHardware( ) error{
if nil == this{
return errors.New("null pointer")
}
e := db.GetElastic().Create("hardware","0","",*this)
e := db.GetElastic().Create("hardware","0","sdfasdfasd",*this)
if nil != e{
log.Print(e.Error())
return e