public virtual void TestBinaryFields() { Directory dir = new RAMDirectory(); byte[] bin = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); for (int i = 0; i < 10; i++) { AddDoc(writer, "document number " + (i + 1)); AddDocumentWithFields(writer); AddDocumentWithDifferentFields(writer); AddDocumentWithTermVectorFields(writer); } writer.Close(); writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED); Document doc = new Document(); doc.Add(new Field("bin1", bin, Field.Store.YES)); doc.Add(new Field("junk", "junk text", Field.Store.NO, Field.Index.ANALYZED)); writer.AddDocument(doc); writer.Close(); IndexReader reader = IndexReader.Open(dir, false); doc = reader.Document(reader.MaxDoc - 1); Field[] fields = doc.GetFields("bin1"); Assert.IsNotNull(fields); Assert.AreEqual(1, fields.Length); Field b1 = fields[0]; Assert.IsTrue(b1.IsBinary); byte[] data1 = b1.GetBinaryValue(); Assert.AreEqual(bin.Length, b1.BinaryLength); for (int i = 0; i < bin.Length; i++) { Assert.AreEqual(bin[i], data1[i + b1.BinaryOffset]); } var lazyFields = Support.Compatibility.SetFactory.CreateHashSet<string>(); lazyFields.Add("bin1"); FieldSelector sel = new SetBasedFieldSelector(Support.Compatibility.SetFactory.CreateHashSet<string>(), lazyFields); doc = reader.Document(reader.MaxDoc - 1, sel); IFieldable[] fieldables = doc.GetFieldables("bin1"); Assert.IsNotNull(fieldables); Assert.AreEqual(1, fieldables.Length); IFieldable fb1 = fieldables[0]; Assert.IsTrue(fb1.IsBinary); Assert.AreEqual(bin.Length, fb1.BinaryLength); data1 = fb1.GetBinaryValue(); Assert.AreEqual(bin.Length, fb1.BinaryLength); for (int i = 0; i < bin.Length; i++) { Assert.AreEqual(bin[i], data1[i + fb1.BinaryOffset]); } reader.Close(); // force optimize writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED); writer.Optimize(); writer.Close(); reader = IndexReader.Open(dir, false); doc = reader.Document(reader.MaxDoc - 1); fields = doc.GetFields("bin1"); Assert.IsNotNull(fields); Assert.AreEqual(1, fields.Length); b1 = fields[0]; Assert.IsTrue(b1.IsBinary); data1 = b1.GetBinaryValue(); Assert.AreEqual(bin.Length, b1.BinaryLength); for (int i = 0; i < bin.Length; i++) { Assert.AreEqual(bin[i], data1[i + b1.BinaryOffset]); } reader.Close(); }