Example #1
1
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")
	})
}