package util import ( "fmt" "github.com/go-ego/riot" "github.com/go-ego/riot/types" "trading-go/common" ) var searcher = riot.Engine{} type Index struct { Id uint `db:"id"` Desc string `db:"desc"` Title string `db:"title"` } func init() { searcher.Init(types.EngineOpts{ Using: 3, GseDict: "zh", }) var index []Index db := common.DB sqlStr := "SELECT id, `desc`, title FROM goods" err := db.Select(&index, sqlStr) if err != nil { fmt.Println("[search engine] ----->>> search engine restore failed") return } for i := 0; i < len(index); i++ { NewIndex(index[i].Id, index[i].Desc+index[i].Title) } } func NewIndex(ID uint, doc string) { searcher.Index(fmt.Sprintf("%d", ID), types.DocData{Content: doc}, true) fmt.Println("[search engine] ----->>> " + doc + "ok") searcher.Flush() } func SearchId(keys string) (ids []string) { docs := searcher.Search(types.SearchReq{Text: keys}).Docs.(types.ScoredDocs) for i := 0; i < len(docs); i++ { ids = append(ids, docs[i].DocId) } return } func DelIndex(ID uint) { searcher.RemoveDoc(fmt.Sprintf("%d", ID), true) fmt.Println("[search engine] ----->>> ok") searcher.Flush() }