修复elasticsearch根据id删除文档,改用querydelete的形式,测试ok

master
a7458969 2020-04-26 14:11:36 +08:00
parent 69f4f91445
commit 3909f2052c
3 changed files with 12 additions and 21 deletions

View File

@ -43,6 +43,7 @@ func DeleteHardWare(c *gin.Context) {
e := model.DeleteHardware(name) e := model.DeleteHardware(name)
if nil != e{ if nil != e{
logs.Error(e.Error()) logs.Error(e.Error())
return
} }
resp.Msg = "OK" resp.Msg = "OK"
resp.Status = 0 resp.Status = 0

View File

@ -43,18 +43,15 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{})
return nil return nil
} }
func (p *ElkEngine)Delete(index string,types string,id string) error{ func (p *ElkEngine)Delete(query elastic.Query,index string) error{
if nil != p{ if nil != p{
res, err := p.cli.Delete().Index(index). _, err := p.cli.DeleteByQuery().Index(index).Query(query).
Id(id).
Do(context.Background()) Do(context.Background())
if err != nil { if err != nil {
print(err) log.Print(err)
return err return err
} }
if !res.ForcedRefresh{
return errors.New(DELETE_ERROR)
}
}else{ }else{
return errors.New(ERROR_PTR) return errors.New(ERROR_PTR)
} }

View File

@ -50,7 +50,7 @@ func (this *Hardware )CreateHardware( ) error{
return errors.New(ERR_COLUMN_EXISTED) return errors.New(ERR_COLUMN_EXISTED)
} }
} }
e = db.GetElastic().Create("hardware","0","",*this) e = db.GetElastic().Create("hardware_data","0","",*this)
if nil != e{ if nil != e{
log.Print("shit1") log.Print("shit1")
log.Print(e.Error()) log.Print(e.Error())
@ -62,7 +62,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,ids,e := db.GetElastic().Query("hardware",nil,Hardware{},limit,size) data,ids,e := db.GetElastic().Query("hardware_data",nil,Hardware{},limit,size)
if nil != e{ if nil != e{
return nil,e return nil,e
} }
@ -77,7 +77,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,ids,e := db.GetElastic().Query("hardware",query,Hardware{},limit,offset) data,ids,e := db.GetElastic().Query("hardware_data",query,Hardware{},limit,offset)
log.Print(data) log.Print(data)
if nil != e{ if nil != e{
return nil,e return nil,e
@ -92,17 +92,10 @@ func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error)
} }
func DeleteHardware(name string) error{ func DeleteHardware(name string) error{
matchPhraseQuery := elastic.NewMatchQuery("name", "stm32开发板") query := elastic.NewTermQuery("name",name)
existedHardware,e := QueryHardwares(matchPhraseQuery,10,0) err := db.GetElastic().Delete(query,"hardware_data")
log.Print(e,existedHardware) if err != nil{
return err
for _,v := range existedHardware{
if v.Name == name{
err := db.GetElastic().Delete("hardware","",v.ID)
if err != nil {
return err
}
}
} }
return nil return nil
} }