Add getMatchingChildren() method to Scorer

This commit is contained in:
Alan Woodward 2017-01-14 09:08:02 +00:00
parent 8fa0a8dd1e
commit 9403372fbc
5 changed files with 62 additions and 0 deletions

View File

@ -130,6 +130,10 @@ New features
SortedSetDocValues to allow filtering their TermsEnums with a SortedSetDocValues to allow filtering their TermsEnums with a
CompiledAutomaton (Alan Woodward, Mike McCandless) CompiledAutomaton (Alan Woodward, Mike McCandless)
* LUCENE-7628: Scorer now has a getMatchingChildren() method that will
return all child scorers positioned on the current document. (Alan
Woodward)
Bug Fixes Bug Fixes
* LUCENE-7547: JapaneseTokenizerFactory was failing to close the * LUCENE-7547: JapaneseTokenizerFactory was failing to close the

View File

@ -202,4 +202,12 @@ abstract class DisjunctionScorer extends Scorer {
return children; return children;
} }
@Override
public Collection<ChildScorer> getMatchingChildren() throws IOException {
List<ChildScorer> children = new ArrayList<>();
for (DisiWrapper w = getSubMatches(); w != null; w = w.next) {
children.add(new ChildScorer(w.scorer, "SHOULD"));
}
return children;
}
} }

View File

@ -132,6 +132,15 @@ final class MinShouldMatchSumScorer extends Scorer {
return childScorers; return childScorers;
} }
@Override
public Collection<ChildScorer> getMatchingChildren() throws IOException {
List<ChildScorer> children = new ArrayList<>();
for (DisiWrapper s = lead; s != null; s = s.next) {
children.add(new ChildScorer(s.scorer, "SHOULD"));
}
return children;
}
@Override @Override
public DocIdSetIterator iterator() { public DocIdSetIterator iterator() {
return new DocIdSetIterator() { return new DocIdSetIterator() {

View File

@ -83,6 +83,14 @@ public abstract class Scorer {
return Collections.emptyList(); return Collections.emptyList();
} }
/**
* Returns child sub-scorers that match the current document
* @lucene.experimental
*/
public Collection<ChildScorer> getMatchingChildren() throws IOException {
return getChildren();
}
/** A child Scorer and its relationship to its parent. /** A child Scorer and its relationship to its parent.
* the meaning of the relationship depends upon the parent query. * the meaning of the relationship depends upon the parent query.
* @lucene.experimental */ * @lucene.experimental */

View File

@ -109,6 +109,39 @@ public class TestBooleanQueryVisitSubscorers extends LuceneTestCase {
assertEquals(2, tfs.get(2).intValue()); // f2:search + f2:lucene assertEquals(2, tfs.get(2).intValue()); // f2:search + f2:lucene
} }
public void testDisjunctionMatches() throws IOException {
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
bq1.add(new TermQuery(new Term(F1, "lucene")), Occur.SHOULD);
bq1.add(new PhraseQuery(F2, "search", "engine"), Occur.SHOULD);
Weight w1 = scorerSearcher.createNormalizedWeight(bq1.build(), true);
Scorer s1 = w1.scorer(reader.leaves().get(0));
assertEquals(0, s1.iterator().nextDoc());
assertEquals(2, s1.getMatchingChildren().size());
BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
bq2.add(new TermQuery(new Term(F1, "lucene")), Occur.SHOULD);
bq2.add(new PhraseQuery(F2, "search", "library"), Occur.SHOULD);
Weight w2 = scorerSearcher.createNormalizedWeight(bq2.build(), true);
Scorer s2 = w2.scorer(reader.leaves().get(0));
assertEquals(0, s2.iterator().nextDoc());
assertEquals(1, s2.getMatchingChildren().size());
}
public void testMinShouldMatchMatches() throws IOException {
BooleanQuery.Builder bq = new BooleanQuery.Builder();
bq.add(new TermQuery(new Term(F1, "lucene")), Occur.SHOULD);
bq.add(new TermQuery(new Term(F2, "lucene")), Occur.SHOULD);
bq.add(new PhraseQuery(F2, "search", "library"), Occur.SHOULD);
bq.setMinimumNumberShouldMatch(2);
Weight w = scorerSearcher.createNormalizedWeight(bq.build(), true);
Scorer s = w.scorer(reader.leaves().get(0));
assertEquals(0, s.iterator().nextDoc());
assertEquals(2, s.getMatchingChildren().size());
}
public void testConjunctions() throws IOException { public void testConjunctions() throws IOException {
BooleanQuery.Builder bq = new BooleanQuery.Builder(); BooleanQuery.Builder bq = new BooleanQuery.Builder();
bq.add(new TermQuery(new Term(F2, "lucene")), BooleanClause.Occur.MUST); bq.add(new TermQuery(new Term(F2, "lucene")), BooleanClause.Occur.MUST);