es 驱动
parent
43e0a6c865
commit
b1564b91ff
|
@ -52,6 +52,29 @@ func DeleteHardWare(c *gin.Context) {
|
||||||
resp.Data = nil
|
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) {
|
func UpdateHardWare(c *gin.Context) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"background/utils"
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
"gopkg.in/olivere/elastic.v7"
|
"gopkg.in/olivere/elastic.v7"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
const(
|
const(
|
||||||
ERROR_PTR = "null pointer error"
|
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) {
|
limit int,offset int) ([]interface{},[]string,error) {
|
||||||
|
rets := []interface{}{}
|
||||||
if nil != p{
|
if nil != p{
|
||||||
if(limit == 0){
|
if(limit == 0){
|
||||||
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
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)
|
print(err)
|
||||||
return nil,nil,err
|
return nil,nil,err
|
||||||
}
|
}
|
||||||
//var typ Employee
|
|
||||||
typ := reflect.TypeOf(data)
|
|
||||||
rets := res.Each(typ)
|
|
||||||
id := []string{}
|
id := []string{}
|
||||||
for _,vs := range res.Hits.Hits{
|
for _,vs := range res.Hits.Hits{
|
||||||
id = append(id,vs.Id)
|
id = append(id,vs.Id)
|
||||||
|
@ -83,13 +85,25 @@ func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
|
||||||
print(err)
|
print(err)
|
||||||
return nil,nil,err
|
return nil,nil,err
|
||||||
}
|
}
|
||||||
|
|
||||||
//var typ Employee
|
|
||||||
typ := reflect.TypeOf(data)
|
|
||||||
rets := res.Each(typ)
|
|
||||||
id := []string{}
|
id := []string{}
|
||||||
for _,vs := range res.Hits.Hits{
|
for _,vs := range res.Hits.Hits{
|
||||||
id = append(id,vs.Id)
|
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
|
return rets,id,nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
|
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
|
||||||
// This file was generated by swaggo/swag at
|
// 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
|
package docs
|
||||||
|
|
||||||
|
|
4
main.go
4
main.go
|
@ -149,7 +149,7 @@ func main() {
|
||||||
api.POST("/hardware", controller.AddHardware) // 新增硬件
|
api.POST("/hardware", controller.AddHardware) // 新增硬件
|
||||||
api.GET("/hardware", controller.ReadHardWare) // 读取硬件
|
api.GET("/hardware", controller.ReadHardWare) // 读取硬件
|
||||||
api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件
|
api.DELETE("/hardware", controller.DeleteHardWare) // 读取硬件
|
||||||
|
api.POST("/doc_search",)
|
||||||
api.PUT("/file", fileController.OnFileUploadFile) // 上传文件
|
api.PUT("/file", fileController.OnFileUploadFile) // 上传文件
|
||||||
api.GET("/file", fileController.DownloadFile) // 下载 文件
|
api.GET("/file", fileController.DownloadFile) // 下载 文件
|
||||||
api.GET("/filelist", fileController.FileList) // 文件列表
|
api.GET("/filelist", fileController.FileList) // 文件列表
|
||||||
|
@ -173,7 +173,7 @@ func main() {
|
||||||
api.POST("/getbook", controller.GetBook) // 单书籍数据
|
api.POST("/getbook", controller.GetBook) // 单书籍数据
|
||||||
api.POST("/getbooks", controller.GetPageBook) // 批量书籍
|
api.POST("/getbooks", controller.GetPageBook) // 批量书籍
|
||||||
api.POST("/delbook", controller.DeleteMemos) // 删除书籍
|
api.POST("/delbook", controller.DeleteMemos) // 删除书籍
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
openapi := r.Group("openapi")
|
openapi := r.Group("openapi")
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"background/db"
|
"background/db"
|
||||||
"background/logs"
|
"background/logs"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
|
@ -2,9 +2,9 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"background/db"
|
"background/db"
|
||||||
|
"background/logs"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"background/logs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
|
|
|
@ -3,6 +3,8 @@ package model
|
||||||
import (
|
import (
|
||||||
"background/db"
|
"background/db"
|
||||||
"background/utils"
|
"background/utils"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gopkg.in/olivere/elastic.v3"
|
"gopkg.in/olivere/elastic.v3"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
|
@ -11,6 +13,7 @@ import (
|
||||||
const (
|
const (
|
||||||
ERR_COLUMN_EXISTED = "column_existed"
|
ERR_COLUMN_EXISTED = "column_existed"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HardwareTypeMapping() (string){
|
func HardwareTypeMapping() (string){
|
||||||
return `"mappings":{
|
return `"mappings":{
|
||||||
"hardware":{
|
"hardware":{
|
||||||
|
@ -62,7 +65,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_data",nil,Hardware{},limit,size)
|
data,ids,e := db.GetElastic().Query("hardware_data",nil,reflect.TypeOf(Hardware{}),limit,size)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
return 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){
|
func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
||||||
var ret []Hardware
|
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)
|
log.Print(data)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
return nil,e
|
return nil,e
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"background/db"
|
||||||
|
"background/logs"
|
||||||
"background/utils"
|
"background/utils"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"background/db"
|
|
||||||
"background/logs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Users struct {
|
type Users struct {
|
||||||
|
|
|
@ -2,12 +2,14 @@ package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"background/db"
|
"background/db"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"ubntgo/logger"
|
||||||
|
|
||||||
"github.com/go-openapi/errors"
|
"github.com/go-openapi/errors"
|
||||||
json "github.com/json-iterator/go"
|
json "github.com/json-iterator/go"
|
||||||
"gopkg.in/olivere/elastic.v3"
|
"gopkg.in/olivere/elastic.v3"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
"strings"
|
|
||||||
"ubntgo/logger"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Field struct {
|
type Field struct {
|
||||||
|
@ -65,7 +67,7 @@ func MysqlToElasticSearchMapping(types string,Key string) string{
|
||||||
// 不同类型db之间进行缓存
|
// 不同类型db之间进行缓存
|
||||||
func QueryDocument(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
func QueryDocument(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
||||||
var ret []Hardware
|
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)
|
log.Print(data)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
return nil,e
|
return nil,e
|
||||||
|
@ -122,6 +124,7 @@ func PortDocumentToElasticsearch(tblname string ) error{
|
||||||
if nil != e{
|
if nil != e{
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
|
log.Print("index not existed , create " + tblname)
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
props := map[string]interface{}{}
|
props := map[string]interface{}{}
|
||||||
|
|
|
@ -5,12 +5,16 @@ import (
|
||||||
"background/db"
|
"background/db"
|
||||||
"background/logs"
|
"background/logs"
|
||||||
"background/model"
|
"background/model"
|
||||||
|
"background/utils"
|
||||||
"log"
|
"log"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/olivere/elastic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitConfig() {
|
func InitConfig() {
|
||||||
e := config.Init("user.yaml")
|
e := config.Init("../user.yaml")
|
||||||
if nil != e {
|
if nil != e {
|
||||||
log.Println(e.Error())
|
log.Println(e.Error())
|
||||||
}
|
}
|
||||||
|
@ -33,9 +37,9 @@ func InitRedisConfig() {
|
||||||
func InitElasticSearch() {
|
func InitElasticSearch() {
|
||||||
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
|
e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping())
|
||||||
if nil != e {
|
if nil != e {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitLogs() {
|
func InitLogs() {
|
||||||
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
|
logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile)
|
||||||
}
|
}
|
||||||
|
@ -44,10 +48,42 @@ func TestPortDocToElastic(t *testing.T) {
|
||||||
InitLogs()
|
InitLogs()
|
||||||
InitRedisConfig()
|
InitRedisConfig()
|
||||||
InitMysql()
|
InitMysql()
|
||||||
InitElasticSearch()
|
|
||||||
db.InitELK()
|
db.InitELK()
|
||||||
e := model.PortDocumentToElasticsearch("doc")
|
e := model.PortDocumentToElasticsearch("doc")
|
||||||
if nil != e {
|
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
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OrmTime struct {
|
type OrmTime struct {
|
||||||
|
|
Loading…
Reference in New Issue