background/model/port.go

189 lines
3.9 KiB
Go
Raw Normal View History

package model
import (
"background/db"
2021-02-05 17:46:59 +00:00
"strings"
"ubntgo/logger"
"github.com/go-openapi/errors"
json "github.com/json-iterator/go"
"gopkg.in/olivere/elastic.v3"
"qiniupkg.com/x/log.v7"
)
type Field struct {
2021-02-07 09:21:19 +00:00
Field string `sql:"Field"`
Type string `sql:"Type"`
Key string `sql:"Key"`
}
2021-02-07 09:21:19 +00:00
func MysqlToElasticSearchMapping(types string, Key string) string {
if Key == "PRI" {
return "keyword"
}
2021-02-07 09:21:19 +00:00
if strings.Contains(types, "int(") {
2020-05-01 13:21:04 +00:00
return "integer"
}
2021-02-07 09:21:19 +00:00
if strings.Contains(types, "longblob") {
return "text"
}
2021-02-07 09:21:19 +00:00
if strings.Contains(types, "varchar") {
return "text"
}
2021-02-07 09:21:19 +00:00
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"
}
}
}
*/
// 不同类型db之间进行缓存
2021-02-07 09:21:19 +00:00
func QueryDocument(query elastic.Query, limit int, offset int) ([]Hardware, error) {
var ret []Hardware
2021-02-07 09:21:19 +00:00
ids, e := db.GetElastic().Query("doc", query, ret, limit, offset)
if nil != e {
return nil, e
}
i := 0
2021-02-07 09:21:19 +00:00
for _, v := range ids {
ret[i].ID = v
i++
}
2021-02-07 09:21:19 +00:00
return ret, nil
}
2021-02-07 09:21:19 +00:00
func InsertDocToElaticSearch(doc Doc) error {
matchPhraseQuery := elastic.NewMatchQuery("title", doc.Title)
2021-02-07 09:21:19 +00:00
existedHardware, e := QueryHardwares(matchPhraseQuery, 10, 0)
log.Print(e, existedHardware)
2021-07-24 18:05:35 +00:00
2021-02-07 09:21:19 +00:00
for _, v := range existedHardware {
if v.Name == doc.Title {
log.Print(v.ID)
2021-02-07 09:21:19 +00:00
return errors.New(200, "existed title")
}
}
2021-02-07 09:21:19 +00:00
e = db.GetElastic().Create("doc", "0", "", doc)
if nil != e {
log.Print(e.Error())
return e
}
return nil
}
2021-02-08 04:05:38 +00:00
func CreateIndexFromMysqlTable(tblname string) error {
columns := []Field{}
2021-07-20 16:14:05 +00:00
e := db.GetMysqlClient().Query2("describe " + tblname, &columns)
2021-02-08 04:05:38 +00:00
if nil != e {
logger.Debug(e.Error())
return e
}
if existed, _ := db.GetElastic().IndexExisted(tblname); existed {
log.Print("index not existed , create " + tblname)
} else {
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 {
props[v.Field] = map[string]string{"type": MysqlToElasticSearchMapping(v.Type, v.Key)}
}
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())
}
log.Print(string(dat))
}
return nil
}
2021-02-07 09:21:19 +00:00
func PortDocumentToElasticsearch(tblname string) error {
ret, e := GetAllDocs()
if nil != e {
log.Print(e.Error())
}
columns := []Field{}
2021-02-07 09:21:19 +00:00
e = db.GetMysqlClient().Query2("describe "+tblname, &columns)
if nil != e {
logger.Debug(e.Error())
return e
}
2021-07-24 18:05:35 +00:00
if existed, _ := db.GetElastic().IndexExisted(tblname); !existed {
props := map[string]interface{}{}
mapping := map[string]interface{}{
2021-02-07 09:21:19 +00:00
"settings": map[string]interface{}{
"analysis": map[string]interface{}{
"analyzer": map[string]interface{}{
"default": map[string]interface{}{
2021-08-01 11:50:48 +00:00
"type": "ik_max_word",
},
2020-04-30 17:54:19 +00:00
},
},
},
"mappings": map[string]interface{}{
2021-02-07 09:21:19 +00:00
"properties": props,
2020-04-30 17:54:19 +00:00
},
}
2021-02-07 09:21:19 +00:00
for _, v := range columns {
props[v.Field] = map[string]string{"type": MysqlToElasticSearchMapping(v.Type, v.Key)}
}
2021-02-07 09:21:19 +00:00
dat, e := json.Marshal(mapping)
if nil != e {
log.Print(e.Error())
}
2021-02-07 09:21:19 +00:00
e = db.GetElastic().CreateIndex(tblname, string(dat))
if nil != e {
log.Print(e.Error())
}
log.Print(string(dat))
2021-02-07 09:21:19 +00:00
for _, v := range ret {
e := InsertDocToElaticSearch(v)
2021-02-07 09:21:19 +00:00
if nil != e {
log.Print(e.Error())
}
}
}
return nil
2021-02-07 09:21:19 +00:00
}