我是新来的,白热化 (抱歉,如果我问的是琐碎的事情,…)。这个搜索引擎看起来真的很不错,但是当涉及到我的搜索结果时,我会陷入困境。
假设我们有一个结构:
type Person struct {
Name string `json:"name"`
Bio string `json:"bio"`
}现在,我们从数据库中提取数据(使用sqlx ):
rows := []Person{}
db.Select(&rows, "SELECT * FROM person")...and索引它:
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 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 :(你能帮忙吗?
发布于 2018-05-28 21:37:53
搜索结果中的每一个点击都是一个DocumentMatch。您可以在文档中看到DocumentMatch具有Fields,这是一个map[string]interface{},可以按以下方式访问:
searchResults.Hits[0].Fields["Bio"].(string)默认情况下,Bleve在结果中不包括文档的字段。您必须提供希望返回到SearchRequest.Fields (index.Search的参数)的字段的列表。或者,您可以设置
searchRequest.Fields = []string{"*"}返回所有字段。
https://stackoverflow.com/questions/50572998
复制相似问题