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 ff48cb1d857..461db434e7f 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java +++ b/lucene/core/src/java/org/apache/lucene/index/DefaultIndexingChain.java @@ -550,7 +550,9 @@ final class DefaultIndexingChain extends DocConsumer { fieldInfo.setNormValueType(FieldInfo.DocValuesType.NUMERIC); norms = new NormValuesWriter(fieldInfo, docState.docWriter.bytesUsed); } - norms.addValue(docState.docID, similarity.computeNorm(invertState)); + if (invertState.length != 0) { + norms.addValue(docState.docID, similarity.computeNorm(invertState)); + } } termsHashPerField.finish(); 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 0de4e4e4829..4a354412d52 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -33,6 +33,7 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.CannedTokenStream; import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockTokenFilter; import org.apache.lucene.analysis.MockTokenizer; @@ -2798,5 +2799,19 @@ public class TestIndexWriter extends LuceneTestCase { ids.add(id); } } + + public void testEmptyNorm() throws Exception { + Directory d = newDirectory(); + IndexWriter w = new IndexWriter(d, newIndexWriterConfig(new MockAnalyzer(random()))); + Document doc = new Document(); + doc.add(new TextField("foo", new CannedTokenStream())); + w.addDocument(doc); + w.commit(); + w.close(); + DirectoryReader r = DirectoryReader.open(d); + assertEquals(0, getOnlySegmentReader(r).getNormValues("foo").get(0)); + r.close(); + d.close(); + } }