美化了代码,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

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

View File

@ -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) // 读取硬件

View File

@ -35,17 +35,31 @@ func MysqlToElasticSearchMapping(types string,Key string) string{
return ""
}
/*
`"mappings":{
"hardware":{
"settings":{
"number_of_shards":1,
"number_of_replicas":0
},
"mappings":{
"properties":{
"id":{"type":"keyword"},
"name":{"type":"keyword"},
"desc":{"type":"text"},
"pic":{"type":"doc"},
"doc":{"type":"doc"}
"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
}

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