public override void SetUp() { base.SetUp(); Dir = NewDirectory(); FieldName = Random().NextBoolean() ? "field" : ""; // sometimes use an empty string as field name RandomIndexWriter writer = new RandomIndexWriter(Random(), Dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random(), MockTokenizer.KEYWORD, false)).SetMaxBufferedDocs(TestUtil.NextInt(Random(), 50, 1000))); List<string> terms = new List<string>(); int num = AtLeast(200); for (int i = 0; i < num; i++) { Document doc = new Document(); doc.Add(NewStringField("id", Convert.ToString(i), Field.Store.NO)); int numTerms = Random().Next(4); for (int j = 0; j < numTerms; j++) { string s = TestUtil.RandomUnicodeString(Random()); doc.Add(NewStringField(FieldName, s, Field.Store.NO)); // if the default codec doesn't support sortedset, we will uninvert at search time if (DefaultCodecSupportsSortedSet()) { doc.Add(new SortedSetDocValuesField(FieldName, new BytesRef(s))); } terms.Add(s); } writer.AddDocument(doc); } if (VERBOSE) { // utf16 order terms.Sort(); Console.WriteLine("UTF16 order:"); foreach (string s in terms) { Console.WriteLine(" " + UnicodeUtil.ToHexString(s)); } } int numDeletions = Random().Next(num / 10); for (int i = 0; i < numDeletions; i++) { writer.DeleteDocuments(new Term("id", Convert.ToString(Random().Next(num)))); } Reader = writer.Reader; Searcher1 = NewSearcher(Reader); Searcher2 = NewSearcher(Reader); writer.Dispose(); }
private int[] BuildIndex(RandomIndexWriter writer, int docs) { int[] docStates = new int[docs]; for (int i = 0; i < docs; i++) { Document doc = new Document(); if (Random().NextBoolean()) { docStates[i] = 1; doc.Add(NewTextField("some", "value", Field.Store.YES)); } doc.Add(NewTextField("all", "test", Field.Store.NO)); doc.Add(NewTextField("id", "" + i, Field.Store.YES)); writer.AddDocument(doc); } writer.Commit(); int numDeletes = Random().Next(docs); for (int i = 0; i < numDeletes; i++) { int docID = Random().Next(docs); writer.DeleteDocuments(new Term("id", "" + docID)); docStates[docID] = 2; } writer.Dispose(); return docStates; }
public void BeforeClass() { Random random = Random(); Directory = NewDirectory(); Stopword = "" + RandomChar(); CharacterRunAutomaton stopset = new CharacterRunAutomaton(BasicAutomata.MakeString(Stopword)); Analyzer = new MockAnalyzer(random, MockTokenizer.WHITESPACE, false, stopset); RandomIndexWriter iw = new RandomIndexWriter(random, Directory, Analyzer); Document doc = new Document(); Field id = new StringField("id", "", Field.Store.NO); Field field = new TextField("field", "", Field.Store.NO); doc.Add(id); doc.Add(field); // index some docs int numDocs = AtLeast(1000); for (int i = 0; i < numDocs; i++) { id.StringValue = Convert.ToString(i); field.StringValue = RandomFieldContents(); iw.AddDocument(doc); } // delete some docs int numDeletes = numDocs / 20; for (int i = 0; i < numDeletes; i++) { Term toDelete = new Term("id", Convert.ToString(random.Next(numDocs))); if (random.NextBoolean()) { iw.DeleteDocuments(toDelete); } else { iw.DeleteDocuments(new TermQuery(toDelete)); } } Reader = iw.Reader; S1 = NewSearcher(Reader); S2 = NewSearcher(Reader); iw.Dispose(); }
public virtual void TestRandom() { Directory d = NewDirectory(); RandomIndexWriter w = new RandomIndexWriter(Random(), d); w.w.Config.SetMaxBufferedDocs(17); int numDocs = AtLeast(100); HashSet<string> aDocs = new HashSet<string>(); for (int i = 0; i < numDocs; i++) { Document doc = new Document(); string v; if (Random().Next(5) == 4) { v = "a"; aDocs.Add("" + i); } else { v = "b"; } Field f = NewStringField("field", v, Field.Store.NO); doc.Add(f); doc.Add(NewStringField("id", "" + i, Field.Store.YES)); w.AddDocument(doc); } int numDelDocs = AtLeast(10); for (int i = 0; i < numDelDocs; i++) { string delID = "" + Random().Next(numDocs); w.DeleteDocuments(new Term("id", delID)); aDocs.Remove(delID); } IndexReader r = w.Reader; w.Dispose(); TopDocs hits = NewSearcher(r).Search(new MatchAllDocsQuery(), new QueryWrapperFilter(new TermQuery(new Term("field", "a"))), numDocs); Assert.AreEqual(aDocs.Count, hits.TotalHits); foreach (ScoreDoc sd in hits.ScoreDocs) { Assert.IsTrue(aDocs.Contains(r.Document(sd.Doc).Get("id"))); } r.Dispose(); d.Dispose(); }