Beispiel #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")
	})
}
Beispiel #2
0
func TestKeywordSearch(t *testing.T) {
	d, err := store.OpenFSDirectory("testdata/belfrysample")
	if err != nil {
		t.Error(err)
	}
	r, err := index.OpenDirectoryReader(d)
	if err != nil {
		t.Error(err)
	}
	if r == nil {
		t.Error("DirectoryReader cannot be opened.")
	}
	if len(r.Leaves()) < 1 {
		t.Error("Should have one leaf.")
	}
	ss := NewIndexSearcher(r)
	docs, err := ss.SearchTop(NewTermQuery(index.NewTerm("content", "bat")), 10)
	if err != nil {
		t.Error(err)
	}

	assertEquals(t, 8, docs.TotalHits)
	doc, err := r.Document(docs.ScoreDocs[0].Doc)
	if err != nil {
		t.Error(err)
	}
	assertEquals(t, "Bat recycling", doc.Get("title"))
}
Beispiel #3
0
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"))
	}

}
Beispiel #4
0
func main() {
	log.Print("Oepning FSDirectory...")
	// path := "src/github.com/balzaczyy/golucene/search/testdata/win8/belfrysample"
	path := "core/search/testdata/win8/belfrysample"
	// path := "/private/tmp/kc/index/belfrysample"
	// path := "c:/tmp/kc/index/belfrysample"
	d, err := store.OpenFSDirectory(path)
	if err != nil {
		panic(err)
	}
	log.Print("Opening DirectoryReader...")
	r, err := index.OpenDirectoryReader(d)
	if err != nil {
		panic(err)
	}
	if r == nil {
		panic("DirectoryReader cannot be opened.")
	}
	if len(r.Leaves()) < 1 {
		panic("Should have one leaf.")
	}
	log.Print("Initializing IndexSearcher...")
	ss := search.NewIndexSearcher(r)
	log.Print("Searching...")
	docs, err := ss.SearchTop(search.NewTermQuery(index.NewTerm("content", "bat")), 10)
	if err != nil {
		panic(err)
	}
	log.Println("Hits:", docs.TotalHits)
	doc, err := r.Document(docs.ScoreDocs[0].Doc)
	if err != nil {
		panic(err)
	}
	log.Println("Hit 1's title: ", doc.Get("title"))
}
Beispiel #5
0
func TestBasicIndexAndSearch(t *testing.T) {
	q := search.NewTermQuery(index.NewTerm("foo", "bar"))
	q.SetBoost(-42)

	os.RemoveAll(".gltest")

	directory, err := store.OpenFSDirectory(".gltest")
	It(t).Should("has no error: %v", err).Assert(err == nil)
	It(t).Should("has valid directory").Assert(directory != nil)
	fmt.Println("Directory", directory)
	defer directory.Close()

	analyzer := std.NewStandardAnalyzer()
	conf := index.NewIndexWriterConfig(util.VERSION_LATEST, analyzer)

	writer, err := index.NewIndexWriter(directory, conf)
	It(t).Should("has no error: %v", err).Assert(err == nil)

	d := docu.NewDocument()
	d.Add(docu.NewTextFieldFromString("foo", "bar", docu.STORE_YES))
	err = writer.AddDocument(d.Fields())
	It(t).Should("has no error: %v", err).Assert(err == nil)
	err = writer.Close() // ensure index is written
	It(t).Should("has no error: %v", err).Assert(err == nil)

	reader, err := index.OpenDirectoryReader(directory)
	It(t).Should("has no error: %v", err).Assert(err == nil)
	defer reader.Close()

	searcher := search.NewIndexSearcher(reader)
	res, err := searcher.Search(q, nil, 1000)
	It(t).Should("has no error: %v", err).Assert(err == nil)
	hits := res.ScoreDocs
	It(t).Should("expect 1 hits, but %v only.", len(hits)).Assert(len(hits) == 1)
	It(t).Should("expect score to be negative (got %v)", hits[0].Score).Verify(hits[0].Score < 0)

	explain, err := searcher.Explain(q, hits[0].Doc)
	It(t).Should("has no error: %v", err).Assert(err == nil)
	It(t).Should("score doesn't match explanation (%v vs %v)", hits[0].Score, explain.Value()).Verify(isSimilar(hits[0].Score, explain.Value(), 0.001))
	It(t).Should("explain doesn't think doc is a match").Verify(explain.IsMatch())
}
Beispiel #6
0
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)
}