func main() { util.SetDefaultInfoStream(util.NewPrintStreamInfoStream(os.Stdout)) index.DefaultSimilarity = func() index.Similarity { return search.NewDefaultSimilarity() } directory, _ := store.OpenFSDirectory("test_index") analyzer := std.NewStandardAnalyzer() conf := index.NewIndexWriterConfig(util.VERSION_LATEST, analyzer) writer, _ := index.NewIndexWriter(directory, conf) d := document.NewDocument() d.Add(document.NewTextFieldFromString("foo", "bar", document.STORE_YES)) writer.AddDocument(d.Fields()) writer.Close() // ensure index is written reader, _ := index.OpenDirectoryReader(directory) searcher := search.NewIndexSearcher(reader) q := search.NewTermQuery(index.NewTerm("foo", "bar")) res, _ := searcher.Search(q, nil, 1000) fmt.Printf("Found %v hit(s).\n", res.TotalHits) for _, hit := range res.ScoreDocs { fmt.Printf("Doc %v score: %v\n", hit.Doc, hit.Score) doc, _ := reader.Document(hit.Doc) fmt.Printf("foo -> %v\n", doc.Get("foo")) } }
// Hook up custom test logic into Go's test runner. func TestBefore(t *testing.T) { fmt.Printf("tests_codec: %v\n", os.Getenv("tests_codec")) index.DefaultSimilarity = func() index.Similarity { return search.NewDefaultSimilarity() } // This controls how suite-level rules are nested. It is important // that _all_ rules declared in testcase are executed in proper // order if they depend on each other. ClassRuleChain(ClassEnvRule) BeforeSuite(t) }
func (rule *TestRuleSetupAndRestoreClassEnv) Before() error { // if verbose: print some debugging stuff about which codecs are loaded. if VERBOSE { for _, codec := range index.AvailableCodecs() { log.Printf("Loaded codec: '%v': %v", codec, reflect.TypeOf(index.LoadCodec(codec))) } for _, postingFormat := range index.AvailablePostingsFormats() { log.Printf("Loaded postingsFormat: '%v': %v", postingFormat, reflect.TypeOf(index.LoadPostingsFormat(postingFormat))) } } rule.savedInfoStream = util.DefaultInfoStream() random := Random() if INFOSTREAM { util.SetDefaultInfoStream(newThreadNameFixingPrintStreamInfoStream(os.Stdout)) } else if random.Intn(2) == 0 { util.SetDefaultInfoStream(NewNullInfoStream()) } rule.avoidCodecs = make(map[string]bool) if suppressedCodecs != "" { rule.avoidCodecs[suppressedCodecs] = true } PREFLEX_IMPERSONATION_IS_ACTIVE = false rule.savedCodec = index.DefaultCodec() randomVal := random.Intn(10) if "Lucene3x" == TEST_CODEC || "random" == TEST_CODEC && "random" == TEST_POSTINGSFORMAT && "random" == TEST_DOCVALUESFORMAT && randomVal == 3 && !rule.shouldAvoidCodec("Lucene3x") { // preflex-only setup panic("not supported yet") } else if "Lucene40" == TEST_CODEC || "random" == TEST_CODEC && "random" == TEST_POSTINGSFORMAT && randomVal == 0 && !rule.shouldAvoidCodec("Lucene40") { // 4.0 setup panic("not supported yet") } else if "Lucene41" == TEST_CODEC || "random" == TEST_CODEC && "random" == TEST_POSTINGSFORMAT && "random" == TEST_DOCVALUESFORMAT && randomVal == 1 && !rule.shouldAvoidCodec("Lucene41") { panic("not supported yet") } else if "Lucene42" == TEST_CODEC || "random" == TEST_CODEC && "random" == TEST_POSTINGSFORMAT && "random" == TEST_DOCVALUESFORMAT && randomVal == 2 && !rule.shouldAvoidCodec("Lucene42") { panic("not supported yet") } else if "random" != TEST_POSTINGSFORMAT || "random" != TEST_DOCVALUESFORMAT { // the user wired postings or DV: this is messy // refactor into RandomCodec... panic("not supported yet") } else if "SimpleText" == TEST_CODEC || "random" == TEST_CODEC && randomVal == 9 && Rarely(random) && !rule.shouldAvoidCodec("SimpleText") { panic("not supported yet") } else if "Appending" == TEST_CODEC || "random" == TEST_CODEC && randomVal == 8 && !rule.shouldAvoidCodec("Appending") { panic("not supported yet") } else if "CheapBastard" == TEST_CODEC || "random" == TEST_CODEC && randomVal == 8 && !rule.shouldAvoidCodec("CheapBastard") && !rule.shouldAvoidCodec("Lucene41") { panic("not supported yet") } else if "Asserting" == TEST_CODEC || "random" == TEST_CODEC && randomVal == 6 && !rule.shouldAvoidCodec("Asserting") { panic("not implemented yet") } else if "Compressing" == TEST_CODEC || "random" == TEST_CODEC && randomVal == 5 && !rule.shouldAvoidCodec("Compressing") { panic("not supported yet") } else if "random" != TEST_CODEC { rule.codec = index.LoadCodec(TEST_CODEC) } else if "random" == TEST_POSTINGSFORMAT { panic("not supported yet") } else { assert(false) } log.Printf("Use codec: %v", rule.codec) index.DefaultCodec = func() index.Codec { return rule.codec } // Initialize locale/ timezone // testLocale := or(os.Getenv("tests.locale"), "random") // testTimeZon := or(os.Getenv("tests.timezone"), "random") // Always pick a random one for consistency (whether tests.locale // was specified or not) // Ian: it's not supported yet // rule.savedLocale := DefaultLocale() // if "random" == testLocale { // rule.locale = randomLocale(random) // } else { // rule.locale = localeForName(testLocale) // } // SetDefaultLocale(rule.locale) // SetDefaultTimeZone() will set user.timezone to the default // timezone of the user's locale. So store the original property // value and restore it at end. // rule.restoreProperties["user.timezone"] = os.Getenv("user.timezone") // rule.savedTimeZone = DefaultTimeZone() // if "random" == testTimeZone { // rule.timeZone = randomTimeZone(random) // } else { // rule.timeZone = TimeZone(testTimeZone) // } // SetDefaultTImeZone(rule.timeZone) if random.Intn(2) == 0 { rule.similarity = search.NewDefaultSimilarity() } else { rule.similarity = ts.NewRandomSimilarityProvider(random) } // Check codec restrictions once at class level. err := rule.checkCodecRestrictions(rule.codec) if err != nil { log.Printf("NOTE: %v Suppressed codecs: %v", err, rule.avoidCodecs) } return err }
func main() { fmt.Printf("tests_codec: %v\n", os.Getenv("tests_codec")) index.DefaultSimilarity = func() index.Similarity { return search.NewDefaultSimilarity() } // This controls how suite-level rules are nested. It is important // that _all_ rules declared in testcase are executed in proper // order if they depend on each other. ClassRuleChain(ClassEnvRule) BeforeSuite(nil) Test(nil, func(t *T) { q := search.NewTermQuery(index.NewTerm("foo", "bar")) q.SetBoost(-42) t.Assert(-42 == q.Boost()) directory := NewDirectory() fmt.Println("Directory", directory) defer directory.Close() analyzer := analysis.NewMockAnalyzerWithRandom(Random()) conf := NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer) writer, err := index.NewIndexWriter(directory, conf) if err != nil { t.Error(err) } if writer != nil { defer writer.Close() } // d := index.NewDocument() // d.Add(NewTextField("foo", "bar", true)) // writer.AddDocument(d.Fields()) // writer.Close() // ensure index is written // reader, err := index.OpenDirectoryReader(directory) // if err != nil { // t.Error(err) // } // defer reader.Close() // searcher := NewSearcher(reader) // res, err := searcher.Search(q, nil, 1000) // if err != nil { // t.Error(err) // } // hits := res.ScoreDocs // t.Assert(1 == len(hits)) // t.Assert2(hits[0].Score < 0, fmt.Sprintf("score is not negative: %v", hits[0].Score)) // explain, err := searcher.Explain(q, hits[0].Doc) // if err != nil { // t.Error(err) // } // t.Assert2(isSimilar(hits[0].Score, explain.Value(), 0.01), "score doesn't match explanation") // t.Assert2(explain.IsMatch(), "explain doesn't think doc is a match") }) AfterSuite(nil) }