func TestNegativeQueryBoost(t *testing.T) { Test(t, func(t *T) { q := search.NewTermQuery(index.NewTerm("foo", "bar")) q.SetBoost(-42) t.Assert(-42 == q.Boost()) directory := NewDirectory() 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) } 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") }) }
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) }