添加elasticsearch精确查找的接口代码

master
a7458969 2020-03-30 02:10:18 +08:00
parent fc4176568e
commit 991efd5924
6 changed files with 32 additions and 14 deletions

View File

@ -4,6 +4,10 @@
<list default="true" id="7cf7b6b3-0082-44ef-bb0f-bfcc57e19eb1" name="Default Changelist" comment=""> <list default="true" id="7cf7b6b3-0082-44ef-bb0f-bfcc57e19eb1" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/controller/file.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/file.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/controller/file.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/file.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/controller/hardware.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/hardware.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/db/elasticEngine.go" beforeDir="false" afterPath="$PROJECT_DIR$/db/elasticEngine.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/model/hardware.go" beforeDir="false" afterPath="$PROJECT_DIR$/model/hardware.go" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -62,7 +66,7 @@
</option> </option>
</component> </component>
<component name="RunManager"> <component name="RunManager">
<configuration name="background" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true"> <configuration name="background" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="background" /> <module name="background" />
<working_directory value="$PROJECT_DIR$/" /> <working_directory value="$PROJECT_DIR$/" />
<envs> <envs>
@ -74,11 +78,6 @@
<output_directory value="$PROJECT_DIR$/" /> <output_directory value="$PROJECT_DIR$/" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<recent_temporary>
<list>
<item itemvalue="Go Build.background" />
</list>
</recent_temporary>
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" /> <option name="version" value="1" />

View File

@ -23,7 +23,7 @@ func (this *FileController) OnUpload(c *gin.Context) {
return return
} }
file, _, err := c.Request.FormFile("image") file, _, err := c.Request.FormFile("file")
if nil != err || nil == file{ if nil != err || nil == file{
log.Print(err.Error()) log.Print(err.Error())
return return

View File

@ -18,8 +18,9 @@ func AddHardware(c *gin.Context) {
print(e) print(e)
return return
} }
if nil != hardware.CreateHardware(){ e = hardware.CreateHardware()
print(e) if nil != e{
log.Print(e)
return return
} }
resp.Data = nil resp.Data = nil

View File

@ -32,7 +32,7 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{})
return errors.New(CREATED_ERROR) return errors.New(CREATED_ERROR)
} }
if err != nil { if err != nil {
log.Print(err) log.Print("create error",err)
return err return err
} }
}else{ }else{
@ -91,6 +91,7 @@ func (p *ElkEngine)Query(index string,
print(err) print(err)
return nil,err return nil,err
} }
log.Print(res)
//var typ Employee //var typ Employee
typ := reflect.TypeOf(data) typ := reflect.TypeOf(data)
return res.Each(typ),nil return res.Each(typ),nil

View File

@ -123,7 +123,7 @@ func main() {
api.DELETE("/article/:id",controller.DeleteArticle) ////删除文章 api.DELETE("/article/:id",controller.DeleteArticle) ////删除文章
api.POST("/image_upload",fileController.OnUpload) // 上传图片 api.POST("/image_upload",fileController.OnUpload) // 上传图片
api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片 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) // 获取所有的文章类型 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型

View File

@ -4,6 +4,7 @@ import (
"background/db" "background/db"
"background/utils" "background/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"gopkg.in/olivere/elastic.v3"
"qiniupkg.com/x/log.v7" "qiniupkg.com/x/log.v7"
) )
@ -15,7 +16,7 @@ func HardwareTypeMapping() (string){
"hardware":{ "hardware":{
"properties":{ "properties":{
"id":{"type":"keyword"}, "id":{"type":"keyword"},
"name":{"type":"text"}, "name":{"type":"text","index":"not_analyzed"},
"desc":{"type":"text"}, "desc":{"type":"text"},
"pic":{"type":"doc"}, "pic":{"type":"doc"},
"doc":{"type":"doc"} "doc":{"type":"doc"}
@ -38,15 +39,20 @@ func (this *Hardware )CreateHardware( ) error{
if nil == this{ if nil == this{
return errors.New(utils.ERRNULLPOINTER) return errors.New(utils.ERRNULLPOINTER)
} }
name,e := GetHardwares(1,1) log.Print(this.Name)
if len(name) != 0{ 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) return errors.New(ERR_COLUMN_EXISTED)
} }
e = db.GetElastic().Create("hardware","0","",*this) e = db.GetElastic().Create("hardware","0","",*this)
if nil != e{ if nil != e{
log.Print("shit1")
log.Print(e.Error()) log.Print(e.Error())
return e return e
} }
log.Print("shit2")
return nil; return nil;
} }
@ -62,3 +68,14 @@ func GetHardwares(limit int,size int) ([]Hardware,error){
return ret,nil 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
}