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
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)

View File

@ -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)
}

View File

@ -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

View File

@ -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())
}

View File

@ -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
}

View File

@ -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)
}