Apache Lucene open-source search software
SOLR-3076: fix ToChildBlockJoinQuery to correctly apply acceptDocs per-child
Michael McCandlessdba31ba5a52e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex c0e40cc03ea..cb2f64ac841 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -708,10 +708,6 @@ Bug fixes* LUCENE-3589: BytesRef copy(short) didnt set length.(Peter Chang via Robert Muir)-* SOLR-3076: ToParent/ChildBlockJoinQuery was not handling an incoming- filter nor deleted docs correctly (Mikhail Khludnev via Mike- McCandless).-======================= Lucene 3.6.0 =======================Changes in backwards compatibility policydiff --git a/lucene/contrib/CHANGES.txt b/lucene/contrib/CHANGES.txtindex 20b8042e9cb..6e57df0839d 100644--- a/lucene/contrib/CHANGES.txt+++ b/lucene/contrib/CHANGES.txt@@ -248,6 +248,10 @@ Bug FixesSpanOrQuery with no clauses added). (Alan Woodward via MikeMcCandless).+ * SOLR-3076: ToParent/ChildBlockJoinQuery was not handling an incoming+ filter nor deleted docs correctly (Mikhail Khludnev via Mike+ McCandless).+Documentation* LUCENE-3599: Javadocs for DistanceUtils.haversine() were incorrectlydiff --git a/modules/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java b/modules/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.javaindex 8419cdb1743..3d89fcc102f 100644--- a/modules/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java+++ b/modules/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java@@ -183,6 +183,7 @@ public class ToChildBlockJoinQuery extends Query {//System.out.println("Q.nextDoc() parentDoc=" + parentDoc + " childDoc=" + childDoc);// Loop until we hit a childDoc that's accepted+ nextChildDoc:while (true) {if (childDoc+1 == parentDoc) {// OK, we are done iterating through all children@@ -192,6 +193,7 @@ public class ToChildBlockJoinQuery extends Query {// children:while (true) {parentDoc = parentScorer.nextDoc();+if (parentDoc == 0) {// Degenerate but allowed: parent has no children// TODO: would be nice to pull initial parent@@ -210,7 +212,7 @@ public class ToChildBlockJoinQuery extends Query {childDoc = 1 + parentBits.prevSetBit(parentDoc-1);if (acceptDocs != null && !acceptDocs.get(childDoc)) {- continue;+ continue nextChildDoc;}if (childDoc < parentDoc) {@@ -247,15 +249,16 @@ public class ToChildBlockJoinQuery extends Query {@Overridepublic int advance(int childTarget) throws IOException {-+ assert childTarget >= parentBits.length() || !parentBits.get(childTarget);+//System.out.println("Q.advance childTarget=" + childTarget);if (childTarget == NO_MORE_DOCS) {//System.out.println(" END");return childDoc = parentDoc = NO_MORE_DOCS;}- assert childTarget != parentDoc;- if (childTarget > parentDoc) {+ assert childDoc == -1 || childTarget != parentDoc: "childTarget=" + childTarget;+ if (childDoc == -1 || childTarget > parentDoc) {// Advance to new parent:parentDoc = parentScorer.advance(childTarget);//System.out.println(" advance to parentDoc=" + parentDoc);@@ -277,6 +280,9 @@ public class ToChildBlockJoinQuery extends Query {// Advance within children of current parent:childDoc = childTarget;//System.out.println(" " + childDoc);+ if (acceptDocs != null && !acceptDocs.get(childDoc)) {+ nextDoc();+ }return childDoc;}}diff --git a/modules/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java b/modules/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.javaindex 04b0e72be71..96cb1090b94 100644--- a/modules/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java+++ b/modules/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java@@ -312,6 +312,7 @@ public class ToParentBlockJoinQuery extends Query {childDocUpto = 0;do {+//System.out.println(" c=" + nextChildDoc);if (pendingChildDocs.length == childDocUpto) {pendingChildDocs = ArrayUtil.grow(pendingChildDocs);diff --git a/modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java b/modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.javaindex 076c0ba7ffe..ff3620ccfab 100644--- a/modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java+++ b/modules/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java@@ -30,6 +30,7 @@ import org.apache.lucene.util.*;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;+import java.util.Collections;import java.util.List;public class TestBlockJoin extends LuceneTestCase {@@ -149,23 +150,36 @@ public class TestBlockJoin extends LuceneTestCase {dir.close();}+ protected QueryWrapperFilter skill(String skill) {+ return new QueryWrapperFilter(new TermQuery(new Term("skill", skill)));+ }+public void testSimpleFilter() throws Exception {final Directory dir = newDirectory();final RandomIndexWriter w = new RandomIndexWriter(random, dir);final List<Document> docs = new ArrayList<Document>();-docs.add(makeJob("java", 2007));docs.add(makeJob("python", 2010));+ Collections.shuffle(docs, random);docs.add(makeResume("Lisa", "United Kingdom"));- w.addDocuments(docs);- docs.clear();- docs.add(makeJob("ruby", 2005));- docs.add(makeJob("java", 2006));- docs.add(makeResume("Frank", "United States"));- w.addDocuments(docs);+ final List<Document> docs2 = new ArrayList<Document>();+ docs2.add(makeJob("ruby", 2005));+ docs2.add(makeJob("java", 2006));+ Collections.shuffle(docs2, random);+ docs2.add(makeResume("Frank", "United States"));++ addSkillless(w);+ boolean turn = random.nextBoolean();+ w.addDocuments(turn ? docs:docs2);++ addSkillless(w);++ w.addDocuments(!turn ? docs:docs2);++ addSkillless(w);IndexReader r = w.getReader();w.close();@@ -204,9 +218,31 @@ public class TestBlockJoin extends LuceneTestCase {TopDocs usThen = s.search(childJoinQuery , new QueryWrapperFilter(new TermQuery(new Term("country", "United States"))), 1);assertEquals("has filter - single passed", 1, usThen.totalHits);assertEquals("Frank", r.document(usThen.scoreDocs[0].doc).get("name"));+++ TermQuery us = new TermQuery(new Term("country", "United States"));+ assertEquals("@ US we have java and ruby", 2,+ s.search(new ToChildBlockJoinQuery(us,+ parentsFilter, random.nextBoolean()), 10).totalHits );++ assertEquals("java skills in US", 1, s.search(new ToChildBlockJoinQuery(us, parentsFilter, random.nextBoolean()),+ skill("java"), 10).totalHits );++ BooleanQuery rubyPython = new BooleanQuery();+ rubyPython.add(new TermQuery(new Term("skill", "ruby")), Occur.SHOULD);+ rubyPython.add(new TermQuery(new Term("skill", "python")), Occur.SHOULD);+ assertEquals("ruby skills in US", 1, s.search(new ToChildBlockJoinQuery(us, parentsFilter, random.nextBoolean()),+ new QueryWrapperFilter(rubyPython), 10).totalHits );+r.close();dir.close();}++ private void addSkillless(final RandomIndexWriter w) throws IOException {+ if (random.nextBoolean()) {+ w.addDocument(makeResume("Skillless", random.nextBoolean() ? "United Kingdom":"United States"));+ }+ }private Document getParentDoc(IndexReader reader, Filter parents, int childDocID) throws IOException {final AtomicReaderContext[] leaves = reader.getTopReaderContext().leaves();@@ -654,45 +690,68 @@ public class TestBlockJoin extends LuceneTestCase {// the fully denormalized index (so we can compare// results):final Query childQuery2;++ // apply a filter to children+ final Filter childFilter2, childJoinFilter2;if (random.nextBoolean()) {childQuery2 = parentQuery2;childJoinQuery2 = parentJoinQuery2;+ childFilter2 = null;+ childJoinFilter2 = null;} else {- // AND child field w/ parent query:- final BooleanQuery bq = new BooleanQuery();- childJoinQuery2 = bq;final Term childTerm = randomChildTerm(childFields[0]);- if (random.nextBoolean()) {- bq.add(parentJoinQuery2, BooleanClause.Occur.MUST);- bq.add(new TermQuery(childTerm),- BooleanClause.Occur.MUST);+ if (random.nextBoolean()) { // filtered case+ childJoinQuery2 = parentJoinQuery2;+ final Filter f = new QueryWrapperFilter(new TermQuery(childTerm));+ childJoinFilter2 = random.nextBoolean()+ ? new CachingWrapperFilter(f): f;} else {- bq.add(new TermQuery(childTerm),- BooleanClause.Occur.MUST);- bq.add(parentJoinQuery2, BooleanClause.Occur.MUST);+ childJoinFilter2 = null;+ // AND child field w/ parent query:+ final BooleanQuery bq = new BooleanQuery();+ childJoinQuery2 = bq;+ if (random.nextBoolean()) {+ bq.add(parentJoinQuery2, BooleanClause.Occur.MUST);+ bq.add(new TermQuery(childTerm),+ BooleanClause.Occur.MUST);+ } else {+ bq.add(new TermQuery(childTerm),+ BooleanClause.Occur.MUST);+ bq.add(parentJoinQuery2, BooleanClause.Occur.MUST);+ }}-- final BooleanQuery bq2 = new BooleanQuery();- childQuery2 = bq2;- if (random.nextBoolean()) {- bq2.add(parentQuery2, BooleanClause.Occur.MUST);- bq2.add(new TermQuery(childTerm),- BooleanClause.Occur.MUST);++ if (random.nextBoolean()) { // filtered case+ childQuery2 = parentQuery2;+ final Filter f = new QueryWrapperFilter(new TermQuery(childTerm));+ childFilter2 = random.nextBoolean()+ ? new CachingWrapperFilter(f): f;} else {- bq2.add(new TermQuery(childTerm),- BooleanClause.Occur.MUST);- bq2.add(parentQuery2, BooleanClause.Occur.MUST);+ childFilter2 = null;+ final BooleanQuery bq2 = new BooleanQuery();+ childQuery2 = bq2;+ if (random.nextBoolean()) {+ bq2.add(parentQuery2, BooleanClause.Occur.MUST);+ bq2.add(new TermQuery(childTerm),+ BooleanClause.Occur.MUST);+ } else {+ bq2.add(new TermQuery(childTerm),+ BooleanClause.Occur.MUST);+ bq2.add(parentQuery2, BooleanClause.Occur.MUST);+ }}}final Sort childSort2 = getRandomSort("child", childFields.length);-+// Search denormalized index:if (VERBOSE) {- System.out.println("TEST: run top down query=" + childQuery2 + " sort=" + childSort2);+ System.out.println("TEST: run top down query=" + childQuery2 ++ " filter=" + childFilter2 ++ " sort=" + childSort2);}- final TopDocs results2 = s.search(childQuery2, null, r.numDocs(),+ final TopDocs results2 = s.search(childQuery2, childFilter2, r.numDocs(),childSort2);if (VERBOSE) {System.out.println(" " + results2.totalHits + " totalHits:");@@ -704,9 +763,10 @@ public class TestBlockJoin extends LuceneTestCase {// Search join index:if (VERBOSE) {- System.out.println("TEST: run top down join query=" + childJoinQuery2 + " sort=" + childSort2);+ System.out.println("TEST: run top down join query=" + childJoinQuery2 ++ " filter=" + childJoinFilter2 + " sort=" + childSort2);}- TopDocs joinResults2 = joinS.search(childJoinQuery2, null, joinR.numDocs(), childSort2);+ TopDocs joinResults2 = joinS.search(childJoinQuery2, childJoinFilter2, joinR.numDocs(), childSort2);if (VERBOSE) {System.out.println(" " + joinResults2.totalHits + " totalHits:");for(ScoreDoc sd : joinResults2.scoreDocs) {