elasticsearch接口更新到7.0的版本

master
a7458969 2020-03-31 15:19:57 +08:00
parent 991efd5924
commit 02aa809976
6 changed files with 46 additions and 66 deletions

View File

@ -1,6 +1,7 @@
package controller package controller
import ( import (
"background/utils"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
@ -142,7 +143,7 @@ func (this *FileController) OnDownLoad(c *gin.Context) {
return return
} }
file,e := os.Open("/home/ubuntu/api/bin/image/" +fileName) file,e := os.Open(utils.GetCurrentDirectory() + "/image/" +fileName)
if nil != e{ if nil != e{
log.Print(e.Error()) log.Print(e.Error())
c.JSON(200,resp) c.JSON(200,resp)

View File

@ -1,9 +1,9 @@
package db package db
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"gopkg.in/olivere/elastic.v3" "golang.org/x/net/context"
"gopkg.in/olivere/elastic.v7"
"qiniupkg.com/x/log.v7" "qiniupkg.com/x/log.v7"
"reflect" "reflect"
) )
@ -12,6 +12,7 @@ const(
INPUT_TYPE_ERROR = "wrong input parameter" INPUT_TYPE_ERROR = "wrong input parameter"
CREATED_ERROR = "create error" CREATED_ERROR = "create error"
DELETE_ERROR = "delete error" DELETE_ERROR = "delete error"
INDEX_EXISTED = "index existed"
) )
type ElkEngine struct { type ElkEngine struct {
@ -27,14 +28,12 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{})
Index(index). Index(index).
Type(types). Type(types).
BodyJson(data). BodyJson(data).
Do() Do(context.Background())
if !resp.Created{
return errors.New(CREATED_ERROR)
}
if err != nil { if err != nil {
log.Print("create error",err) log.Print("create error",err)
return err return err
} }
log.Print(resp)
}else{ }else{
log.Print(reflect.TypeOf(data).Kind()) log.Print(reflect.TypeOf(data).Kind())
return errors.New(INPUT_TYPE_ERROR) return errors.New(INPUT_TYPE_ERROR)
@ -50,12 +49,12 @@ func (p *ElkEngine)Delete(index string,types string,id string) error{
res, err := p.cli.Delete().Index(index). res, err := p.cli.Delete().Index(index).
Type(types). Type(types).
Id(id). Id(id).
Do() Do(context.Background())
if err != nil { if err != nil {
print(err) print(err)
return err return err
} }
if !res.Found{ if !res.ForcedRefresh{
return errors.New(DELETE_ERROR) return errors.New(DELETE_ERROR)
} }
}else{ }else{
@ -66,15 +65,13 @@ func (p *ElkEngine)Delete(index string,types string,id string) error{
/* /*
*/ */
func (p *ElkEngine)Query(index string, func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
types string,query elastic.Query,data interface{},
limit int,offset int) ([]interface{},error) { limit int,offset int) ([]interface{},error) {
if nil != p{ if nil != p{
if(limit == 0){ if(limit == 0){
res, err := p.cli. res, err := p.cli.
Search(index). Search(index).
Type(types). Query(query).Do(context.Background())
Query(query).Do()
if err != nil { if err != nil {
print(err) print(err)
return nil,err return nil,err
@ -85,8 +82,7 @@ func (p *ElkEngine)Query(index string,
}else{ }else{
res, err := p.cli. res, err := p.cli.
Search(index). Search(index).
Type(types). Query(query).Size(limit).From(limit*offset).Do(context.Background())
Query(query).Size(limit).From(limit*offset).Do()
if err != nil { if err != nil {
print(err) print(err)
return nil,err return nil,err
@ -109,7 +105,7 @@ func (p *ElkEngine)Update(index string,types string,id string,data map[string]in
Type(types). Type(types).
Id(id). Id(id).
Doc(data). Doc(data).
Do() Do(context.Background())
if err != nil { if err != nil {
println(err.Error()) println(err.Error())
return err return err
@ -117,54 +113,26 @@ func (p *ElkEngine)Update(index string,types string,id string,data map[string]in
} }
return errors.New(ERROR_PTR) return errors.New(ERROR_PTR)
} }
func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
// 创建 elasticSearch 的 Mapping if nil != p {
func (p *ElkEngine)InitMapping(esIndexName string, esTypeName string, typeMapping string) error{ exists, err := p.cli.IndexExists("elastic_index").Do(context.Background())
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.
createIndex, err := p.cli.CreateIndex(esIndexName).Body(typeMapping).Do()
if err != nil { if err != nil {
log.Println("CreateIndex" + err.Error()) // Handle error
log.Print(err)
return err
}
if exists{
return errors.New(INDEX_EXISTED)
}
createIndex, err := p.cli.CreateIndex(index).Do(context.Background())
if err != nil {
log.Print(err)
return err return err
} }
if !createIndex.Acknowledged { if !createIndex.Acknowledged {
return errors.New("create index error")
// Not acknowledged // Not acknowledged
return errors.New("create index:" + esIndexName + ", not Ack nowledged")
} }
} }
/** return errors.New(ERROR_PTR)
* 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
} }

View File

@ -10,11 +10,10 @@ import (
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/olivere/elastic.v7"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"gopkg.in/olivere/elastic.v3"
) )
var gEla ElkEngine var gEla ElkEngine
var gDb Database var gDb Database

View File

@ -41,7 +41,7 @@ func InitRedis() {
} }
} }
func InitElasticSearch(){ func InitElasticSearch(){
e := db.GetElastic().InitMapping("hardware","0",model.HardwareTypeMapping()) e := db.GetElastic().CreateIndex("hardware",model.HardwareTypeMapping())
if nil != e{ if nil != e{
log.Print(e.Error()) log.Print(e.Error())
} }

View File

@ -16,7 +16,7 @@ func HardwareTypeMapping() (string){
"hardware":{ "hardware":{
"properties":{ "properties":{
"id":{"type":"keyword"}, "id":{"type":"keyword"},
"name":{"type":"text","index":"not_analyzed"}, "name":{"type":"keyword"},
"desc":{"type":"text"}, "desc":{"type":"text"},
"pic":{"type":"doc"}, "pic":{"type":"doc"},
"doc":{"type":"doc"} "doc":{"type":"doc"}
@ -58,7 +58,7 @@ func (this *Hardware )CreateHardware( ) error{
func GetHardwares(limit int,size int) ([]Hardware,error){ func GetHardwares(limit int,size int) ([]Hardware,error){
var ret []Hardware var ret []Hardware
data,e := db.GetElastic().Query("hardware","0",nil,Hardware{},limit,size) data,e := db.GetElastic().Query("hardware",nil,Hardware{},limit,size)
if nil != e{ if nil != e{
return nil,e return nil,e
} }
@ -70,7 +70,7 @@ func GetHardwares(limit int,size int) ([]Hardware,error){
func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){ func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){
var ret []Hardware var ret []Hardware
data,e := db.GetElastic().Query("hardware","0",query,Hardware{},limit,offset) data,e := db.GetElastic().Query("hardware",query,Hardware{},limit,offset)
if nil != e{ if nil != e{
return nil,e return nil,e
} }

View File

@ -1,6 +1,11 @@
package utils package utils
import "fmt" import (
"fmt"
"os"
"path/filepath"
"strings"
)
func ByteSliceToString(b []byte) string { func ByteSliceToString(b []byte) string {
var ret string var ret string
@ -11,3 +16,10 @@ func ByteSliceToString(b []byte) string {
} }
return ret return ret
} }
func GetCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return "./"
}
return strings.Replace(dir, "\\", "/", -1)
}