Skip to content

Instantly share code, notes, and snippets.

@brendan-munro
Created February 23, 2016 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendan-munro/32ffb6a377f4a49bc593 to your computer and use it in GitHub Desktop.
Save brendan-munro/32ffb6a377f4a49bc593 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/blevesearch/bleve"
)
func main() {
// index, err := bleve.Open("example.bleve")
// open a new index
mapping := bleve.NewIndexMapping()
docMapping := bleve.NewDocumentMapping()
nameMapping := bleve.NewTextFieldMapping()
nameMapping.IncludeTermVectors = true
docMapping.AddFieldMappingsAt("name", nameMapping)
mapping.AddDocumentMapping("doc", docMapping)
mapping.TypeField = "_type"
index, err := bleve.New("example.bleve", mapping)
if err != nil {
fmt.Println(err)
return
}
for i := 0; i < 1000; i++ {
data := struct {
Type string `json:"_type"`
Name string `json:"name"`
}{
Type: "doc",
Name: fmt.Sprintf("test%d", i),
}
// index some data
index.Index(fmt.Sprintf("id%d", i), data)
}
// search for some text
// query := bleve.NewMatchQuery("test1")
// query := bleve.NewPhraseQuery([]string{"test5"}, "name")
query := bleve.NewDisjunctionQuery([]bleve.Query{
bleve.NewPhraseQuery([]string{"test5"}, "name"),
bleve.NewPhraseQuery([]string{"test3"}, "name"),
})
search := bleve.NewSearchRequest(query)
// typeFacet := bleve.NewFacetRequest("type", 10)
// search.AddFacet("type", typeFacet)
searchResults, err := index.Search(search)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(searchResults)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment