Prevent parent field from added to existing index (#13016)

This change prevents users from adding a parent field to an existing index.
Parent field must be added before any documents are added to the index to
prevent documents without the parent field from being indexed and later
to be treated as child documents upon merge.

Relates to #12829
This commit is contained in:
Simon Willnauer 2024-01-16 10:53:43 +01:00 committed by GitHub
parent ed7c78ce6d
commit 340d9d2f9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -1121,6 +1121,12 @@ public class IndexWriter
// 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 @@ public class IndexWriter
map.addOrGet(fi);
}
}
return map;
}

View File

@ -4843,6 +4843,48 @@ public class TestIndexWriter extends LuceneTestCase {
}
}
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()));