Skip to content

Instantly share code, notes, and snippets.

@tenox7
Created May 15, 2024 06:15
Show Gist options
  • Save tenox7/05d0d944a9318907ffd63549cf34d18a to your computer and use it in GitHub Desktop.
Save tenox7/05d0d944a9318907ffd63549cf34d18a to your computer and use it in GitHub Desktop.
example of how to index and search a directory of text files using bleve
package main
import (
"fmt"
"log"
"os"
"path"
"github.com/blevesearch/bleve/v2"
)
const myDir = "some/dir/path"
const myQuery = "lorem ipsum"
func main() {
index, err := bleve.NewMemOnly(bleve.NewIndexMapping())
if err != nil {
log.Fatal(err)
}
fsd, err := os.ReadDir(myDir)
if err != nil {
log.Fatal(err)
}
for _, f := range fsd {
if f.IsDir() {
continue
}
index.Index(f.Name(), func(name string) string {
f, err := os.ReadFile(path.Join(myDir, name))
if err != nil {
return ""
}
return string(f)
}(f.Name()))
}
res, err := index.Search(bleve.NewSearchRequest(bleve.NewMatchQuery(myQuery)))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res)
for _, hit := range res.Hits {
fmt.Printf("- %s (score: %f)\n", hit.ID, hit.Score)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment