background/db/elasticEngine.go

155 lines
3.2 KiB
Go
Raw Normal View History

2020-03-13 09:40:00 +00:00
package db
import (
2020-03-20 17:50:04 +00:00
"github.com/pkg/errors"
"golang.org/x/net/context"
"gopkg.in/olivere/elastic.v7"
"qiniupkg.com/x/log.v7"
2020-03-20 17:50:04 +00:00
"reflect"
2020-03-13 09:40:00 +00:00
)
2020-03-20 17:50:04 +00:00
const(
2020-04-30 17:54:19 +00:00
ERROR_PTR = "null pointer error"
2020-03-20 17:50:04 +00:00
INPUT_TYPE_ERROR = "wrong input parameter"
CREATED_ERROR = "create error"
DELETE_ERROR = "delete error"
INDEX_EXISTED = "index existed"
2020-03-13 09:40:00 +00:00
2020-03-20 17:50:04 +00:00
)
2020-03-13 09:40:00 +00:00
type ElkEngine struct {
cli *elastic.Client
}
2020-03-20 17:50:04 +00:00
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){
2020-03-20 17:50:04 +00:00
resp, err := p.cli.Index().
Index(index).
BodyJson(data).
Do(context.Background())
2020-03-20 17:50:04 +00:00
if err != nil {
log.Print("create error",err)
2020-03-20 17:50:04 +00:00
return err
}
log.Print(resp)
2020-03-20 17:50:04 +00:00
}else{
log.Print(reflect.TypeOf(data).Kind())
2020-03-20 17:50:04 +00:00
return errors.New(INPUT_TYPE_ERROR)
}
}else{
return errors.New(ERROR_PTR)
2020-03-13 11:44:32 +00:00
}
2020-03-13 09:40:00 +00:00
return nil
}
func (p *ElkEngine)Delete(query elastic.Query,index string) error{
2020-03-20 17:50:04 +00:00
if nil != p{
_, err := p.cli.DeleteByQuery().Index(index).Query(query).
Do(context.Background())
2020-03-20 17:50:04 +00:00
if err != nil {
log.Print(err)
2020-03-20 17:50:04 +00:00
return err
2020-03-21 03:17:24 +00:00
}
2020-03-20 17:50:04 +00:00
}else{
return errors.New(ERROR_PTR)
}
2020-03-24 16:30:55 +00:00
return nil
2020-03-20 17:50:04 +00:00
}
2020-03-21 03:17:24 +00:00
/*
*/
func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
2020-03-31 17:07:32 +00:00
limit int,offset int) ([]interface{},[]string,error) {
2020-03-21 03:17:24 +00:00
if nil != p{
if(limit == 0){
2020-03-31 17:07:32 +00:00
res, err := p.cli.Search(index).Query(query).Do(context.Background())
if err != nil {
print(err)
2020-03-31 17:07:32 +00:00
return nil,nil,err
}
//var typ Employee
typ := reflect.TypeOf(data)
2020-03-31 17:07:32 +00:00
rets := res.Each(typ)
id := []string{}
for _,vs := range res.Hits.Hits{
id = append(id,vs.Id)
}
return rets,id,nil
}else{
2020-03-31 17:07:32 +00:00
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit*offset).Do(context.Background())
if err != nil {
print(err)
2020-03-31 17:07:32 +00:00
return nil,nil,err
}
2020-03-31 17:07:32 +00:00
//var typ Employee
typ := reflect.TypeOf(data)
2020-03-31 17:07:32 +00:00
rets := res.Each(typ)
id := []string{}
for _,vs := range res.Hits.Hits{
id = append(id,vs.Id)
}
return rets,id,nil
2020-03-21 03:17:24 +00:00
}
}else{
2020-03-31 17:07:32 +00:00
return nil,nil,errors.New(ERROR_PTR)
2020-03-21 03:17:24 +00:00
}
}
func (p *ElkEngine)Update(index string,types string,id string,data map[string]interface{}) error {
if nil != p {
_, err := p.cli.Update().
Index(index).
Id(id).
Doc(data).
Do(context.Background())
2020-03-21 03:17:24 +00:00
if err != nil {
println(err.Error())
return err
}
}
return errors.New(ERROR_PTR)
}
func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
if nil != p {
exists, err := p.cli.IndexExists(index).Do(context.Background())
if err != nil {
// Handle error
log.Print(err)
return err
}
if exists{
return errors.New(INDEX_EXISTED)
}
2020-04-30 17:54:19 +00:00
createIndex, err := p.cli.CreateIndex(index).Body(typemaping).Do(context.Background())
2020-03-25 04:49:26 +00:00
if err != nil {
log.Print(err)
2020-03-25 04:49:26 +00:00
return err
}
if !createIndex.Acknowledged {
return errors.New("create index error")
2020-03-25 04:49:26 +00:00
// Not acknowledged
}
2020-04-30 17:54:19 +00:00
return nil
2020-03-25 04:49:26 +00:00
}
return errors.New(ERROR_PTR)
}
func (p *ElkEngine)IndexExisted(index string) (bool,error ){
if nil != p {
exists, err := p.cli.IndexExists(index).Do(context.Background())
if exists{
return true,nil
}
if err != nil {
// Handle error
log.Print(err)
return false,err
}
return false,nil
}
return false,nil
2020-03-25 04:49:26 +00:00
}