Last active
August 29, 2015 14:15
-
-
Save mschoch/2b0d3f0ac2d92ba21f5c to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"github.com/blevesearch/bleve" | |
) | |
type x struct { | |
Level, Parent string | |
} | |
func main() { | |
index := indexCreate() | |
// populate index with sample data | |
values := []x{x{"1", "cookies"}, x{"9", "sweet-cookies"}} | |
for _, data := range values { | |
index.Index(data.Level, data) | |
} | |
// now let's do term queries.. | |
// THIS WORKS: term query on level field but with short term value | |
levelQuery := bleve.NewTermQuery("1").SetField("Level") | |
levelReq := bleve.NewSearchRequest(levelQuery) | |
levelRslt, _ := index.Search(levelReq) | |
// count is 1 | |
fmt.Println(len(levelRslt.Hits)) | |
// THIS DO NOT WORK: term query on parent field but with longer term value | |
parentQuery := bleve.NewTermQuery("cookies").SetField("Parent") | |
parentReq := bleve.NewSearchRequest(parentQuery) | |
parentRslt, _ := index.Search(parentReq) | |
// count is 0 - why? | |
fmt.Println(len(parentRslt.Hits)) | |
// I could use MatchQuery but.. | |
// MatchQuery will return both results for 'cookies' since it's a match (not exact) query type | |
// I also could use BooleanQuery like so.. | |
// bleve.NewBooleanQuery( | |
// []bleve.Query{bleve.NewTermQuery("<level>").SetField("Level")}, | |
// []bleve.Query{bleve.NewMatchQuery("<parent>").SetField("Parent")}, | |
// nil) | |
// but I don;t always know the "level" value! | |
} | |
func indexCreate() bleve.Index { | |
kwFieldMapping := bleve.NewTextFieldMapping() | |
kwFieldMapping.Analyzer = "keyword" | |
docMapping := bleve.NewDocumentMapping() | |
docMapping.AddFieldMappingsAt("Level", kwFieldMapping) | |
docMapping.AddFieldMappingsAt("Parent", kwFieldMapping) | |
indexMapping := bleve.NewIndexMapping() | |
// this adds docMapping for type "Document" | |
// but bleve has no way to know that the documents | |
// you are indexing are of this type | |
// so it is using the default mapping | |
// which was not configured | |
//indexMapping.AddDocumentMapping("Document", docMapping) | |
// instead you could have set | |
indexMapping.DefaultMapping = docMapping | |
indexMapping.DefaultAnalyzer = "en" | |
index, _ := bleve.New("test.bleve", indexMapping) | |
return index | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment