es数据库接口封装

master
a7458969 2020-03-21 01:50:04 +08:00
parent 0a452fe617
commit eba983e8b2
1 changed files with 51 additions and 2 deletions

View File

@ -3,9 +3,17 @@ package db
import (
"background/config"
"background/logs"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v3"
"reflect"
)
const(
ERROR_PTR = "nullpointer error"
INPUT_TYPE_ERROR = "wrong input parameter"
CREATED_ERROR = "create error"
DELETE_ERROR = "delete error"
)
type ElkEngine struct {
cli *elastic.Client
}
@ -25,10 +33,51 @@ func InitELK() {
}
}
func (*ElkEngine)Search(index string) (error) {
if nil != gElkEngine.cli{
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){
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 {
return err
}
}else{
return errors.New(INPUT_TYPE_ERROR)
}
}else{
return errors.New(ERROR_PTR)
}
return nil
}
func (p *ElkEngine)Delete(index string,types string,id string) error{
if nil != p{
res, err := p.cli.Delete().Index(index).
Type(types).
Id(id).
Do()
if err != nil {
return err
}
if !res.Found{
return errors.New(DELETE_ERROR)
}
}else{
return errors.New(ERROR_PTR)
}
return nil
}
func (p *ElkEngine)Search(index string,types string,id string) (interface{},error){
return nil,nil
}