background/db/elasticEngine.go

156 lines
3.5 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"
2020-03-13 09:40:00 +00:00
"gopkg.in/olivere/elastic.v3"
"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(
ERROR_PTR = "nullpointer error"
INPUT_TYPE_ERROR = "wrong input parameter"
CREATED_ERROR = "create error"
DELETE_ERROR = "delete error"
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).
Type(types).
BodyJson(data).
Do()
if !resp.Created{
return errors.New(CREATED_ERROR)
}
if err != nil {
log.Print(err)
2020-03-20 17:50:04 +00:00
return err
}
}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
}
2020-03-20 17:50:04 +00:00
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 {
2020-03-21 03:17:24 +00:00
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
if !res.Found{
return errors.New(DELETE_ERROR)
}
}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,
types string,query elastic.Query,data interface{},
limit int,offset int) ([]interface{},error) {
2020-03-21 03:17:24 +00:00
if nil != p{
res, err := p.cli.
Search(index).
Type(types).
Query(query).Size(limit).From(limit*offset).Do()
2020-03-21 03:17:24 +00:00
if err != nil {
print(err)
return nil,err
}
//var typ Employee
typ := reflect.TypeOf(data)
return res.Each(typ),nil
}else{
return nil,errors.New(ERROR_PTR)
}
}
func (p *ElkEngine)Update(index string,types string,id string,data map[string]interface{}) error {
if nil != p {
_, err := p.cli.Update().
Index(index).
Type(types).
Id(id).
Doc(data).
Do()
if err != nil {
println(err.Error())
return err
}
}
return errors.New(ERROR_PTR)
}
2020-03-25 04:49:26 +00:00
// 创建 elasticSearch 的 Mapping
func (p *ElkEngine)InitMapping(esIndexName string, esTypeName string, typeMapping string) error{
var err error
exists, err := p.cli.IndexExists(esIndexName).Do()
if err != nil {
log.Println("IndexExists" + err.Error())
return err
}
//log.Println("es index: " + esIndexName)
//log.Println("es type: " + esTypeName)
//log.Println("es index mapping: " + indexMapping)
//log.Println("es type mapping: " + typeMapping)
if !exists {
log.Println("es index not exists: " + esIndexName)
// Create a new index.
2020-03-25 04:52:46 +00:00
createIndex, err := p.cli.CreateIndex(esIndexName).Body(typeMapping).Do()
2020-03-25 04:49:26 +00:00
if err != nil {
log.Println("CreateIndex" + err.Error())
return err
}
if !createIndex.Acknowledged {
// Not acknowledged
return errors.New("create index:" + esIndexName + ", not Ack nowledged")
}
}
/**
* type
exists, err = client.TypeExists().Index(esIndexName).Type(esTypeName).Do(ctx)
if err != nil {
return err
}
if !exists {
}
*/
// PutMapping() *IndicesPutMappingService
putresp, err := p.cli.PutMapping().Index(esIndexName).Type(esTypeName).BodyString(typeMapping).Do()
// 新建 mapping
//indicesCreateResult, err := elastic.NewIndicesCreateService(client).Index(esIndexName).BodyString(mapping).Do(ctx)
if err != nil {
log.Println("NewIndicesCreateService" + err.Error())
return err
}
if !putresp.Acknowledged {
// Not acknowledged
return errors.New("create mapping fail, esIndexName:" + esIndexName + ", esTypeName:" + esTypeName + ", not Ack nowledged")
}
return err
}