Last active
June 23, 2022 00:16
-
-
Save mosuka/46f5f22cab389e559fb1 to your computer and use it in GitHub Desktop.
Bleve with Japanese
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 ( | |
"os" | |
"fmt" | |
"github.com/blevesearch/bleve" | |
_ "github.com/blevesearch/bleve/analysis/language/ja" | |
) | |
func main() { | |
indexMapping := bleve.NewIndexMapping() | |
docMapping := bleve.NewDocumentMapping() | |
fieldMapping := bleve.NewTextFieldMapping() | |
fieldMapping.Analyzer = "ja" | |
docMapping.AddFieldMappingsAt("Body", fieldMapping) | |
indexMapping.AddDocumentMapping("Example", docMapping) | |
// create index | |
var index bleve.Index | |
var err error | |
_, err = os.Stat("example.bleve") | |
if err == nil { | |
index, err = bleve.Open("example.bleve") | |
} else { | |
index, err = bleve.New("example.bleve", indexMapping) | |
} | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// index data | |
data := struct { | |
Body string | |
}{ | |
Body: "東京都港区六本木", | |
} | |
index.Index("000", data) // expect it to be tokeinzed "東京","都","港","区" and "六本木" from "東京都港区六本木" | |
// search index | |
query := bleve.NewMatchQuery("京都") // search for "京都" | |
search := bleve.NewSearchRequest(query) | |
searchResults, err := index.Search(search) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
// show results | |
fmt.Println(searchResults) // expect "No matches", but "京都" will match | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment