mirror of
https://github.com/apache/lucene.git
synced 2025-03-07 00:39:21 +00:00
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:
parent
ed7c78ce6d
commit
340d9d2f9c
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user