es 驱动
parent
43e0a6c865
commit
b1564b91ff
|
@ -52,6 +52,29 @@ func DeleteHardWare(c *gin.Context) {
|
|||
resp.Data = nil
|
||||
}
|
||||
|
||||
func SearchDoc(c *gin.Context){
|
||||
type ReqSearch struct{
|
||||
Content string `json:"content"`
|
||||
}
|
||||
resp := RespBase{"unkown error", -231, nil}
|
||||
defer func() {
|
||||
c.JSON(200, resp)
|
||||
}()
|
||||
req := ReqSearch{}
|
||||
e := c.BindJSON(&req)
|
||||
if nil != e {
|
||||
log.Print(e)
|
||||
print(e)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
resp.Data = nil
|
||||
resp.Msg = "OK"
|
||||
resp.Status = 0
|
||||
|
||||
}
|
||||
|
||||
func UpdateHardWare(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"background/utils"
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/context"
|
||||
"gopkg.in/olivere/elastic.v7"
|
||||
"qiniupkg.com/x/log.v7"
|
||||
"reflect"
|
||||
)
|
||||
const(
|
||||
ERROR_PTR = "null pointer error"
|
||||
|
@ -60,8 +63,9 @@ func (p *ElkEngine)Delete(query elastic.Query,index string) error{
|
|||
/*
|
||||
|
||||
*/
|
||||
func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
|
||||
func (p *ElkEngine)Query(index string,query elastic.Query,typ reflect.Type,
|
||||
limit int,offset int) ([]interface{},[]string,error) {
|
||||
rets := []interface{}{}
|
||||
if nil != p{
|
||||
if(limit == 0){
|
||||
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
||||
|
@ -69,9 +73,7 @@ func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
|
|||
print(err)
|
||||
return nil,nil,err
|
||||
}
|
||||
//var typ Employee
|
||||
typ := reflect.TypeOf(data)
|
||||
rets := res.Each(typ)
|
||||
|
||||
id := []string{}
|
||||
for _,vs := range res.Hits.Hits{
|
||||
id = append(id,vs.Id)
|
||||
|
@ -83,13 +85,25 @@ func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
|
|||
print(err)
|
||||
return nil,nil,err
|
||||
}
|
||||
|
||||
//var typ Employee
|
||||
typ := reflect.TypeOf(data)
|
||||
rets := res.Each(typ)
|
||||
id := []string{}
|
||||
for _,vs := range res.Hits.Hits{
|
||||
id = append(id,vs.Id)
|
||||
data,e := vs.Source.MarshalJSON()
|
||||
if nil != e{
|
||||
log.Print(e.Error())
|
||||
}
|
||||
obj := utils.ReflectMakeNew(typ)
|
||||
mapobj := map[string]interface{}{}
|
||||
e = json.Unmarshal(data,&mapobj)
|
||||
if nil != e{
|
||||
log.Print(e.Error())
|
||||
}
|
||||
// for k,_ := range mapobj{
|
||||
// value := reflect.ValueOf(obj)
|
||||
// }
|
||||
log.Print(obj)
|
||||
rets = append(rets,obj)
|
||||
log.Print(string(data))
|
||||
}
|
||||
return rets,id,nil
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
|
||||
// This file was generated by swaggo/swag at
|
||||
// 2020-06-24 21:33:14.5754792 +0800 CST m=+0.035031801
|
||||
// 2021-01-27 22:40:54.1593671 +0800 CST m=+0.052880501
|
||||
|
||||
package docs
|
||||
|
||||
|
|
2
main.go
2
main.go
|
@ -149,7 +149,7 @@ func main() {
|
|||
api.POST("/hardware", controller.AddHardware) // 新增硬件
|
||||
api.GET("/hardware", controller.ReadHardWare) // 读取硬件
|
||||
api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件
|
||||
|
||||
api.POST("/doc_search",)
|
||||
api.PUT("/file", fileController.OnFileUploadFile) // 上传文件
|
||||
api.GET("/file", fileController.DownloadFile) // 下载 文件
|
||||
api.GET("/filelist", fileController.FileList) // 文件列表
|
||||
|
|
|
@ -2,9 +2,9 @@ package model
|
|||
|
||||
import (
|
||||
"background/db"
|
||||
"background/logs"
|
||||
"fmt"
|
||||
"log"
|
||||
"background/logs"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
|
|
|
@ -3,6 +3,8 @@ package model
|
|||
import (
|
||||
"background/db"
|
||||
"background/utils"
|
||||
"reflect"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/olivere/elastic.v3"
|
||||
"qiniupkg.com/x/log.v7"
|
||||
|
@ -11,6 +13,7 @@ import (
|
|||
const (
|
||||
ERR_COLUMN_EXISTED = "column_existed"
|
||||
)
|
||||
|
||||
func HardwareTypeMapping() (string){
|
||||
return `"mappings":{
|
||||
"hardware":{
|
||||
|
@ -62,7 +65,7 @@ func (this *Hardware )CreateHardware( ) error{
|
|||
|
||||
func GetHardwares(limit int,size int) ([]Hardware,error){
|
||||
var ret []Hardware
|
||||
data,ids,e := db.GetElastic().Query("hardware_data",nil,Hardware{},limit,size)
|
||||
data,ids,e := db.GetElastic().Query("hardware_data",nil,reflect.TypeOf(Hardware{}),limit,size)
|
||||
if nil != e{
|
||||
return nil,e
|
||||
}
|
||||
|
@ -77,7 +80,7 @@ func GetHardwares(limit int,size int) ([]Hardware,error){
|
|||
|
||||
func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
||||
var ret []Hardware
|
||||
data,ids,e := db.GetElastic().Query("hardware_data",query,Hardware{},limit,offset)
|
||||
data,ids,e := db.GetElastic().Query("hardware_data",query,reflect.TypeOf(Hardware{}),limit,offset)
|
||||
log.Print(data)
|
||||
if nil != e{
|
||||
return nil,e
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"background/db"
|
||||
"background/logs"
|
||||
"background/utils"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"log"
|
||||
"background/db"
|
||||
"background/logs"
|
||||
)
|
||||
|
||||
type Users struct {
|
||||
|
|
|
@ -2,12 +2,14 @@ package model
|
|||
|
||||
import (
|
||||
"background/db"
|
||||
"reflect"
|
||||
"strings"
|
||||
"ubntgo/logger"
|
||||
|
||||
"github.com/go-openapi/errors"
|
||||
json "github.com/json-iterator/go"
|
||||
"gopkg.in/olivere/elastic.v3"
|
||||
"qiniupkg.com/x/log.v7"
|
||||
"strings"
|
||||
"ubntgo/logger"
|
||||
)
|
||||
|
||||
type Field struct {
|
||||
|
@ -65,7 +67,7 @@ func MysqlToElasticSearchMapping(types string,Key string) string{
|
|||
// 不同类型db之间进行缓存
|
||||
func QueryDocument(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
||||
var ret []Hardware
|
||||
data,ids,e := db.GetElastic().Query("doc",query,Hardware{},limit,offset)
|
||||
data,ids,e := db.GetElastic().Query("doc",query,reflect.TypeOf(Hardware{}),limit,offset)
|
||||
log.Print(data)
|
||||
if nil != e{
|
||||
return nil,e
|
||||
|
@ -122,6 +124,7 @@ func PortDocumentToElasticsearch(tblname string ) error{
|
|||
if nil != e{
|
||||
log.Print(e.Error())
|
||||
}
|
||||
log.Print("index not existed , create " + tblname)
|
||||
}
|
||||
}else{
|
||||
props := map[string]interface{}{}
|
||||
|
|
|
@ -5,12 +5,16 @@ import (
|
|||
"background/db"
|
||||
"background/logs"
|
||||
"background/model"
|
||||
"background/utils"
|
||||
"log"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/olivere/elastic"
|
||||
)
|
||||
|
||||
func InitConfig() {
|
||||
e := config.Init("user.yaml")
|
||||
e := config.Init("../user.yaml")
|
||||
if nil != e {
|
||||
log.Println(e.Error())
|
||||
}
|
||||
|
@ -33,9 +37,9 @@ func InitRedisConfig() {
|
|||
func InitElasticSearch() {
|
||||
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
|
||||
if nil != e {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
func InitLogs() {
|
||||
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
|
||||
}
|
||||
|
@ -44,10 +48,42 @@ func TestPortDocToElastic(t *testing.T) {
|
|||
InitLogs()
|
||||
InitRedisConfig()
|
||||
InitMysql()
|
||||
InitElasticSearch()
|
||||
db.InitELK()
|
||||
e := model.PortDocumentToElasticsearch("doc")
|
||||
if nil != e {
|
||||
t.Error(e)
|
||||
log.Print(e.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestReflect(t *testing.T){
|
||||
type XX struct{
|
||||
a int16 `json:"bb"`
|
||||
b string `json: "cc"`
|
||||
}
|
||||
x := utils.ReflectMakeNew(reflect.TypeOf(1212))
|
||||
y := utils.ReflectMakeNew(reflect.TypeOf(XX{}))
|
||||
|
||||
log.Print(x)
|
||||
log.Print(y)
|
||||
remap := utils.ReflectTagMap(reflect.TypeOf(XX{}))
|
||||
log.Print(remap)
|
||||
t.Log(x)
|
||||
}
|
||||
|
||||
func TestQueryDoc(t *testing.T){
|
||||
InitConfig()
|
||||
InitLogs()
|
||||
InitRedisConfig()
|
||||
InitMysql()
|
||||
db.InitELK()
|
||||
|
||||
query := elastic.NewTermQuery("content", "title")
|
||||
//_ = elastic.NewQueryStringQuery(fieldValue) //关键字查询
|
||||
searchResult,titles,e := db.GetElastic().Query("doc",query,reflect.TypeOf(model.Doc{}),10,0)
|
||||
if nil != e{
|
||||
log.Print(e.Error())
|
||||
}
|
||||
log.Print(searchResult)
|
||||
log.Print(titles)
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type OrmTime struct {
|
||||
|
|
Loading…
Reference in New Issue