Index: lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java (date 1379444024000) +++ lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoin.java (revision ) @@ -99,7 +99,7 @@ childQuery.add(new BooleanClause(new TermQuery(new Term("skill", "java")), Occur.MUST)); childQuery.add(new BooleanClause(NumericRangeQuery.newIntRange("year", 2006, 2011, true, true), Occur.MUST)); - ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg, true); BooleanQuery fullQuery = new BooleanQuery(); fullQuery.add(new BooleanClause(childJoinQuery, Occur.MUST)); @@ -157,7 +157,7 @@ // Wrap the child document query to 'join' any matches // up to corresponding parent: - ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg, true); // Combine the parent and nested child queries into a single query for a candidate BooleanQuery fullQuery = new BooleanQuery(); @@ -622,7 +622,7 @@ agg = ScoreMode.Avg; } - final ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, agg); + final ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, agg, true); // To run against the block-join index: final Query parentJoinQuery; @@ -1008,8 +1008,8 @@ // Wrap the child document query to 'join' any matches // up to corresponding parent: - ToParentBlockJoinQuery childJobJoinQuery = new ToParentBlockJoinQuery(childJobQuery, parentsFilter, ScoreMode.Avg); - ToParentBlockJoinQuery childQualificationJoinQuery = new ToParentBlockJoinQuery(childQualificationQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJobJoinQuery = new ToParentBlockJoinQuery(childJobQuery, parentsFilter, ScoreMode.Avg, true); + ToParentBlockJoinQuery childQualificationJoinQuery = new ToParentBlockJoinQuery(childQualificationQuery, parentsFilter, ScoreMode.Avg, true); // Combine the parent and nested child queries into a single query for a candidate BooleanQuery fullQuery = new BooleanQuery(); @@ -1149,7 +1149,7 @@ // Wrap the child document query to 'join' any matches // up to corresponding parent: - ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg, true); ToParentBlockJoinCollector c = new ToParentBlockJoinCollector(Sort.RELEVANCE, 2, true, true); @@ -1245,7 +1245,7 @@ Query childQuery = new TermQuery(new Term("childText", "text")); Filter parentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("isParent", "yes")))); - ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg, true); BooleanQuery parentQuery = new BooleanQuery(); parentQuery.add(childJoinQuery, Occur.SHOULD); parentQuery.add(new TermQuery(new Term("parentText", "text")), Occur.SHOULD); @@ -1311,7 +1311,7 @@ // never matches: Query childQuery = new TermQuery(new Term("childText", "bogus")); Filter parentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("isParent", "yes")))); - ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg, true); BooleanQuery parentQuery = new BooleanQuery(); parentQuery.add(childJoinQuery, Occur.SHOULD); parentQuery.add(new TermQuery(new Term("parentText", "text")), Occur.SHOULD); @@ -1377,7 +1377,7 @@ // illegally matches parent: Query childQuery = new TermQuery(new Term("parentText", "text")); Filter parentsFilter = new FixedBitSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("isParent", "yes")))); - ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg); + ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, ScoreMode.Avg, true); BooleanQuery parentQuery = new BooleanQuery(); parentQuery.add(childJoinQuery, Occur.SHOULD); parentQuery.add(new TermQuery(new Term("parentText", "text")), Occur.SHOULD); Index: lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java (date 1379444024000) +++ lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java (revision ) @@ -17,15 +17,9 @@ * limitations under the License. */ -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.Locale; -import java.util.Set; - import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; // javadocs +import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.ComplexExplanation; import org.apache.lucene.search.DocIdSet; @@ -41,6 +35,12 @@ import org.apache.lucene.util.Bits; import org.apache.lucene.util.FixedBitSet; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.Locale; +import java.util.Set; + /** * This query requires that you index * children and parent docs as a single block, using the @@ -95,6 +95,7 @@ // query before searching: private final Query origChildQuery; private final ScoreMode scoreMode; + private final boolean useJoinCollector; /** Create a ToParentBlockJoinQuery. * @@ -111,19 +112,30 @@ this.childQuery = childQuery; this.parentsFilter = parentsFilter; this.scoreMode = scoreMode; + this.useJoinCollector = false; } - private ToParentBlockJoinQuery(Query origChildQuery, Query childQuery, Filter parentsFilter, ScoreMode scoreMode) { + public ToParentBlockJoinQuery(Query childQuery, Filter parentsFilter, ScoreMode scoreMode, boolean useJoinCollector) { super(); + this.origChildQuery = childQuery; + this.childQuery = childQuery; + this.parentsFilter = parentsFilter; + this.scoreMode = scoreMode; + this.useJoinCollector = useJoinCollector; + } + + private ToParentBlockJoinQuery(Query origChildQuery, Query childQuery, Filter parentsFilter, ScoreMode scoreMode, boolean useJoinCollector) { + super(); this.origChildQuery = origChildQuery; this.childQuery = childQuery; this.parentsFilter = parentsFilter; this.scoreMode = scoreMode; + this.useJoinCollector = useJoinCollector; } @Override public Weight createWeight(IndexSearcher searcher) throws IOException { - return new BlockJoinWeight(this, childQuery.createWeight(searcher), parentsFilter, scoreMode); + return new BlockJoinWeight(this, childQuery.createWeight(searcher), parentsFilter, scoreMode, useJoinCollector); } private static class BlockJoinWeight extends Weight { @@ -131,13 +143,15 @@ private final Weight childWeight; private final Filter parentsFilter; private final ScoreMode scoreMode; + private final boolean useCollector; - public BlockJoinWeight(Query joinQuery, Weight childWeight, Filter parentsFilter, ScoreMode scoreMode) { + public BlockJoinWeight(Query joinQuery, Weight childWeight, Filter parentsFilter, ScoreMode scoreMode, boolean useCollector) { super(); this.joinQuery = joinQuery; this.childWeight = childWeight; this.parentsFilter = parentsFilter; this.scoreMode = scoreMode; + this.useCollector = useCollector; } @Override @@ -190,7 +204,7 @@ throw new IllegalStateException("parentFilter must return FixedBitSet; got " + parents); } - return new BlockJoinScorer(this, childScorer, (FixedBitSet) parents, firstChildDoc, scoreMode, acceptDocs); + return new BlockJoinScorer(this, childScorer, (FixedBitSet) parents, firstChildDoc, scoreMode, useCollector, acceptDocs); } @Override @@ -212,6 +226,7 @@ private final Scorer childScorer; private final FixedBitSet parentBits; private final ScoreMode scoreMode; + private final boolean useCollector; private final Bits acceptDocs; private int parentDoc = -1; private int prevParentDoc; @@ -219,18 +234,22 @@ private int parentFreq; private int nextChildDoc; - private int[] pendingChildDocs = new int[5]; + private int[] pendingChildDocs; private float[] pendingChildScores; private int childDocUpto; - public BlockJoinScorer(Weight weight, Scorer childScorer, FixedBitSet parentBits, int firstChildDoc, ScoreMode scoreMode, Bits acceptDocs) { + public BlockJoinScorer(Weight weight, Scorer childScorer, FixedBitSet parentBits, int firstChildDoc, ScoreMode scoreMode, boolean useCollector, Bits acceptDocs) { super(weight); //System.out.println("Q.init firstChildDoc=" + firstChildDoc); this.parentBits = parentBits; this.childScorer = childScorer; this.scoreMode = scoreMode; + this.useCollector = useCollector; this.acceptDocs = acceptDocs; - if (scoreMode != ScoreMode.None) { + if (useCollector) { + pendingChildDocs = new int[5]; + } + if (useCollector && scoreMode != ScoreMode.None) { pendingChildScores = new float[5]; } nextChildDoc = firstChildDoc; @@ -321,18 +340,22 @@ do { //System.out.println(" c=" + nextChildDoc); - if (pendingChildDocs.length == childDocUpto) { + if (useCollector && pendingChildDocs.length == childDocUpto) { pendingChildDocs = ArrayUtil.grow(pendingChildDocs); } - if (scoreMode != ScoreMode.None && pendingChildScores.length == childDocUpto) { + if (useCollector && scoreMode != ScoreMode.None && pendingChildScores.length == childDocUpto) { pendingChildScores = ArrayUtil.grow(pendingChildScores); } + if (useCollector) { - pendingChildDocs[childDocUpto] = nextChildDoc; + pendingChildDocs[childDocUpto] = nextChildDoc; + } if (scoreMode != ScoreMode.None) { // TODO: specialize this into dedicated classes per-scoreMode final float childScore = childScorer.score(); final int childFreq = childScorer.freq(); + if (useCollector) { - pendingChildScores[childDocUpto] = childScore; + pendingChildScores[childDocUpto] = childScore; + } maxScore = Math.max(childScore, maxScore); totalScore += childScore; parentFreq += childFreq; @@ -446,7 +469,8 @@ Query rewritten = new ToParentBlockJoinQuery(childQuery, childRewrite, parentsFilter, - scoreMode); + scoreMode, + useJoinCollector); rewritten.setBoost(getBoost()); return rewritten; } else {