go-ego / riot

Go Open Source, Distributed, Simple and efficient Search Engine; Warning: This is V1 and beta version, because of big memory consume, and the V2 will be rewrite all code.
Apache License 2.0
6.11k stars 474 forks source link

Is is required to call indexDoc before search #108

Closed lboquan closed 4 years ago

lboquan commented 4 years ago

I copied the following code from example and add some options in Riot. It works well if I call Index before query. In reality, all the text data will be read from database, and make indexes if required. But I found if I set variable 'buildIdx' to false and run again, nothing is returned. Does that mean I have to query database and make indexes before query?

package main

import (
    "log"

    "github.com/go-ego/riot"
    "github.com/go-ego/riot/types"
)

var (
    searcher = riot.Engine{}
)

func main() {
    searcher.Init(types.EngineOpts{
        Using:       3,
        GseDict:     "zh",
        StoreFolder: "./indexes",
        UseStore:    true,
    })
    defer searcher.Close()
    buildIdx := true

    if buildIdx {
        text := "《复仇者联盟3:无限战争》是全片使用IMAX摄影机拍摄"
        text1 := "在IMAX影复仇院放映时者"

        searcher.Index("你好,世界", types.DocData{Content: text})
        searcher.Index("2", types.DocData{Content: text1}, false)
        searcher.Flush()
    }

    // 搜索输出格式见 types.SearchResp 结构体
    log.Print(searcher.Search(types.SearchReq{Text: "复仇者"}).Docs)
}
lboquan commented 4 years ago

Sorry, I finally got it, I need to call searcher.Flush().