Skip to content

Commit

Permalink
LUCENE-AZ-1744: don't create a LeafCollector when the Scorer for the …
Browse files Browse the repository at this point in the history
…leaf is null
  • Loading branch information
Michael Sokolov committed Feb 4, 2019
1 parent 49dc7a9 commit 03a19c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 49 deletions.
15 changes: 5 additions & 10 deletions lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
Expand Up @@ -638,21 +638,16 @@ protected void search(List<LeafReaderContext> leaves, Weight weight, Collector c
// threaded...? the Collector could be sync'd?
// always use single thread:
for (LeafReaderContext ctx : leaves) { // search each subreader
final LeafCollector leafCollector;
try {
leafCollector = collector.getLeafCollector(ctx);
} catch (CollectionTerminatedException e) {
// there is no doc of interest in this reader context
// continue with the following leaf
continue;
}
BulkScorer scorer = weight.bulkScorer(ctx);
if (scorer != null) {
try {
final LeafCollector leafCollector= collector.getLeafCollector(ctx);
scorer.score(leafCollector, ctx.reader().getLiveDocs());
} catch (CollectionTerminatedException e) {
// collection was terminated prematurely
// continue with the following leaf
// Either there is no doc of interest in this reader context,
// or collection was terminated prematurely:
// Continue with the following leaf
continue;
}
}
}
Expand Down
Expand Up @@ -20,7 +20,6 @@
import java.util.List;
import java.util.Random;

import junit.framework.Assert;
import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.Fields;
Expand All @@ -40,9 +39,9 @@
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.Version;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Utility class for sanity-checking queries.
Expand Down Expand Up @@ -266,8 +265,6 @@ public CacheHelper getReaderCacheHelper() {
*/
public static void checkSkipTo(final Query q, final IndexSearcher s) throws IOException {
//System.out.println("Checking "+q);
final List<LeafReaderContext> readerContextArray = s.getTopReaderContext().leaves();

final int skip_op = 0;
final int next_op = 1;
final int orders [][] = {
Expand All @@ -286,18 +283,17 @@ public static void checkSkipTo(final Query q, final IndexSearcher s) throws IOEx
// System.out.print(order[i]==skip_op ? " skip()":" next()");
// System.out.println();
final int opidx[] = { 0 };
final LeafReaderContext[] lastReaderContext = { null };
final int lastDoc[] = {-1};

// FUTURE: ensure scorer.doc()==-1

final float maxDiff = 1e-5f;
final LeafReader lastReader[] = {null};

s.search(q, new SimpleCollector() {
private Scorable sc;
private Scorer scorer;
private DocIdSetIterator iterator;
private int leafPtr;

@Override
public void setScorer(Scorable scorer) {
Expand All @@ -312,8 +308,7 @@ public void collect(int doc) throws IOException {
if (scorer == null) {
Query rewritten = s.rewrite(q);
Weight w = s.createWeight(rewritten, ScoreMode.COMPLETE, 1);
LeafReaderContext context = readerContextArray.get(leafPtr);
scorer = w.scorer(context);
scorer = w.scorer(lastReaderContext[0]);
iterator = scorer.iterator();
}

Expand Down Expand Up @@ -372,8 +367,8 @@ public ScoreMode scoreMode() {
protected void doSetNextReader(LeafReaderContext context) throws IOException {
// confirm that skipping beyond the last doc, on the
// previous reader, hits NO_MORE_DOCS
if (lastReader[0] != null) {
final LeafReader previousReader = lastReader[0];
if (lastReaderContext[0] != null) {
final LeafReader previousReader = lastReaderContext[0].reader();
IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader, false);
indexSearcher.setSimilarity(s.getSimilarity());
Query rewritten = indexSearcher.rewrite(q);
Expand All @@ -390,21 +385,19 @@ protected void doSetNextReader(LeafReaderContext context) throws IOException {
break;
}
}
Assert.assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
}
leafPtr++;
}
lastReader[0] = context.reader();
assert readerContextArray.get(leafPtr).reader() == context.reader();
lastReaderContext[0] = context;
this.scorer = null;
lastDoc[0] = -1;
}
});

if (lastReader[0] != null) {
if (lastReaderContext[0] != null) {
// confirm that skipping beyond the last doc, on the
// previous reader, hits NO_MORE_DOCS
final LeafReader previousReader = lastReader[0];
final LeafReader previousReader = lastReaderContext[0].reader();
IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader, false);
indexSearcher.setSimilarity(s.getSimilarity());
Query rewritten = indexSearcher.rewrite(q);
Expand All @@ -414,14 +407,14 @@ protected void doSetNextReader(LeafReaderContext context) throws IOException {
if (scorer != null) {
DocIdSetIterator iterator = scorer.iterator();
boolean more = false;
final Bits liveDocs = lastReader[0].getLiveDocs();
final Bits liveDocs = previousReader.getLiveDocs();
for (int d = iterator.advance(lastDoc[0] + 1); d != DocIdSetIterator.NO_MORE_DOCS; d = iterator.nextDoc()) {
if (liveDocs == null || liveDocs.get(d)) {
more = true;
break;
}
}
Assert.assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
}
}
}
Expand All @@ -432,29 +425,32 @@ public static void checkFirstSkipTo(final Query q, final IndexSearcher s) throws
//System.out.println("checkFirstSkipTo: "+q);
final float maxDiff = 1e-3f;
final int lastDoc[] = {-1};
final LeafReader lastReader[] = {null};
final LeafReaderContext lastReaderContext[] = {null};
final List<LeafReaderContext> context = s.getTopReaderContext().leaves();
Query rewritten = s.rewrite(q);
s.search(q,new SimpleCollector() {

private Scorable scorer;
private int leafPtr;

@Override
public void setScorer(Scorable scorer) {
this.scorer = scorer;
}

@Override
public void collect(int doc) throws IOException {
float score = scorer.score();
try {
long startMS = System.currentTimeMillis();
for (int i=lastDoc[0]+1; i<=doc; i++) {
int startDoc = lastDoc[0] + 1;
for (int i = startDoc; i <= doc; i++) {
Weight w = s.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Scorer scorer = w.scorer(context.get(leafPtr));
Assert.assertTrue("query collected "+doc+" but advance("+i+") says no more docs!",scorer.iterator().advance(i) != DocIdSetIterator.NO_MORE_DOCS);
Assert.assertEquals("query collected "+doc+" but advance("+i+") got to "+scorer.docID(),doc,scorer.docID());
Scorer scorer = w.scorer(context.get(lastReaderContext[0].ord));
assertTrue("query collected "+doc+" but advance("+i+") says no more docs!",scorer.iterator().advance(i) != DocIdSetIterator.NO_MORE_DOCS);
assertEquals("query collected "+doc+" but advance("+i+") got to "+scorer.docID(),doc,scorer.docID());
float advanceScore = scorer.score();
Assert.assertEquals("unstable advance("+i+") score!",advanceScore,scorer.score(),maxDiff);
Assert.assertEquals("query assigned doc "+doc+" a score of <"+score+"> but advance("+i+") has <"+advanceScore+">!",score,advanceScore,maxDiff);
assertEquals("unstable advance("+i+") score!",advanceScore,scorer.score(), maxDiff);
assertEquals("query assigned doc "+doc+" a score of <"+score+"> but advance("+i+") has <"+advanceScore+">!",score,advanceScore,maxDiff);

// Hurry things along if they are going slow (eg
// if you got SimpleText codec this will kick in):
Expand All @@ -477,8 +473,8 @@ public ScoreMode scoreMode() {
protected void doSetNextReader(LeafReaderContext context) throws IOException {
// confirm that skipping beyond the last doc, on the
// previous reader, hits NO_MORE_DOCS
if (lastReader[0] != null) {
final LeafReader previousReader = lastReader[0];
if (lastReaderContext[0] != null) {
final LeafReader previousReader = lastReaderContext[0].reader();
IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader, false);
indexSearcher.setSimilarity(s.getSimilarity());
Weight w = indexSearcher.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Expand All @@ -493,35 +489,34 @@ protected void doSetNextReader(LeafReaderContext context) throws IOException {
break;
}
}
Assert.assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
}
leafPtr++;
}

lastReader[0] = context.reader();
lastReaderContext[0] = context;
lastDoc[0] = -1;
}
});

if (lastReader[0] != null) {
if (lastReaderContext[0] != null) {
// confirm that skipping beyond the last doc, on the
// previous reader, hits NO_MORE_DOCS
final LeafReader previousReader = lastReader[0];
final LeafReader previousReader = lastReaderContext[0].reader();
IndexSearcher indexSearcher = LuceneTestCase.newSearcher(previousReader, false);
indexSearcher.setSimilarity(s.getSimilarity());
Weight w = indexSearcher.createWeight(rewritten, ScoreMode.COMPLETE, 1);
Scorer scorer = w.scorer((LeafReaderContext)indexSearcher.getTopReaderContext());
if (scorer != null) {
DocIdSetIterator iterator = scorer.iterator();
boolean more = false;
final Bits liveDocs = lastReader[0].getLiveDocs();
final Bits liveDocs = previousReader.getLiveDocs();
for (int d = iterator.advance(lastDoc[0] + 1); d != DocIdSetIterator.NO_MORE_DOCS; d = iterator.nextDoc()) {
if (liveDocs == null || liveDocs.get(d)) {
more = true;
break;
}
}
Assert.assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
assertFalse("query's last doc was "+ lastDoc[0] +" but advance("+(lastDoc[0]+1)+") got to "+scorer.docID(),more);
}
}
}
Expand Down Expand Up @@ -558,8 +553,8 @@ public void setScorer(Scorable scorer) throws IOException {
public void collect(int doc) throws IOException {
assert doc >= min;
assert doc < max;
Assert.assertEquals(scorer.docID(), doc);
Assert.assertEquals(scorer.score(), scorer2.score(), 0.01f);
assertEquals(scorer.docID(), doc);
assertEquals(scorer.score(), scorer2.score(), 0.01f);
iterator.nextDoc();
}
}, null, min, max);
Expand Down

0 comments on commit 03a19c5

Please sign in to comment.