2020-04-29 17:52:35 +00:00
|
|
|
package model
|
|
|
|
|
2020-04-30 09:33:47 +00:00
|
|
|
import (
|
|
|
|
"background/db"
|
|
|
|
"github.com/go-openapi/errors"
|
|
|
|
json "github.com/json-iterator/go"
|
|
|
|
"qiniupkg.com/x/log.v7"
|
|
|
|
"strings"
|
|
|
|
"ubntgo/logger"
|
|
|
|
)
|
2020-04-29 17:52:35 +00:00
|
|
|
|
2020-04-30 09:33:47 +00:00
|
|
|
type Field struct {
|
|
|
|
Field string `sql:"Field"`
|
|
|
|
Type string `sql:"Type"`
|
|
|
|
Key string `sql:"Key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func MysqlToElasticSearchMapping(types string,Key string) string{
|
|
|
|
if Key == "PRI"{
|
|
|
|
return "keyword"
|
|
|
|
}
|
|
|
|
if(strings.Contains(types,"int(")){
|
2020-05-01 13:21:04 +00:00
|
|
|
return "integer"
|
2020-04-30 09:33:47 +00:00
|
|
|
}
|
|
|
|
if(strings.Contains(types,"longblob")){
|
|
|
|
return "text"
|
|
|
|
}
|
|
|
|
if(strings.Contains(types,"varchar")){
|
|
|
|
return "text"
|
|
|
|
}
|
|
|
|
if(strings.Contains(types,"datetime")){
|
|
|
|
return "date"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
/*
|
2020-04-30 17:54:19 +00:00
|
|
|
"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"
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 09:33:47 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
// 不同类型db之间进行缓存
|
|
|
|
func PortDocumentToElasticsearch(tblname string ) error{
|
|
|
|
columns := []Field{}
|
|
|
|
|
|
|
|
e := db.GetMysqlClient().Query2("describe " + tblname,&columns)
|
|
|
|
if nil != e{
|
|
|
|
logger.Debug(e.Error())
|
|
|
|
return e
|
|
|
|
}
|
2020-04-30 17:54:19 +00:00
|
|
|
if existed,_ := db.GetElastic().IndexExisted(tblname);existed{
|
2020-04-30 09:33:47 +00:00
|
|
|
return errors.New(203,"data existed")
|
|
|
|
}
|
2020-04-30 17:54:19 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2020-04-30 09:33:47 +00:00
|
|
|
|
|
|
|
for _,v := range columns{
|
2020-04-30 17:54:19 +00:00
|
|
|
props[v.Field] = map[string]string{"type":MysqlToElasticSearchMapping(v.Type,v.Key)};
|
2020-04-30 09:33:47 +00:00
|
|
|
}
|
|
|
|
dat,e := json.Marshal(mapping)
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
}
|
|
|
|
e = db.GetElastic().CreateIndex(tblname,string(dat))
|
|
|
|
if nil != e{
|
|
|
|
log.Print(e.Error())
|
|
|
|
}
|
2020-04-30 17:54:19 +00:00
|
|
|
log.Print(string(dat))
|
2020-04-30 09:33:47 +00:00
|
|
|
return nil
|
|
|
|
}
|