Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6588: Fixed how ToChildBlockJoinQuery deals with acceptDocs.

Adrien Grand6e0d10d25793
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 0f3fdffe418..f8254ce4cd1 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -161,6 +161,9 @@ Bug fixes
* LUCENE-6591: Never write a negative vLong (Robert Muir, Ryan Ernst,
Adrien Grand, Mike McCandless)
+* LUCENE-6588: Fix how ToChildBlockJoinQuery deals with acceptDocs.
+ (Christoph Kaser via Adrien Grand)
+
Changes in Runtime Behavior
* LUCENE-6501: The subreader structure in ParallelCompositeReader
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 1b3505f5c36..6ca8f164823 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
@@ -20,6 +20,7 @@ package org.apache.lucene.search.join;
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.IndexReader;
@@ -142,10 +143,17 @@ public class ToChildBlockJoinQuery extends Query {
}
@Override
- public Explanation explain(LeafReaderContext reader, int doc) throws IOException {
- // TODO
- throw new UnsupportedOperationException(getClass().getName() +
- " cannot explain match on parent document");
+ public Explanation explain(LeafReaderContext context, int doc) throws IOException {
+ ToChildBlockJoinScorer scorer = (ToChildBlockJoinScorer) scorer(context, context.reader().getLiveDocs());
+ if (scorer != null && scorer.advance(doc) == doc) {
+ int parentDoc = scorer.getParentDoc();
+ return Explanation.match(
+ scorer.score(),
+ String.format(Locale.ROOT, "Score based on parent document %d", parentDoc + context.docBase),
+ parentWeight.explain(context, parentDoc)
+ );
+ }
+ return Explanation.noMatch("Not a match");
}
}
@@ -217,7 +225,17 @@ public class ToChildBlockJoinQuery extends Query {
}
if (acceptDocs != null && !acceptDocs.get(childDoc)) {
- continue nextChildDoc;
+ // find the first child that is accepted
+ while (true) {
+ if (childDoc+1 < parentDoc) {
+ childDoc++;
+ if (acceptDocs.get(childDoc))
+ break;
+ } else {
+ // no child for this parent doc matches acceptDocs
+ continue nextChildDoc;
+ }
+ }
}
if (childDoc < parentDoc) {
@@ -315,6 +333,10 @@ public class ToChildBlockJoinQuery extends Query {
public long cost() {
return parentScorer.cost();
}
+
+ int getParentDoc() {
+ return parentDoc;
+ }
}
@Override
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 dcee28f3d49..8a7e532874e 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
@@ -23,7 +23,6 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
-import java.util.Random;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
@@ -58,6 +57,7 @@ import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.NumericRangeQuery;
+import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.search.QueryWrapperFilter;
@@ -1616,4 +1616,81 @@ public class TestBlockJoin extends LuceneTestCase {
w.close();
dir.close();
}
+
+ //LUCENE-6588
+ // delete documents to simulate FilteredQuery applying a filter as acceptDocs
+ public void testParentScoringBug() throws Exception {
+ final Directory dir = newDirectory();
+ final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+ final List<Document> docs = new ArrayList<>();
+ docs.add(makeJob("java", 2007));
+ docs.add(makeJob("python", 2010));
+ docs.add(makeResume("Lisa", "United Kingdom"));
+ w.addDocuments(docs);
+
+ docs.clear();
+ docs.add(makeJob("java", 2006));
+ docs.add(makeJob("ruby", 2005));
+ docs.add(makeResume("Frank", "United States"));
+ w.addDocuments(docs);
+ w.deleteDocuments(new Term("skill", "java")); // delete the first child of every parent
+
+ IndexReader r = w.getReader();
+ w.close();
+ IndexSearcher s = newSearcher(r);
+
+ // Create a filter that defines "parent" documents in the index - in this case resumes
+ BitDocIdSetFilter parentsFilter = new BitDocIdSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("docType", "resume"))));
+ Query parentQuery = new PrefixQuery(new Term("country", "United"));
+
+ ToChildBlockJoinQuery toChildQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);
+
+ TopDocs hits = s.search(toChildQuery, 10);
+ assertEquals(hits.scoreDocs.length, 2);
+ for (int i = 0; i < hits.scoreDocs.length; i++) {
+ if (hits.scoreDocs[i].score == 0.0)
+ fail("Failed to calculate score for hit #"+i);
+ }
+
+ r.close();
+ dir.close();
+ }
+
+ public void testToChildBlockJoinQueryExplain() throws Exception {
+ final Directory dir = newDirectory();
+ final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
+
+ final List<Document> docs = new ArrayList<>();
+ docs.add(makeJob("java", 2007));
+ docs.add(makeJob("python", 2010));
+ docs.add(makeResume("Lisa", "United Kingdom"));
+ w.addDocuments(docs);
+
+ docs.clear();
+ docs.add(makeJob("java", 2006));
+ docs.add(makeJob("ruby", 2005));
+ docs.add(makeResume("Frank", "United States"));
+ w.addDocuments(docs);
+ w.deleteDocuments(new Term("skill", "java")); // delete the first child of every parent
+
+ IndexReader r = w.getReader();
+ w.close();
+ IndexSearcher s = newSearcher(r);
+
+ // Create a filter that defines "parent" documents in the index - in this case resumes
+ BitDocIdSetFilter parentsFilter = new BitDocIdSetCachingWrapperFilter(new QueryWrapperFilter(new TermQuery(new Term("docType", "resume"))));
+ Query parentQuery = new PrefixQuery(new Term("country", "United"));
+
+ ToChildBlockJoinQuery toChildQuery = new ToChildBlockJoinQuery(parentQuery, parentsFilter);
+
+ TopDocs hits = s.search(toChildQuery, 10);
+ assertEquals(hits.scoreDocs.length, 2);
+ for (int i = 0; i < hits.scoreDocs.length; i++) {
+ assertEquals(hits.scoreDocs[i].score, s.explain(toChildQuery, hits.scoreDocs[i].doc).getValue(), 0.01);
+ }
+
+ r.close();
+ dir.close();
+ }
}