2020-03-13 09:40:00 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2021-02-05 17:46:59 +00:00
|
|
|
"background/utils"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
|
2020-03-20 17:50:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-03-31 07:19:57 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"gopkg.in/olivere/elastic.v7"
|
2020-03-25 03:26:32 +00:00
|
|
|
"qiniupkg.com/x/log.v7"
|
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"
|
2020-03-31 07:19:57 +00:00
|
|
|
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{
|
2020-03-25 03:26:32 +00:00
|
|
|
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).
|
2020-03-31 07:19:57 +00:00
|
|
|
Do(context.Background())
|
2020-03-20 17:50:04 +00:00
|
|
|
if err != nil {
|
2020-03-29 18:10:18 +00:00
|
|
|
log.Print("create error",err)
|
2020-03-20 17:50:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-31 07:19:57 +00:00
|
|
|
log.Print(resp)
|
2020-03-20 17:50:04 +00:00
|
|
|
}else{
|
2020-03-25 03:26:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:11:36 +00:00
|
|
|
func (p *ElkEngine)Delete(query elastic.Query,index string) error{
|
2020-03-20 17:50:04 +00:00
|
|
|
if nil != p{
|
2020-04-26 06:11:36 +00:00
|
|
|
_, err := p.cli.DeleteByQuery().Index(index).Query(query).
|
2020-03-31 07:19:57 +00:00
|
|
|
Do(context.Background())
|
2020-03-20 17:50:04 +00:00
|
|
|
if err != nil {
|
2020-04-26 06:11:36 +00:00
|
|
|
log.Print(err)
|
2020-03-20 17:50:04 +00:00
|
|
|
return err
|
2020-03-21 03:17:24 +00:00
|
|
|
}
|
2020-04-26 06:11:36 +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
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
2021-02-05 17:46:59 +00:00
|
|
|
func (p *ElkEngine)Query(index string,query elastic.Query,typ reflect.Type,
|
2020-03-31 17:07:32 +00:00
|
|
|
limit int,offset int) ([]interface{},[]string,error) {
|
2021-02-05 17:46:59 +00:00
|
|
|
rets := []interface{}{}
|
2020-03-21 03:17:24 +00:00
|
|
|
if nil != p{
|
2020-03-26 05:17:20 +00:00
|
|
|
if(limit == 0){
|
2020-03-31 17:07:32 +00:00
|
|
|
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
2020-03-26 05:17:20 +00:00
|
|
|
if err != nil {
|
|
|
|
print(err)
|
2020-03-31 17:07:32 +00:00
|
|
|
return nil,nil,err
|
2020-03-26 05:17:20 +00:00
|
|
|
}
|
2021-02-05 17:46:59 +00:00
|
|
|
|
2020-03-31 17:07:32 +00:00
|
|
|
id := []string{}
|
|
|
|
for _,vs := range res.Hits.Hits{
|
|
|
|
id = append(id,vs.Id)
|
|
|
|
}
|
|
|
|
return rets,id,nil
|
2020-03-26 05:17:20 +00:00
|
|
|
}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())
|
2020-03-26 05:17:20 +00:00
|
|
|
if err != nil {
|
|
|
|
print(err)
|
2020-03-31 17:07:32 +00:00
|
|
|
return nil,nil,err
|
2020-03-26 05:17:20 +00:00
|
|
|
}
|
2020-03-31 17:07:32 +00:00
|
|
|
id := []string{}
|
|
|
|
for _,vs := range res.Hits.Hits{
|
|
|
|
id = append(id,vs.Id)
|
2021-02-05 17:46:59 +00:00
|
|
|
data,e := vs.Source.MarshalJSON()
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
}
|
|
|
|
obj := utils.ReflectMakeNew(typ)
|
|
|
|
mapobj := map[string]interface{}{}
|
|
|
|
e = json.Unmarshal(data,&mapobj)
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
}
|
|
|
|
// for k,_ := range mapobj{
|
|
|
|
// value := reflect.ValueOf(obj)
|
|
|
|
// }
|
|
|
|
log.Print(obj)
|
|
|
|
rets = append(rets,obj)
|
|
|
|
log.Print(string(data))
|
2020-03-31 17:07:32 +00:00
|
|
|
}
|
|
|
|
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).
|
2020-03-31 07:19:57 +00:00
|
|
|
Do(context.Background())
|
2020-03-21 03:17:24 +00:00
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.New(ERROR_PTR)
|
|
|
|
}
|
2020-03-31 07:19:57 +00:00
|
|
|
func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
|
|
|
|
if nil != p {
|
2020-04-24 14:56:24 +00:00
|
|
|
exists, err := p.cli.IndexExists(index).Do(context.Background())
|
2020-03-31 07:19:57 +00:00
|
|
|
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 {
|
2020-03-31 07:19:57 +00:00
|
|
|
log.Print(err)
|
2020-03-25 04:49:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !createIndex.Acknowledged {
|
2020-03-31 07:19:57 +00:00
|
|
|
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
|
|
|
}
|
2020-03-31 07:19:57 +00:00
|
|
|
return errors.New(ERROR_PTR)
|
2020-04-24 14:56:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|