Skip to content

Commit

Permalink
Prevent parent field from added to existing index
Browse files Browse the repository at this point in the history
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 apache#12829
  • Loading branch information
s1monw committed Jan 16, 2024
1 parent c746bea commit 9c29186
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -1270,7 +1276,6 @@ private FieldNumbers getFieldNumberMap() throws IOException {
map.addOrGet(fi);
}
}

return map;
}

Expand Down
42 changes: 42 additions & 0 deletions lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 9c29186

Please sign in to comment.