测试成功文章转存到elasticsearch,方便索引

master
a7458969 2020-05-04 02:05:04 +08:00
parent 0735ca6904
commit 3e61016a3e
2 changed files with 92 additions and 22 deletions

View File

@ -105,4 +105,15 @@ func DeleteArticleType(id int32) error {
} }
return nil return nil
}
func GetAllDocs() ([]Doc,error) {
ret := []Doc{}
sql := fmt.Sprintf("select * from doc")
e := db.GetMysqlClient().Query2(sql,&ret)
if nil != e {
logs.Error(e.Error())
return nil,e
}
return ret,nil
} }

View File

@ -4,6 +4,7 @@ import (
"background/db" "background/db"
"github.com/go-openapi/errors" "github.com/go-openapi/errors"
json "github.com/json-iterator/go" json "github.com/json-iterator/go"
"gopkg.in/olivere/elastic.v3"
"qiniupkg.com/x/log.v7" "qiniupkg.com/x/log.v7"
"strings" "strings"
"ubntgo/logger" "ubntgo/logger"
@ -62,44 +63,102 @@ func MysqlToElasticSearchMapping(types string,Key string) string{
} }
*/ */
// 不同类型db之间进行缓存 // 不同类型db之间进行缓存
func PortDocumentToElasticsearch(tblname string ) error{ func QueryDocument(query elastic.Query,limit int,offset int) ([]Hardware,error){
columns := []Field{} var ret []Hardware
data,ids,e := db.GetElastic().Query("doc",query,Hardware{},limit,offset)
log.Print(data)
if nil != e{
return nil,e
}
i := 0
for _,v := range data{
ret = append(ret,v.(Hardware))
ret[i].ID = ids[i]
i++
}
return ret,nil
}
func InsertDocToElaticSearch(doc Doc) error{
e := db.GetMysqlClient().Query2("describe " + tblname,&columns) matchPhraseQuery := elastic.NewMatchQuery("title", doc.Title)
existedHardware,e := QueryHardwares(matchPhraseQuery,10,0)
log.Print(e,existedHardware)
for _,v := range existedHardware{
if v.Name == doc.Title{
log.Print(v.ID)
return errors.New(200,"existed title")
}
}
e = db.GetElastic().Create("doc","0","",doc)
if nil != e{
log.Print(e.Error())
return e
}
return nil
}
func PortDocumentToElasticsearch(tblname string ) error{
ret,e := GetAllDocs()
if nil != e{
log.Print(e.Error())
}
insert,err := json.Marshal(ret)
if nil != err{
log.Print(err)
}
log.Print(len(ret))
log.Print(string(insert))
columns := []Field{}
e = db.GetMysqlClient().Query2("describe " + tblname,&columns)
if nil != e{ if nil != e{
logger.Debug(e.Error()) logger.Debug(e.Error())
return e return e
} }
if existed,_ := db.GetElastic().IndexExisted(tblname);existed{ if existed,_ := db.GetElastic().IndexExisted(tblname);existed{
return errors.New(203,"data existed") for _,v := range ret{
} e := InsertDocToElaticSearch(v)
props := map[string]interface{}{} if nil != e{
mapping := map[string]interface{}{ log.Print(e.Error())
}
}
}else{
props := map[string]interface{}{}
mapping := map[string]interface{}{
"settings":map[string]interface{}{ "settings":map[string]interface{}{
"analysis":map[string]interface{}{ "analysis":map[string]interface{}{
"analyzer":map[string]interface{}{ "analyzer":map[string]interface{}{
"default":map[string]interface{}{ "default":map[string]interface{}{
"type": "smartcn", "type": "smartcn",
}, },
}, },
}, },
}, },
"mappings": map[string]interface{}{ "mappings": map[string]interface{}{
"properties":props, "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))
for _,v := range ret{
e := InsertDocToElaticSearch(v)
if nil != e{
log.Print(e.Error())
}
}
} }
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 return nil
} }