diff --git a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java index 646b71dc8b6..ae448485cc1 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java +++ b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java @@ -51,7 +51,8 @@ final class DefaultIndexingChain extends DocConsumer { // Writes postings and term vectors: final TermsHash termsHash; - final StoredFieldsWriter storedFieldsWriter; + // lazy init: + private StoredFieldsWriter storedFieldsWriter; private int lastStoredDocID; // NOTE: I tried using Hash Map @@ -75,12 +76,16 @@ final class DefaultIndexingChain extends DocConsumer { this.docState = docWriter.docState; this.bytesUsed = docWriter.bytesUsed; - // Writes stored fields: - storedFieldsWriter = docWriter.codec.storedFieldsFormat().fieldsWriter(docWriter.directory, docWriter.getSegmentInfo(), IOContext.DEFAULT); - TermsHash termVectorsWriter = new TermVectorsConsumer(docWriter); termsHash = new FreqProxTermsWriter(docWriter, termVectorsWriter); } + + // TODO: can we remove this lazy-init / make cleaner / do it another way...? + private void initStoredFieldsWriter() throws IOException { + if (storedFieldsWriter == null) { + storedFieldsWriter = docWriter.codec.storedFieldsFormat().fieldsWriter(docWriter.directory, docWriter.getSegmentInfo(), IOContext.DEFAULT); + } + } @Override public void flush(SegmentWriteState state) throws IOException { @@ -92,6 +97,8 @@ final class DefaultIndexingChain extends DocConsumer { writeNorms(state); writeDocValues(state); + // its possible all docs hit non-aborting exceptions... + initStoredFieldsWriter(); fillStoredFields(numDocs); storedFieldsWriter.finish(state.fieldInfos, numDocs); storedFieldsWriter.close(); @@ -246,6 +253,7 @@ final class DefaultIndexingChain extends DocConsumer { private void startStoredFields() throws IOException { boolean success = false; try { + initStoredFieldsWriter(); storedFieldsWriter.startDocument(); success = true; } finally { diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java index bc6c6d0cc2a..3b973209f1a 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -2188,7 +2188,6 @@ public class TestIndexWriter extends LuceneTestCase { IOUtils.close(reader, dir); } - @AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/LUCENE-5611") public void testIterableThrowsException2() throws IOException { Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(