From 991efd59243a8e40a3f9f6d04daa62f54c73f3df Mon Sep 17 00:00:00 2001
From: a7458969 <290198252@qq.com>
Date: Mon, 30 Mar 2020 02:10:18 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0elasticsearch=E7=B2=BE?=
=?UTF-8?q?=E7=A1=AE=E6=9F=A5=E6=89=BE=E7=9A=84=E6=8E=A5=E5=8F=A3=E4=BB=A3?=
=?UTF-8?q?=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/workspace.xml | 11 +++++------
controller/file.go | 2 +-
controller/hardware.go | 5 +++--
db/elasticEngine.go | 3 ++-
main.go | 2 +-
model/hardware.go | 23 ++++++++++++++++++++---
6 files changed, 32 insertions(+), 14 deletions(-)
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 7396d8d..0de3cc4 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,6 +4,10 @@
+
+
+
+
@@ -62,7 +66,7 @@
-
+
@@ -74,11 +78,6 @@
-
-
-
-
-
diff --git a/controller/file.go b/controller/file.go
index 9ca10b3..46c72fc 100644
--- a/controller/file.go
+++ b/controller/file.go
@@ -23,7 +23,7 @@ func (this *FileController) OnUpload(c *gin.Context) {
return
}
- file, _, err := c.Request.FormFile("image")
+ file, _, err := c.Request.FormFile("file")
if nil != err || nil == file{
log.Print(err.Error())
return
diff --git a/controller/hardware.go b/controller/hardware.go
index 89c2f85..18631de 100644
--- a/controller/hardware.go
+++ b/controller/hardware.go
@@ -18,8 +18,9 @@ func AddHardware(c *gin.Context) {
print(e)
return
}
- if nil != hardware.CreateHardware(){
- print(e)
+ e = hardware.CreateHardware()
+ if nil != e{
+ log.Print(e)
return
}
resp.Data = nil
diff --git a/db/elasticEngine.go b/db/elasticEngine.go
index d6a336d..70fe3c8 100644
--- a/db/elasticEngine.go
+++ b/db/elasticEngine.go
@@ -32,7 +32,7 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{})
return errors.New(CREATED_ERROR)
}
if err != nil {
- log.Print(err)
+ log.Print("create error",err)
return err
}
}else{
@@ -91,6 +91,7 @@ func (p *ElkEngine)Query(index string,
print(err)
return nil,err
}
+ log.Print(res)
//var typ Employee
typ := reflect.TypeOf(data)
return res.Each(typ),nil
diff --git a/main.go b/main.go
index 59d29d8..1c90c88 100644
--- a/main.go
+++ b/main.go
@@ -123,7 +123,7 @@ func main() {
api.DELETE("/article/:id",controller.DeleteArticle) ////删除文章
api.POST("/image_upload",fileController.OnUpload) // 上传图片
api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片
- api.GET("/image_thumbnail/:file",fileController.OnDownLoad) // 下载图片
+ api.GET("/image_thumbnail/:file",fileController.OnThumbnail) // 下载图片
api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型
diff --git a/model/hardware.go b/model/hardware.go
index bdb4497..eb7597f 100644
--- a/model/hardware.go
+++ b/model/hardware.go
@@ -4,6 +4,7 @@ import (
"background/db"
"background/utils"
"github.com/pkg/errors"
+ "gopkg.in/olivere/elastic.v3"
"qiniupkg.com/x/log.v7"
)
@@ -15,7 +16,7 @@ func HardwareTypeMapping() (string){
"hardware":{
"properties":{
"id":{"type":"keyword"},
- "name":{"type":"text"},
+ "name":{"type":"text","index":"not_analyzed"},
"desc":{"type":"text"},
"pic":{"type":"doc"},
"doc":{"type":"doc"}
@@ -38,15 +39,20 @@ func (this *Hardware )CreateHardware( ) error{
if nil == this{
return errors.New(utils.ERRNULLPOINTER)
}
- name,e := GetHardwares(1,1)
- if len(name) != 0{
+ log.Print(this.Name)
+ matchPhraseQuery := elastic.NewMatchQuery("name", this.Name)
+ existedHardware,e := QueryHardwares(matchPhraseQuery,10,1)
+ log.Print(e,existedHardware)
+ if len(existedHardware) > 0{
return errors.New(ERR_COLUMN_EXISTED)
}
e = db.GetElastic().Create("hardware","0","",*this)
if nil != e{
+ log.Print("shit1")
log.Print(e.Error())
return e
}
+ log.Print("shit2")
return nil;
}
@@ -62,3 +68,14 @@ func GetHardwares(limit int,size int) ([]Hardware,error){
return ret,nil
}
+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)
+ if nil != e{
+ return nil,e
+ }
+ for _,v := range data{
+ ret = append(ret,v.(Hardware))
+ }
+ return ret,nil
+}
\ No newline at end of file