Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use golang bleve search results?

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?

like image 928
user650108 Avatar asked Jan 29 '23 01:01

user650108


1 Answers

Every hit in the search result is a DocumentMatch. You can see in the documentation that DocumentMatch has Fields which is a map[string]interface{} and can be accessed as follows:

searchResults.Hits[0].Fields["Bio"].(string)

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 to index.Search). Alternatively, you can set

searchRequest.Fields = []string{"*"}

to return all fields.

like image 84
Zippo Avatar answered Feb 02 '23 10:02

Zippo