【发布时间】:2018-05-28 20:19:16
【问题描述】:
我是 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 command line utility,它可以正确返回数据:
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,然后查询数据库以获取其余日期。致avoid hitting database,我希望能够这样做:
fmt.Println(searchResults[0].Bio) // <- This doesn't work :(
你能帮忙吗?
【问题讨论】:
标签: go full-text-search bleve