diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 600254b..5cf8f40 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -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 +} \ No newline at end of file