Skip to content

Apache Lucene open-source search software

LUCENE-5520: fix AIOOBE from ToChildBlockJoinQuery when a parent has no children

Michael McCandlesse659fc8930dc
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 0988cda42cc..5c4c1f2f7cb 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -180,6 +180,10 @@ Bug fixes
* LUCENE-5522: FacetsConfig didn't add drill-down terms for association facet
fields labels. (Shai Erera)
+* LUCENE-5520: ToChildBlockJoinQuery would hit
+ ArrayIndexOutOfBoundsException if a parent document had no children
+ (Sally Ang via Mike McCandless)
+
Test Framework
* LUCENE-5449: Rename _TestUtil and _TestHelper to remove the leading _.
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
index 180d3107837..c555c4acf41 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
@@ -207,7 +207,7 @@ public class ToChildBlockJoinQuery extends Query {
validateParentDoc();
if (parentDoc == 0) {
- // Degenerate but allowed: parent has no children
+ // Degenerate but allowed: first parent doc has no children
// TODO: would be nice to pull initial parent
// into ctor so we can skip this if... but it's
// tricky because scorer must return -1 for
@@ -222,8 +222,15 @@ public class ToChildBlockJoinQuery extends Query {
return childDoc;
}
+ // Go to first child for this next parentDoc:
childDoc = 1 + parentBits.prevSetBit(parentDoc-1);
+ if (childDoc == parentDoc) {
+ // This parent has no children; continue
+ // parent loop so we move to next parent
+ continue;
+ }
+
if (acceptDocs != null && !acceptDocs.get(childDoc)) {
continue nextChildDoc;
}
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java
index 147dcb773e0..29eef0e941e 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java
@@ -1439,4 +1439,48 @@ public class TestBlockJoin extends LuceneTestCase {
r.close();
d.close();
}
+
+ public void testAdvanceSingleDeletedParentNoChild() throws Exception {
+
+ final Directory dir = newDirectory();
+ final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+ // First doc with 1 children
+ Document parentDoc = new Document();
+ parentDoc.add(newStringField("parent", "1", Field.Store.NO));
+ parentDoc.add(newStringField("isparent", "yes", Field.Store.NO));
+ Document childDoc = new Document();
+ childDoc.add(newStringField("child", "1", Field.Store.NO));
+ w.addDocuments(Arrays.asList(childDoc, parentDoc));
+
+ parentDoc = new Document();
+ parentDoc.add(newStringField("parent", "2", Field.Store.NO));
+ parentDoc.add(newStringField("isparent", "yes", Field.Store.NO));
+ w.addDocuments(Arrays.asList(parentDoc));
+
+ w.deleteDocuments(new Term("parent", "2"));
+
+ parentDoc = new Document();
+ parentDoc.add(newStringField("parent", "2", Field.Store.NO));
+ parentDoc.add(newStringField("isparent", "yes", Field.Store.NO));
+ childDoc = new Document();
+ childDoc.add(newStringField("child", "2", Field.Store.NO));
+ w.addDocuments(Arrays.asList(childDoc, parentDoc));
+
+ IndexReader r = w.getReader();
+ w.close();
+ IndexSearcher s = newSearcher(r);
+
+ // Create a filter that defines "parent" documents in the index - in this case resumes
+ Filter parentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("isparent", "yes"))));
+
+ Query parentQuery = new TermQuery(new Term("parent", "2"));
+
+ ToChildBlockJoinQuery parentJoinQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter, random().nextBoolean());
+ TopDocs topdocs = s.search(parentJoinQuery, 3);
+ assertEquals(1, topdocs.totalHits);
+
+ r.close();
+ dir.close();
+ }
}