diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java index 0c51450d2ff4..8c09cf98b9ef 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -1121,6 +1121,12 @@ public IndexWriter(Directory d, IndexWriterConfig conf) throws IOException { // NOTE: this is correct even for an NRT reader because we'll pull FieldInfos even for the // un-committed segments: globalFieldNumberMap = getFieldNumberMap(); + if (create == false + && conf.getParentField() != null + && globalFieldNumberMap.getFieldNames().contains(conf.getParentField()) == false) { + throw new IllegalArgumentException( + "can't add a parent field to an already existing index without a parent field"); + } validateIndexSort(); @@ -1270,7 +1276,6 @@ private FieldNumbers getFieldNumberMap() throws IOException { map.addOrGet(fi); } } - return map; } 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 7da560fdb402..cb00faeb8561 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -4843,6 +4843,48 @@ public void testParentAndSoftDeletesAreTheSame() throws IOException { } } + public void testParentFieldExistingIndex() throws IOException { + try (Directory dir = newDirectory()) { + IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random())); + try (IndexWriter writer = new IndexWriter(dir, iwc)) { + writer.addDocument(new Document()); + } + IllegalArgumentException iae = + expectThrows( + IllegalArgumentException.class, + () -> + new IndexWriter( + dir, + new IndexWriterConfig(new MockAnalyzer(random())) + .setOpenMode(OpenMode.APPEND) + .setParentField("foo"))); + assertEquals( + "can't add a parent field to an already existing index without a parent field", + iae.getMessage()); + iae = + expectThrows( + IllegalArgumentException.class, + () -> + new IndexWriter( + dir, + new IndexWriterConfig(new MockAnalyzer(random())) + .setOpenMode(OpenMode.CREATE_OR_APPEND) + .setParentField("foo"))); + assertEquals( + "can't add a parent field to an already existing index without a parent field", + iae.getMessage()); + + try (IndexWriter writer = + new IndexWriter( + dir, + new IndexWriterConfig(new MockAnalyzer(random())) + .setOpenMode(OpenMode.CREATE) + .setParentField("foo"))) { + writer.addDocument(new Document()); + } + } + } + public void testIndexWithParentFieldIsCongruent() throws IOException { try (Directory dir = newDirectory()) { IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));