From 30da7dabe09f81802a4ae9161c2c24ab03ff8d0f Mon Sep 17 00:00:00 2001 From: Michael Sokolov Date: Mon, 6 May 2024 12:53:13 -0400 Subject: [PATCH] gh-13340: Allow adding a parent field to an index with no fields (#13341) --- .../org/apache/lucene/index/IndexWriter.java | 1 + .../apache/lucene/index/TestIndexWriter.java | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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 7227ed56f34..3cbf433e174 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -1122,6 +1122,7 @@ public class IndexWriter globalFieldNumberMap = getFieldNumberMap(); if (create == false && conf.getParentField() != null + && globalFieldNumberMap.getFieldNames().isEmpty() == false && globalFieldNumberMap.getFieldNames().contains(conf.getParentField()) == false) { throw new IllegalArgumentException( "can't add a parent field to an already existing index without a parent field"); 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 4cee1635c42..163bb21d67d 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -4851,7 +4851,9 @@ public class TestIndexWriter extends LuceneTestCase { try (Directory dir = newDirectory()) { IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random())); try (IndexWriter writer = new IndexWriter(dir, iwc)) { - writer.addDocument(new Document()); + Document d = new Document(); + d.add(new TextField("f", "a", Field.Store.NO)); + writer.addDocument(d); } IllegalArgumentException iae = expectThrows( @@ -4959,4 +4961,19 @@ public class TestIndexWriter extends LuceneTestCase { iae.getMessage()); } } + + public void testParentFieldEmptyIndex() throws IOException { + try (Directory dir = newMockDirectory()) { + IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random())); + iwc.setParentField("parent"); + try (IndexWriter writer = new IndexWriter(dir, iwc)) { + writer.commit(); + } + IndexWriterConfig iwc2 = new IndexWriterConfig(new MockAnalyzer(random())); + iwc2.setParentField("parent"); + try (IndexWriter writer = new IndexWriter(dir, iwc2)) { + writer.commit(); + } + } + } }