I am new to Go and Bleve (sorry if I'm asking trivial things…). This search engine seems to be really nice, but I am getting stuck when it comes to deal with my search results.
Let's say we have a struct:
type Person struct {
Name string `json:"name"`
Bio string `json:"bio"`
}
Now, we extract data from the database (using sqlx lib):
rows := []Person{}
db.Select(&rows, "SELECT * FROM person")
...and index it:
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
}
}
Now we have our index created. It works great.
Using the bleve command line utility, it returns data correctly:
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. ...
Here we see that bleve has stored Name
and Bio
fields.
Now I want to do it to access it from my code!
query := bleve.NewMatchAllQuery()
searchRequest := bleve.NewSearchRequest(query)
searchResults, _ := index.Index.Search(searchRequest)
fmt.Println(searchResults[0].ID) // <- This works
But I don't only want the ID and then query the database to get the rest of the date. To avoid hitting database, I would like to be able to do something like:
fmt.Println(searchResults[0].Bio) // <- This doesn't work :(
Could you please help?
Every hit in the search result is a DocumentMatch. You can see in the documentation that
DocumentMatch
hasFields
which is amap[string]interface{}
and can be accessed as follows:Bleve doesn't include the document's fields in the results by default. You must provide a list of the fields you'd like returned to
SearchRequest.Fields
(the argument toindex.Search
). Alternatively, you can setto return all fields.