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

}
Example #2
0
func (rule *TestRuleSetupAndRestoreClassEnv) After() error {
	savedCodec := rule.savedCodec
	index.DefaultCodec = func() index.Codec { return savedCodec }
	util.SetDefaultInfoStream(rule.savedInfoStream)
	// if rule.savedLocale != nil {
	// 	SetDefaultLocale(rule.savedLocale)
	// }
	// if rule.savedTimeZone != nil {
	// 	SetDefaultTimeZone(rule.savedTimeZone)
	// }
	return nil
}
Example #3
0
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
}