Skip to content

Commit

Permalink
Prevent parent field from added to existing index (#13016)
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 #12829
  • Loading branch information
s1monw committed Jan 17, 2024
1 parent 0aa8891 commit 28b9f2e
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 @@ -1272,7 +1278,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 @@ -4943,6 +4943,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 28b9f2e

Please sign in to comment.