添加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="">
<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/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>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -62,7 +66,7 @@
</option>
</component>
<component name="RunManager">
<configuration name="background" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true">
<configuration name="background" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="background" />
<working_directory value="$PROJECT_DIR$/" />
<envs>
@ -74,11 +78,6 @@
<output_directory value="$PROJECT_DIR$/" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Go Build.background" />
</list>
</recent_temporary>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />

View File

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

View File

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

View File

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

View File

@ -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) // 获取所有的文章类型

View File

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