blevedemo.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/blevesearch/bleve"
  7. )
  8. const IndexPath = "example.bleve"
  9. type Named struct {
  10. Name string
  11. }
  12. func createIndex() bleve.Index {
  13. // open a new index
  14. mapping := bleve.NewIndexMapping()
  15. index, err := bleve.New(IndexPath, mapping)
  16. if err != nil {
  17. panic(err)
  18. }
  19. // index some data
  20. err = index.Index("latin", Named{"quadra"})
  21. if err != nil {
  22. panic(err)
  23. }
  24. err = index.Index("pirate", Named{"The heavy-hearted kraken fiery breaks the jack."})
  25. if err != nil {
  26. panic(err)
  27. }
  28. err = index.Index("sf", Named{"Cosmonauts experiment from flights like the final ships."})
  29. if err != nil {
  30. panic(err)
  31. }
  32. err = index.Index("cuisine", Named{"Try breaking steak pilaf garnished with the gravy."})
  33. if err != nil {
  34. panic(err)
  35. }
  36. err = index.Index("esoteric", "The death of fearing doers is unbiased, final and from the heart.")
  37. if err != nil {
  38. panic(err)
  39. }
  40. return index
  41. }
  42. func main() {
  43. index, err := bleve.Open(IndexPath)
  44. if err != nil {
  45. if err == bleve.ErrorIndexPathDoesNotExist {
  46. index = createIndex()
  47. } else {
  48. panic(err)
  49. }
  50. }
  51. defer index.Close()
  52. // search for some text
  53. //query := bleve.NewMatchQuery(os.Args[1])
  54. query := bleve.NewFuzzyQuery(os.Args[1])
  55. query.SetFuzziness(2)
  56. search := bleve.NewSearchRequest(query)
  57. searchResults, err := index.Search(search)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. fmt.Println(searchResults)
  62. }