diff --git a/model/blog.go b/model/blog.go index 67fc001..deaeca7 100644 --- a/model/blog.go +++ b/model/blog.go @@ -105,4 +105,15 @@ func DeleteArticleType(id int32) error { } 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 + } \ No newline at end of file diff --git a/model/port.go b/model/port.go index b209e5d..54beb81 100644 --- a/model/port.go +++ b/model/port.go @@ -4,6 +4,7 @@ import ( "background/db" "github.com/go-openapi/errors" json "github.com/json-iterator/go" + "gopkg.in/olivere/elastic.v3" "qiniupkg.com/x/log.v7" "strings" "ubntgo/logger" @@ -62,44 +63,102 @@ func MysqlToElasticSearchMapping(types string,Key string) string{ } */ // 不同类型db之间进行缓存 -func PortDocumentToElasticsearch(tblname string ) error{ - columns := []Field{} +func QueryDocument(query elastic.Query,limit int,offset int) ([]Hardware,error){ + 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{ logger.Debug(e.Error()) return e } if existed,_ := db.GetElastic().IndexExisted(tblname);existed{ - return errors.New(203,"data existed") - } - props := map[string]interface{}{} - mapping := map[string]interface{}{ + for _,v := range ret{ + e := InsertDocToElaticSearch(v) + if nil != e{ + log.Print(e.Error()) + } + } + }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", - }, + "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)) + 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 } \ No newline at end of file