|
@@ -0,0 +1,70 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "log"
|
|
|
+ "os"
|
|
|
+
|
|
|
+ "github.com/blevesearch/bleve"
|
|
|
+)
|
|
|
+
|
|
|
+const IndexPath = "example.bleve"
|
|
|
+
|
|
|
+type Named struct {
|
|
|
+ Name string
|
|
|
+}
|
|
|
+
|
|
|
+func createIndex() bleve.Index {
|
|
|
+ // open a new index
|
|
|
+ mapping := bleve.NewIndexMapping()
|
|
|
+ index, err := bleve.New(IndexPath, mapping)
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ // index some data
|
|
|
+ err = index.Index("latin", Named{"quadra"})
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ err = index.Index("pirate", Named{"The heavy-hearted kraken fiery breaks the jack."})
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ err = index.Index("sf", Named{"Cosmonauts experiment from flights like the final ships."})
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ err = index.Index("cuisine", Named{"Try breaking steak pilaf garnished with the gravy."})
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ err = index.Index("esoteric", "The death of fearing doers is unbiased, final and from the heart.")
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ return index
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ index, err := bleve.Open(IndexPath)
|
|
|
+ if err != nil {
|
|
|
+ if err == bleve.ErrorIndexPathDoesNotExist {
|
|
|
+ index = createIndex()
|
|
|
+ } else {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ defer index.Close()
|
|
|
+
|
|
|
+ // search for some text
|
|
|
+ //query := bleve.NewMatchQuery(os.Args[1])
|
|
|
+ query := bleve.NewFuzzyQuery(os.Args[1])
|
|
|
+ query.SetFuzziness(2)
|
|
|
+ search := bleve.NewSearchRequest(query)
|
|
|
+ searchResults, err := index.Search(search)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err)
|
|
|
+ }
|
|
|
+ fmt.Println(searchResults)
|
|
|
+}
|