我是 Go 和Bleve的新手(对不起,如果我问的是琐碎的事情……)。这个搜索引擎似乎非常好,但是在处理我的搜索结果时我被卡住了。
假设我们有一个结构:
type Person struct {
Name string `json:"name"`
Bio string `json:"bio"`
}
现在,我们从数据库中提取数据(使用 sqlx lib):
rows := []Person{}
db.Select(&rows, "SELECT * FROM person")
...并将其编入索引:
index.Index, err = bleve.Open("index.bleve")
batch := index.Index.NewBatch()
i := 0
for _, row := range rows {
rowId := fmt.Sprintf("%T_%d", row, row.ID)
batch.Index(rowId, row)
i++
if i > 100 {
index.Index.Batch(batch)
i = 0
}
}
现在我们已经创建了索引。它工作得很好。
使用bleve 命令行实用程序,它可以正确返回数据:
bleve query index.bleve doe
3 matches, showing 1 through 3, took 27.767838ms
1. Person_68402 (0.252219)
Name
Doe
Bio
My name is John Doe!
2. ...
在这里我们看到bleve已经存储了Name
和Bio
字段。
现在我想从我的代码中访问它!
query := bleve.NewMatchAllQuery()
searchRequest := bleve.NewSearchRequest(query)
searchResults, _ := index.Index.Search(searchRequest)
fmt.Println(searchResults[0].ID) // <- This works
但我不只是想要ID,然后查询数据库以获取其余日期。为避免命中数据库,我希望能够执行以下操作:
fmt.Println(searchResults[0].Bio) // <- This doesn't work :(
能否请你帮忙?