Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 1518581) +++ lucene/CHANGES.txt (working copy) @@ -149,6 +149,9 @@ implementing end() then be sure it calls super.end(). (Robert Muir, Mike McCandless) +* LUCENE-5192: IndexWriter could allow adding same field name with different + DocValueTypes under some circumstances. (Shai Erera) + API Changes * LUCENE-5094: Add ramBytesUsed() to MultiDocValues.OrdinalMap. Index: lucene/core/src/java/org/apache/lucene/index/FieldInfos.java =================================================================== --- lucene/core/src/java/org/apache/lucene/index/FieldInfos.java (revision 1518581) +++ lucene/core/src/java/org/apache/lucene/index/FieldInfos.java (working copy) @@ -288,6 +288,8 @@ if (docValues != null) { fi.setDocValuesType(docValues); + // must also update docValuesType map so it's aware of this field's DocValueType + globalFieldNumbers.docValuesType.put(name, docValues); } if (!fi.omitsNorms() && normType != null) { Index: lucene/core/src/test/org/apache/lucene/index/TestDocValuesIndexing.java =================================================================== --- lucene/core/src/test/org/apache/lucene/index/TestDocValuesIndexing.java (revision 1518581) +++ lucene/core/src/test/org/apache/lucene/index/TestDocValuesIndexing.java (working copy) @@ -26,9 +26,11 @@ import org.apache.lucene.document.BinaryDocValuesField; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.document.Field.Store; import org.apache.lucene.document.NumericDocValuesField; import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.document.SortedSetDocValuesField; +import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.search.FieldCache; import org.apache.lucene.store.Directory; @@ -751,4 +753,30 @@ dir.close(); } + public void testSameFieldNameForPostingAndDocValue() throws Exception { + // LUCENE-5192: FieldInfos.Builder neglected to update + // globalFieldNumbers.docValuesType map if the field existed, resulting in + // potentially adding the same field with different DV types. + Directory dir = newDirectory(); + IndexWriterConfig conf = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())); + IndexWriter writer = new IndexWriter(dir, conf); + + Document doc = new Document(); + doc.add(new StringField("f", "mock-value", Store.NO)); + doc.add(new NumericDocValuesField("f", 5)); + writer.addDocument(doc); + writer.commit(); + + doc = new Document(); + doc.add(new BinaryDocValuesField("f", new BytesRef("mock"))); + try { + writer.addDocument(doc); + fail("should not have succeeded to add a field with different DV type than what already exists"); + } catch (IllegalArgumentException e) { + writer.rollback(); + } + + dir.close(); + } + }