private static IndexReader Build(Random random, TestIndex index) { /* build an index */ Document doc = new Document(); Field idField = NewStringField(random, "id", "", Field.Store.YES); Field randField = NewStringField(random, "rand", "", Field.Store.YES); Field bodyField = NewStringField(random, "body", "", Field.Store.NO); doc.Add(idField); doc.Add(randField); doc.Add(bodyField); RandomIndexWriter writer = new RandomIndexWriter(random, index.Index, NewIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode.CREATE).SetMaxBufferedDocs(TestUtil.NextInt(random, 50, 1000)).SetMergePolicy(NewLogMergePolicy())); TestUtil.ReduceOpenFiles(writer.w); while (true) { int minCount = 0; int maxCount = 0; for (int d = MinId; d <= MaxId; d++) { idField.StringValue = Pad(d); int r = index.AllowNegativeRandomInts ? random.Next() : random.Next(int.MaxValue); if (index.MaxR < r) { index.MaxR = r; maxCount = 1; } else if (index.MaxR == r) { maxCount++; } if (r < index.MinR) { index.MinR = r; minCount = 1; } else if (r == index.MinR) { minCount++; } randField.StringValue = Pad(r); bodyField.StringValue = "body"; writer.AddDocument(doc); } if (minCount == 1 && maxCount == 1) { // our subclasses rely on only 1 doc having the min or // max, so, we loop until we satisfy that. it should be // exceedingly rare (Yonik calculates 1 in ~429,000) // times) that this loop requires more than one try: IndexReader ir = writer.Reader; writer.Dispose(); return(ir); } // try again writer.DeleteAll(); } }
private void PopulateSampleIndex(Analyzer analyzer) { indexWriter.DeleteAll(); indexWriter.Commit(); String text; Document doc = new Document(); text = "The traveling press secretary for Mitt Romney lost his cool and cursed at reporters " + "who attempted to ask questions of the Republican presidential candidate in a public plaza near the Tomb of " + "the Unknown Soldier in Warsaw Tuesday."; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "politics", ft)); doc.Add(new Field(booleanFieldName, "true", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "Mitt Romney seeks to assure Israel and Iran, as well as Jewish voters in the United" + " States, that he will be tougher against Iran's nuclear ambitions than President Barack Obama."; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "politics", ft)); doc.Add(new Field(booleanFieldName, "true", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "And there's a threshold question that he has to answer for the American people and " + "that's whether he is prepared to be commander-in-chief,\" she continued. \"As we look to the past events, we " + "know that this raises some questions about his preparedness and we'll see how the rest of his trip goes.\""; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "politics", ft)); doc.Add(new Field(booleanFieldName, "true", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "Still, when it comes to gun policy, many congressional Democrats have \"decided to " + "keep quiet and not go there,\" said Alan Lizotte, dean and professor at the State University of New York at " + "Albany's School of Criminal Justice."; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "politics", ft)); doc.Add(new Field(booleanFieldName, "true", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "Standing amongst the thousands of people at the state Capitol, Jorstad, director of " + "technology at the University of Wisconsin-La Crosse, documented the historic moment and shared it with the " + "world through the Internet."; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "technology", ft)); doc.Add(new Field(booleanFieldName, "false", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "So, about all those experts and analysts who've spent the past year or so saying " + "Facebook was going to make a phone. A new expert has stepped forward to say it's not going to happen."; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "technology", ft)); doc.Add(new Field(booleanFieldName, "false", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "More than 400 million people trust Google with their e-mail, and 50 million store files" + " in the cloud using the Dropbox service. People manage their bank accounts, pay bills, trade stocks and " + "generally transfer or store huge volumes of personal data online."; doc.Add(new Field(textFieldName, text, ft)); doc.Add(new Field(categoryFieldName, "technology", ft)); doc.Add(new Field(booleanFieldName, "false", ft)); indexWriter.AddDocument(doc, analyzer); doc = new Document(); text = "unlabeled doc"; doc.Add(new Field(textFieldName, text, ft)); indexWriter.AddDocument(doc, analyzer); indexWriter.Commit(); }
public virtual void TestDeleteAllNoDeadLock() { Directory dir = NewDirectory(); RandomIndexWriter modifier = new RandomIndexWriter(Random(), dir); int numThreads = AtLeast(2); ThreadClass[] threads = new ThreadClass[numThreads]; CountDownLatch latch = new CountDownLatch(1); CountDownLatch doneLatch = new CountDownLatch(numThreads); for (int i = 0; i < numThreads; i++) { int offset = i; threads[i] = new ThreadAnonymousInnerClassHelper(this, modifier, latch, doneLatch, offset); threads[i].Start(); } latch.countDown(); //Wait for 1 millisecond while (!doneLatch.@await(new TimeSpan(0, 0, 0, 0, 1))) { modifier.DeleteAll(); if (VERBOSE) { Console.WriteLine("del all"); } } modifier.DeleteAll(); foreach (ThreadClass thread in threads) { thread.Join(); } modifier.Dispose(); DirectoryReader reader = DirectoryReader.Open(dir); Assert.AreEqual(reader.MaxDoc, 0); Assert.AreEqual(reader.NumDocs, 0); Assert.AreEqual(reader.NumDeletedDocs, 0); reader.Dispose(); dir.Dispose(); }
/// <summary> /// LUCENENET specific /// Passed in because NewStringField and NewIndexWriterConfig are no /// longer static. /// </summary> private IndexReader Build(Random random, TestIndex index) { /* build an index */ Document doc = new Document(); Field idField = NewStringField(random, "id", "", Field.Store.YES); Field randField = NewStringField(random, "rand", "", Field.Store.YES); Field bodyField = NewStringField(random, "body", "", Field.Store.NO); doc.Add(idField); doc.Add(randField); doc.Add(bodyField); RandomIndexWriter writer = new RandomIndexWriter(random, index.Index, NewIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random)).SetOpenMode(OpenMode.CREATE).SetMaxBufferedDocs(TestUtil.NextInt(random, 50, 1000)).SetMergePolicy(NewLogMergePolicy())); TestUtil.ReduceOpenFiles(writer.w); while (true) { int minCount = 0; int maxCount = 0; for (int d = MinId; d <= MaxId; d++) { idField.StringValue = Pad(d); int r = index.AllowNegativeRandomInts ? random.Next() : random.Next(int.MaxValue); if (index.MaxR < r) { index.MaxR = r; maxCount = 1; } else if (index.MaxR == r) { maxCount++; } if (r < index.MinR) { index.MinR = r; minCount = 1; } else if (r == index.MinR) { minCount++; } randField.StringValue = Pad(r); bodyField.StringValue = "body"; writer.AddDocument(doc); } if (minCount == 1 && maxCount == 1) { // our subclasses rely on only 1 doc having the min or // max, so, we loop until we satisfy that. it should be // exceedingly rare (Yonik calculates 1 in ~429,000) // times) that this loop requires more than one try: IndexReader ir = writer.Reader; writer.Dispose(); return ir; } // try again writer.DeleteAll(); } }