Lucene.Net.Index.TestNumericDocValuesUpdates.TestUpdateSegmentWithNoDocValues C# (CSharp) Method

TestUpdateSegmentWithNoDocValues() private method

private TestUpdateSegmentWithNoDocValues ( ) : void
return void
        public virtual void TestUpdateSegmentWithNoDocValues()
        {
            Directory dir = NewDirectory();
            IndexWriterConfig conf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));
            // prevent merges, otherwise by the time updates are applied
            // (writer.Dispose()), the segments might have merged and that update becomes
            // legit.
            conf.SetMergePolicy(NoMergePolicy.COMPOUND_FILES);
            IndexWriter writer = new IndexWriter(dir, conf);

            // first segment with NDV
            Document doc = new Document();
            doc.Add(new StringField("id", "doc0", Store.NO));
            doc.Add(new NumericDocValuesField("ndv", 3));
            writer.AddDocument(doc);
            doc = new Document();
            doc.Add(new StringField("id", "doc4", Store.NO)); // document without 'ndv' field
            writer.AddDocument(doc);
            writer.Commit();

            // second segment with no NDV
            doc = new Document();
            doc.Add(new StringField("id", "doc1", Store.NO));
            writer.AddDocument(doc);
            doc = new Document();
            doc.Add(new StringField("id", "doc2", Store.NO)); // document that isn't updated
            writer.AddDocument(doc);
            writer.Commit();

            // update document in the first segment - should not affect docsWithField of
            // the document without NDV field
            writer.UpdateNumericDocValue(new Term("id", "doc0"), "ndv", 5L);

            // update document in the second segment - field should be added and we should
            // be able to handle the other document correctly (e.g. no NPE)
            writer.UpdateNumericDocValue(new Term("id", "doc1"), "ndv", 5L);
            writer.Dispose();

            DirectoryReader reader = DirectoryReader.Open(dir);
            foreach (AtomicReaderContext context in reader.Leaves)
            {
                AtomicReader r = context.AtomicReader;
                NumericDocValues ndv = r.GetNumericDocValues("ndv");
                Bits docsWithField = r.GetDocsWithField("ndv");
                Assert.IsNotNull(docsWithField);
                Assert.IsTrue(docsWithField.Get(0));
                Assert.AreEqual(5L, ndv.Get(0));
                Assert.IsFalse(docsWithField.Get(1));
                Assert.AreEqual(0L, ndv.Get(1));
            }
            reader.Dispose();

            dir.Dispose();
        }