gh-13340: Allow adding a parent field to an index with no fields (#13341)

This commit is contained in:
Michael Sokolov 2024-05-06 12:53:13 -04:00 committed by GitHub
parent 40cae087f7
commit 30da7dabe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View File

@ -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");

View File

@ -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();
}
}
}
}