From 252eab9a9321955a08af8d03571c97298011f851 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Fri, 1 May 2020 01:54:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=8E=E5=8C=96=E4=BA=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8Cmap=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/elasticEngine.go | 5 ++-- main.go | 2 -- model/port.go | 61 ++++++++++++++++++++++++++++++-------------- utils/jsonbuilder.go | 27 ++++++++++++++++++++ 4 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 utils/jsonbuilder.go diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 7004c9b..bb35423 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -8,7 +8,7 @@ import ( "reflect" ) const( - ERROR_PTR = "nullpointer error" + ERROR_PTR = "null pointer error" INPUT_TYPE_ERROR = "wrong input parameter" CREATED_ERROR = "create error" DELETE_ERROR = "delete error" @@ -123,7 +123,7 @@ func (p *ElkEngine)CreateIndex(index string,typemaping string) error{ if exists{ return errors.New(INDEX_EXISTED) } - createIndex, err := p.cli.CreateIndex(index).Do(context.Background()) + createIndex, err := p.cli.CreateIndex(index).Body(typemaping).Do(context.Background()) if err != nil { log.Print(err) return err @@ -132,6 +132,7 @@ func (p *ElkEngine)CreateIndex(index string,typemaping string) error{ return errors.New("create index error") // Not acknowledged } + return nil } return errors.New(ERROR_PTR) } diff --git a/main.go b/main.go index 470b843..8f7b893 100644 --- a/main.go +++ b/main.go @@ -125,9 +125,7 @@ func main() { api.POST("/image_upload",fileController.OnUpload) // 上传图片 api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片 api.GET("/image_thumbnail/:file",fileController.OnThumbnail) // 下载图片 - api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 - api.POST("/hardware",controller.AddHardware) // 新增硬件 api.GET("/hardware",controller.ReadHardWare) // 读取硬件 api.DELETE("/hardware",controller.DeleteHardWare) // 读取硬件 diff --git a/model/port.go b/model/port.go index 6a01a83..31a68df 100644 --- a/model/port.go +++ b/model/port.go @@ -35,17 +35,31 @@ func MysqlToElasticSearchMapping(types string,Key string) string{ return "" } /* -`"mappings":{ - "hardware":{ - "properties":{ - "id":{"type":"keyword"}, - "name":{"type":"keyword"}, - "desc":{"type":"text"}, - "pic":{"type":"doc"}, - "doc":{"type":"doc"} - } +"settings":{ + "number_of_shards":1, + "number_of_replicas":0 + }, + "mappings":{ + "properties":{ + "user":{ + "type":"keyword" + }, + "message":{ + "type":"text", + "store": true, + "fielddata": true + }, + "tags":{ + "type":"keyword" + }, + "location":{ + "type":"geo_point" + }, + "suggest_field":{ + "type":"completion" + } + } } - }` */ // 不同类型db之间进行缓存 func PortDocumentToElasticsearch(tblname string ) error{ @@ -56,19 +70,27 @@ func PortDocumentToElasticsearch(tblname string ) error{ logger.Debug(e.Error()) return e } - if existed,_ := db.GetElastic().IndexExisted("doc");existed{ + if existed,_ := db.GetElastic().IndexExisted(tblname);existed{ return errors.New(203,"data existed") } - mapping := map[string]interface{}{} - indexprops := map[string]interface{}{} - - mapping["mapping"] = map[string]interface{}{} - mapping["mapping"].(map[string]interface{})[tblname] = map[string]interface{}{} - mapping["mapping"].(map[string]interface{})[tblname].(map[string]interface{})["properties"] = indexprops + props := map[string]interface{}{} + mapping := map[string]interface{}{ + "settings":map[string]interface{}{ + "analysis":map[string]interface{}{ + "analyzer":map[string]interface{}{ + "default":map[string]interface{}{ + "type": "smartcn", + }, + }, + }, + }, + "mappings": map[string]interface{}{ + "properties":props, + }, + } for _,v := range columns{ - indexprops[v.Field] = map[string]string{}; - indexprops[v.Field].(map[string]string)["type"] = MysqlToElasticSearchMapping(v.Type,v.Key) + props[v.Field] = map[string]string{"type":MysqlToElasticSearchMapping(v.Type,v.Key)}; } dat,e := json.Marshal(mapping) if nil != e{ @@ -78,5 +100,6 @@ func PortDocumentToElasticsearch(tblname string ) error{ if nil != e{ log.Print(e.Error()) } + log.Print(string(dat)) return nil } \ No newline at end of file diff --git a/utils/jsonbuilder.go b/utils/jsonbuilder.go new file mode 100644 index 0000000..be152f7 --- /dev/null +++ b/utils/jsonbuilder.go @@ -0,0 +1,27 @@ +package utils + +import ( + "github.com/pkg/errors" + "reflect" +) + +// to simplefy add json + +type JsonObject struct{ + parent *JsonObject + child *JsonObject + value interface{} + +} + +func (*JsonObject)InsertObj(key string,obj interface{}) error{ + if reflect.TypeOf(obj).Kind() != reflect.Map{ + return errors.New("wrong parameter") + } + return nil +} +func (*JsonObject)InsertArrary(key string,arr interface{}) error { + if reflect.TypeOf(arr).Kind() != reflect.Array{ + return errors.New("wrong parameter") + } +} \ No newline at end of file