Created
May 14, 2015 06:25
-
-
Save dtynn/17ce8d32b9284bebbd7e to your computer and use it in GitHub Desktop.
bleve highlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"github.com/blevesearch/bleve" | |
) | |
func main() { | |
indexMapping := bleve.NewIndexMapping() | |
index, err := bleve.NewUsing("boltdb", indexMapping, "boltdb", nil) | |
if err == bleve.ErrorIndexPathExists { | |
index, err = bleve.OpenUsing("boltdb", nil) | |
} | |
if err != nil { | |
log.Fatalln(err) | |
} | |
docs := []struct { | |
Title string | |
Tags []string | |
}{ | |
{ | |
Title: "highlight", | |
Tags: []string{"abc", "12345 678"}, | |
}, | |
} | |
for _, doc := range docs { | |
index.Index(doc.Title, doc) | |
} | |
keyword := "678" | |
query := bleve.NewMatchQuery(keyword) | |
search := bleve.NewSearchRequest(query) | |
search.Highlight = bleve.NewHighlight() | |
searchResults, err := index.Search(search) | |
if err != nil { | |
log.Fatalln(err) | |
return | |
} | |
log.Printf("result: %s: %s \n", keyword, searchResults) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment