Skip to content

Apache Lucene open-source search software

LUCENE-6330: BS1 should not decode norms when scores are not needed.

Adrien Grand4058e217f029
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 2ac36757199..cb35321c4e6 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -110,6 +110,9 @@ Optimizations
* LUCENE-6455: Require a minimum index size to enable query caching in order
not to cache eg. on MemoryIndex. (Adrien Grand)
+* LUCENE-6330: BooleanScorer (used for top-level disjunctions) does not decode
+ norms when not necessary anymore. (Adrien Grand)
+
Bug Fixes
* LUCENE-6378: Fix all RuntimeExceptions to throw the underlying root cause.
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanScorer.java b/lucene/core/src/java/org/apache/lucene/search/BooleanScorer.java
index 61b4424be78..9f0782af5ff 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BooleanScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanScorer.java
@@ -37,6 +37,35 @@ final class BooleanScorer extends BulkScorer {
static final int SET_SIZE = 1 << (SHIFT - 6);
static final int SET_MASK = SET_SIZE - 1;
+ private static BulkScorer disableScoring(final BulkScorer scorer) {
+ return new BulkScorer() {
+
+ @Override
+ public int score(final LeafCollector collector, int min, int max) throws IOException {
+ final LeafCollector noScoreCollector = new LeafCollector() {
+ FakeScorer fake = new FakeScorer();
+
+ @Override
+ public void setScorer(Scorer scorer) throws IOException {
+ collector.setScorer(fake);
+ }
+
+ @Override
+ public void collect(int doc) throws IOException {
+ fake.doc = doc;
+ collector.collect(doc);
+ }
+ };
+ return scorer.score(noScoreCollector, min, max);
+ }
+
+ @Override
+ public long cost() {
+ return scorer.cost();
+ }
+ };
+ }
+
static class Bucket {
double score;
int freq;
@@ -146,7 +175,7 @@ final class BooleanScorer extends BulkScorer {
final OrCollector orCollector = new OrCollector();
- BooleanScorer(BooleanWeight weight, boolean disableCoord, int maxCoord, Collection<BulkScorer> scorers, int minShouldMatch) {
+ BooleanScorer(BooleanWeight weight, boolean disableCoord, int maxCoord, Collection<BulkScorer> scorers, int minShouldMatch, boolean needsScores) {
if (minShouldMatch < 1 || minShouldMatch > scorers.size()) {
throw new IllegalArgumentException("minShouldMatch should be within 1..num_scorers. Got " + minShouldMatch);
}
@@ -158,6 +187,11 @@ final class BooleanScorer extends BulkScorer {
this.tail = new TailPriorityQueue(minShouldMatch - 1);
this.minShouldMatch = minShouldMatch;
for (BulkScorer scorer : scorers) {
+ if (needsScores == false) {
+ // OrCollector calls score() all the time so we have to explicitly
+ // disable scoring in order to avoid decoding useless norms
+ scorer = disableScoring(scorer);
+ }
final BulkScorerAndDoc evicted = tail.insertWithOverflow(new BulkScorerAndDoc(scorer));
if (evicted != null) {
head.add(evicted);
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java
index e89a5dcb759..3d8e449e12f 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java
@@ -196,6 +196,7 @@ public class BooleanWeight extends Weight {
for (Weight w : weights) {
BooleanClause c = cIter.next();
BulkScorer subScorer = w.bulkScorer(context, acceptDocs);
+
if (subScorer == null) {
if (c.isRequired()) {
return null;
@@ -221,7 +222,7 @@ public class BooleanWeight extends Weight {
return null;
}
- return new BooleanScorer(this, disableCoord, maxCoord, optional, Math.max(1, query.minNrShouldMatch));
+ return new BooleanScorer(this, disableCoord, maxCoord, optional, Math.max(1, query.minNrShouldMatch), needsScores);
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java
index d43c6f18358..c7c60153d74 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanOr.java
@@ -254,7 +254,7 @@ public class TestBooleanOr extends LuceneTestCase {
scorer(5000, 100000, 9999998, 9999999)
);
Collections.shuffle(optionalScorers, random());
- BooleanScorer scorer = new BooleanScorer(null, true, 0, optionalScorers, 1);
+ BooleanScorer scorer = new BooleanScorer(null, true, 0, optionalScorers, 1, random().nextBoolean());
final List<Integer> matches = new ArrayList<>();
scorer.score(new LeafCollector() {