美化了代码,map初始化

master
a7458969 2020-05-01 01:54:19 +08:00
parent 77896b9f65
commit 252eab9a93
4 changed files with 72 additions and 23 deletions

View File

@ -123,7 +123,7 @@ func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
if exists{ if exists{
return errors.New(INDEX_EXISTED) 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 { if err != nil {
log.Print(err) log.Print(err)
return err return err
@ -132,6 +132,7 @@ func (p *ElkEngine)CreateIndex(index string,typemaping string) error{
return errors.New("create index error") return errors.New("create index error")
// Not acknowledged // Not acknowledged
} }
return nil
} }
return errors.New(ERROR_PTR) return errors.New(ERROR_PTR)
} }

View File

@ -125,9 +125,7 @@ func main() {
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.OnThumbnail) // 下载图片 api.GET("/image_thumbnail/:file",fileController.OnThumbnail) // 下载图片
api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型
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) // 读取硬件

View File

@ -35,17 +35,31 @@ func MysqlToElasticSearchMapping(types string,Key string) string{
return "" return ""
} }
/* /*
`"mappings":{ "settings":{
"hardware":{ "number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"properties":{ "properties":{
"id":{"type":"keyword"}, "user":{
"name":{"type":"keyword"}, "type":"keyword"
"desc":{"type":"text"}, },
"pic":{"type":"doc"}, "message":{
"doc":{"type":"doc"} "type":"text",
"store": true,
"fielddata": true
},
"tags":{
"type":"keyword"
},
"location":{
"type":"geo_point"
},
"suggest_field":{
"type":"completion"
}
} }
} }
}`
*/ */
// 不同类型db之间进行缓存 // 不同类型db之间进行缓存
func PortDocumentToElasticsearch(tblname string ) error{ func PortDocumentToElasticsearch(tblname string ) error{
@ -56,19 +70,27 @@ func PortDocumentToElasticsearch(tblname string ) error{
logger.Debug(e.Error()) logger.Debug(e.Error())
return e return e
} }
if existed,_ := db.GetElastic().IndexExisted("doc");existed{ if existed,_ := db.GetElastic().IndexExisted(tblname);existed{
return errors.New(203,"data existed") return errors.New(203,"data existed")
} }
mapping := map[string]interface{}{} props := map[string]interface{}{}
indexprops := map[string]interface{}{} mapping := map[string]interface{}{
"settings":map[string]interface{}{
mapping["mapping"] = map[string]interface{}{} "analysis":map[string]interface{}{
mapping["mapping"].(map[string]interface{})[tblname] = map[string]interface{}{} "analyzer":map[string]interface{}{
mapping["mapping"].(map[string]interface{})[tblname].(map[string]interface{})["properties"] = indexprops "default":map[string]interface{}{
"type": "smartcn",
},
},
},
},
"mappings": map[string]interface{}{
"properties":props,
},
}
for _,v := range columns{ for _,v := range columns{
indexprops[v.Field] = map[string]string{}; props[v.Field] = map[string]string{"type":MysqlToElasticSearchMapping(v.Type,v.Key)};
indexprops[v.Field].(map[string]string)["type"] = MysqlToElasticSearchMapping(v.Type,v.Key)
} }
dat,e := json.Marshal(mapping) dat,e := json.Marshal(mapping)
if nil != e{ if nil != e{
@ -78,5 +100,6 @@ func PortDocumentToElasticsearch(tblname string ) error{
if nil != e{ if nil != e{
log.Print(e.Error()) log.Print(e.Error())
} }
log.Print(string(dat))
return nil return nil
} }

27
utils/jsonbuilder.go Normal file
View File

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