From 02aa809976bb3c8283561940c89df8fcc44a44c4 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Tue, 31 Mar 2020 15:19:57 +0800 Subject: [PATCH] =?UTF-8?q?elasticsearch=E6=8E=A5=E5=8F=A3=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=88=B07.0=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/file.go | 3 +- db/elasticEngine.go | 84 ++++++++++++++------------------------------- db/sqlManager.go | 3 +- main.go | 2 +- model/hardware.go | 6 ++-- utils/base.go | 14 +++++++- 6 files changed, 46 insertions(+), 66 deletions(-) diff --git a/controller/file.go b/controller/file.go index 46c72fc..dae8c26 100644 --- a/controller/file.go +++ b/controller/file.go @@ -1,6 +1,7 @@ package controller import ( + "background/utils" "github.com/disintegration/imaging" "github.com/gin-gonic/gin" uuid "github.com/satori/go.uuid" @@ -142,7 +143,7 @@ func (this *FileController) OnDownLoad(c *gin.Context) { return } - file,e := os.Open("/home/ubuntu/api/bin/image/" +fileName) + file,e := os.Open(utils.GetCurrentDirectory() + "/image/" +fileName) if nil != e{ log.Print(e.Error()) c.JSON(200,resp) diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 70fe3c8..8fc87c5 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -1,9 +1,9 @@ package db import ( - "github.com/pkg/errors" - "gopkg.in/olivere/elastic.v3" + "golang.org/x/net/context" + "gopkg.in/olivere/elastic.v7" "qiniupkg.com/x/log.v7" "reflect" ) @@ -12,6 +12,7 @@ const( INPUT_TYPE_ERROR = "wrong input parameter" CREATED_ERROR = "create error" DELETE_ERROR = "delete error" + INDEX_EXISTED = "index existed" ) type ElkEngine struct { @@ -27,14 +28,12 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{}) Index(index). Type(types). BodyJson(data). - Do() - if !resp.Created{ - return errors.New(CREATED_ERROR) - } + Do(context.Background()) if err != nil { log.Print("create error",err) return err } + log.Print(resp) }else{ log.Print(reflect.TypeOf(data).Kind()) 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). Type(types). Id(id). - Do() + Do(context.Background()) if err != nil { print(err) return err } - if !res.Found{ + if !res.ForcedRefresh{ return errors.New(DELETE_ERROR) } }else{ @@ -66,15 +65,13 @@ func (p *ElkEngine)Delete(index string,types string,id string) error{ /* */ -func (p *ElkEngine)Query(index string, - types string,query elastic.Query,data interface{}, +func (p *ElkEngine)Query(index string,query elastic.Query,data interface{}, limit int,offset int) ([]interface{},error) { if nil != p{ if(limit == 0){ res, err := p.cli. Search(index). - Type(types). - Query(query).Do() + Query(query).Do(context.Background()) if err != nil { print(err) return nil,err @@ -85,8 +82,7 @@ func (p *ElkEngine)Query(index string, }else{ res, err := p.cli. Search(index). - Type(types). - Query(query).Size(limit).From(limit*offset).Do() + Query(query).Size(limit).From(limit*offset).Do(context.Background()) if err != nil { print(err) return nil,err @@ -109,7 +105,7 @@ func (p *ElkEngine)Update(index string,types string,id string,data map[string]in Type(types). Id(id). Doc(data). - Do() + Do(context.Background()) if err != nil { println(err.Error()) 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) } - -// 创建 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. - createIndex, err := p.cli.CreateIndex(esIndexName).Body(typeMapping).Do() +func (p *ElkEngine)CreateIndex(index string,typemaping string) error{ + if nil != p { + exists, err := p.cli.IndexExists("elastic_index").Do(context.Background()) 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 } if !createIndex.Acknowledged { + return errors.New("create index error") // 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 + return errors.New(ERROR_PTR) } \ No newline at end of file diff --git a/db/sqlManager.go b/db/sqlManager.go index 8184528..3746281 100644 --- a/db/sqlManager.go +++ b/db/sqlManager.go @@ -10,11 +10,10 @@ import ( "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql" "gopkg.in/mgo.v2" + "gopkg.in/olivere/elastic.v7" "io/ioutil" "log" "os" - "gopkg.in/olivere/elastic.v3" - ) var gEla ElkEngine var gDb Database diff --git a/main.go b/main.go index 1c90c88..8b8bbb5 100644 --- a/main.go +++ b/main.go @@ -41,7 +41,7 @@ func InitRedis() { } } func InitElasticSearch(){ - e := db.GetElastic().InitMapping("hardware","0",model.HardwareTypeMapping()) + e := db.GetElastic().CreateIndex("hardware",model.HardwareTypeMapping()) if nil != e{ log.Print(e.Error()) } diff --git a/model/hardware.go b/model/hardware.go index eb7597f..4157537 100644 --- a/model/hardware.go +++ b/model/hardware.go @@ -16,7 +16,7 @@ func HardwareTypeMapping() (string){ "hardware":{ "properties":{ "id":{"type":"keyword"}, - "name":{"type":"text","index":"not_analyzed"}, + "name":{"type":"keyword"}, "desc":{"type":"text"}, "pic":{"type":"doc"}, "doc":{"type":"doc"} @@ -58,7 +58,7 @@ func (this *Hardware )CreateHardware( ) error{ func GetHardwares(limit int,size int) ([]Hardware,error){ 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{ 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){ 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{ return nil,e } diff --git a/utils/base.go b/utils/base.go index 99b2ac3..843512a 100644 --- a/utils/base.go +++ b/utils/base.go @@ -1,6 +1,11 @@ package utils -import "fmt" +import ( + "fmt" + "os" + "path/filepath" + "strings" +) func ByteSliceToString(b []byte) string { var ret string @@ -11,3 +16,10 @@ func ByteSliceToString(b []byte) string { } return ret } +func GetCurrentDirectory() string { + dir, err := filepath.Abs(filepath.Dir(os.Args[0])) + if err != nil { + return "./" + } + return strings.Replace(dir, "\\", "/", -1) +}