LUCENE-4082: Added explain to ToParentBlockJoinQuery

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1350416 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Martijn van Groningen 2012-06-14 22:40:06 +00:00
parent 222fb0be9d
commit fb1581dbbc
3 changed files with 34 additions and 6 deletions

View File

@ -907,6 +907,9 @@ New features
* LUCENE-3440: Add ordered fragments feature with IDF-weighted terms for FVH.
(Sebastian Lutze via Koji Sekiguchi)
* LUCENE-4082: Added explain to ToParentBlockJoinQuery.
(Christoph Kaser, Martijn van Groningen)
Optimizations
* LUCENE-2588: Don't store unnecessary suffixes when writing the terms

View File

@ -26,6 +26,7 @@ import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; // javadocs
import org.apache.lucene.index.Term;
import org.apache.lucene.search.ComplexExplanation;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.Explanation;
@ -179,7 +180,8 @@ public class ToParentBlockJoinQuery extends Query {
// acceptDocs when we score:
final DocIdSet parents = parentsFilter.getDocIdSet(readerContext, null);
if (parents == null) {
if (parents == null
|| parents.iterator().docID() == DocIdSetIterator.NO_MORE_DOCS) { // <-- means DocIdSet#EMPTY_DOCIDSET
// No matches
return null;
}
@ -191,10 +193,14 @@ public class ToParentBlockJoinQuery extends Query {
}
@Override
public Explanation explain(AtomicReaderContext reader, int doc) throws IOException {
// TODO
throw new UnsupportedOperationException(getClass().getName() +
" cannot explain match on parent document");
public Explanation explain(AtomicReaderContext context, int doc) throws IOException {
BlockJoinScorer scorer = (BlockJoinScorer) scorer(context, true, false, context.reader().getLiveDocs());
if (scorer != null) {
if (scorer.advance(doc) == doc) {
return scorer.explain(context.docBase);
}
}
return new ComplexExplanation(false, 0.0f, "Not a match");
}
@Override
@ -209,6 +215,7 @@ public class ToParentBlockJoinQuery extends Query {
private final ScoreMode scoreMode;
private final Bits acceptDocs;
private int parentDoc = -1;
private int prevParentDoc;
private float parentScore;
private int nextChildDoc;
@ -365,7 +372,7 @@ public class ToParentBlockJoinQuery extends Query {
return nextDoc();
}
final int prevParentDoc = parentBits.prevSetBit(parentTarget-1);
prevParentDoc = parentBits.prevSetBit(parentTarget-1);
//System.out.println(" rolled back to prevParentDoc=" + prevParentDoc + " vs parentDoc=" + parentDoc);
assert prevParentDoc >= parentDoc;
@ -383,6 +390,15 @@ public class ToParentBlockJoinQuery extends Query {
//System.out.println(" return nextParentDoc=" + nd);
return nd;
}
public Explanation explain(int docBase) throws IOException {
int start = docBase + prevParentDoc + 1; // +1 b/c prevParentDoc is previous parent doc
int end = docBase + parentDoc - 1; // -1 b/c parentDoc is parent doc
return new ComplexExplanation(
true, score(), String.format("Score based on child doc range from %d to %d", start, end)
);
}
}
@Override

View File

@ -631,6 +631,15 @@ public class TestBlockJoin extends LuceneTestCase {
assertNull(joinResults);
} else {
compareHits(r, joinR, results, joinResults);
TopDocs b = joinS.search(childJoinQuery, 10);
for (ScoreDoc hit : b.scoreDocs) {
Explanation explanation = joinS.explain(childJoinQuery, hit.doc);
Document document = joinS.doc(hit.doc - 1);
int childId = Integer.parseInt(document.get("childID"));
assertTrue(explanation.isMatch());
assertEquals(hit.score, explanation.getValue(), 0.0f);
assertEquals(String.format("Score based on child doc range from %d to %d", hit.doc - 1 - childId, hit.doc - 1), explanation.getDescription());
}
}
// Test joining in the opposite direction (parent to