public virtual void TestBinaryField() { Documents.Document doc = new Documents.Document(); FieldType ft = new FieldType(); ft.Stored = true; IndexableField stringFld = new Field("string", BinaryVal, ft); IndexableField binaryFld = new StoredField("binary", BinaryVal.GetBytes(Encoding.UTF8)); IndexableField binaryFld2 = new StoredField("binary", BinaryVal2.GetBytes(Encoding.UTF8)); doc.Add(stringFld); doc.Add(binaryFld); Assert.AreEqual(2, doc.Fields.Count); Assert.IsTrue(binaryFld.BinaryValue != null); Assert.IsTrue(binaryFld.FieldType.Stored); Assert.IsFalse(binaryFld.FieldType.Indexed); string binaryTest = doc.GetBinaryValue("binary").Utf8ToString(); Assert.IsTrue(binaryTest.Equals(BinaryVal)); string stringTest = doc.Get("string"); Assert.IsTrue(binaryTest.Equals(stringTest)); doc.Add(binaryFld2); Assert.AreEqual(3, doc.Fields.Count); BytesRef[] binaryTests = doc.GetBinaryValues("binary"); Assert.AreEqual(2, binaryTests.Length); binaryTest = binaryTests[0].Utf8ToString(); string binaryTest2 = binaryTests[1].Utf8ToString(); Assert.IsFalse(binaryTest.Equals(binaryTest2)); Assert.IsTrue(binaryTest.Equals(BinaryVal)); Assert.IsTrue(binaryTest2.Equals(BinaryVal2)); doc.RemoveField("string"); Assert.AreEqual(2, doc.Fields.Count); doc.RemoveFields("binary"); Assert.AreEqual(0, doc.Fields.Count); }
public virtual void TestSegmentMerges() { Directory dir = NewDirectory(); Random random = Random(); IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)); IndexWriter writer = new IndexWriter(dir, (IndexWriterConfig)conf.Clone()); int docid = 0; int numRounds = AtLeast(10); for (int rnd = 0; rnd < numRounds; rnd++) { Document doc = new Document(); doc.Add(new StringField("key", "doc", Store.NO)); doc.Add(new NumericDocValuesField("ndv", -1)); int numDocs = AtLeast(30); for (int i = 0; i < numDocs; i++) { doc.RemoveField("id"); doc.Add(new StringField("id", Convert.ToString(docid++), Store.NO)); writer.AddDocument(doc); } long value = rnd + 1; writer.UpdateNumericDocValue(new Term("key", "doc"), "ndv", value); if (random.NextDouble() < 0.2) // randomly delete some docs { writer.DeleteDocuments(new Term("id", Convert.ToString(random.Next(docid)))); } // randomly commit or reopen-IW (or nothing), before forceMerge if (random.NextDouble() < 0.4) { writer.Commit(); } else if (random.NextDouble() < 0.1) { writer.Dispose(); writer = new IndexWriter(dir, (IndexWriterConfig)conf.Clone()); } // add another document with the current value, to be sure forceMerge has // something to merge (for instance, it could be that CMS finished merging // all segments down to 1 before the delete was applied, so when // forceMerge is called, the index will be with one segment and deletes // and some MPs might now merge it, thereby invalidating test's // assumption that the reader has no deletes). doc = new Document(); doc.Add(new StringField("id", Convert.ToString(docid++), Store.NO)); doc.Add(new StringField("key", "doc", Store.NO)); doc.Add(new NumericDocValuesField("ndv", value)); writer.AddDocument(doc); writer.ForceMerge(1, true); DirectoryReader reader; if (random.NextBoolean()) { writer.Commit(); reader = DirectoryReader.Open(dir); } else { reader = DirectoryReader.Open(writer, true); } Assert.AreEqual(1, reader.Leaves.Count); AtomicReader r = (AtomicReader)reader.Leaves[0].Reader; Assert.IsNull(r.LiveDocs, "index should have no deletes after forceMerge"); NumericDocValues ndv = r.GetNumericDocValues("ndv"); Assert.IsNotNull(ndv); for (int i = 0; i < r.MaxDoc; i++) { Assert.AreEqual(value, ndv.Get(i)); } reader.Dispose(); } writer.Dispose(); dir.Dispose(); }