Apache Lucene open-source search software
LUCENE-6309: disable coord when scores are not needed, or the scoring fn does not use it
Robert Muir99a16fc2dcc9
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.javaindex aa0bc4ebaca..a59ac912fd0 100644--- a/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanWeight.java@@ -42,13 +42,13 @@ public class BooleanWeight extends Weight {protected int maxCoord; // num optional + num requiredprivate final boolean disableCoord;private final boolean needsScores;+ private final float coords[];public BooleanWeight(BooleanQuery query, IndexSearcher searcher, boolean needsScores, boolean disableCoord) throws IOException {super(query);this.query = query;this.needsScores = needsScores;this.similarity = searcher.getSimilarity();- this.disableCoord = disableCoord;weights = new ArrayList<>(query.clauses().size());for (int i = 0 ; i < query.clauses().size(); i++) {BooleanClause c = query.clauses().get(i);@@ -58,6 +58,23 @@ public class BooleanWeight extends Weight {maxCoord++;}}++ // precompute coords (0..N, N).+ // set disableCoord when its explicit, scores are not needed, no scoring clauses, or the sim doesn't use it.+ coords = new float[maxCoord+1];+ Arrays.fill(coords, 1F);+ coords[0] = 0f;+ if (maxCoord > 0 && needsScores && disableCoord == false) {+ // compute coords from the similarity, look for any actual ones.+ boolean seenActualCoord = false;+ for (int i = 1; i < coords.length; i++) {+ coords[i] = coord(i, maxCoord);+ seenActualCoord |= (coords[i] != 1F);+ }+ this.disableCoord = seenActualCoord == false;+ } else {+ this.disableCoord = true;+ }}@Override@@ -346,9 +363,9 @@ public class BooleanWeight extends Weight {}} else {if (minShouldMatch > 0) {- return new BooleanTopLevelScorers.CoordinatingConjunctionScorer(this, coords(), req, requiredScoring.size(), opt);+ return new BooleanTopLevelScorers.CoordinatingConjunctionScorer(this, coords, req, requiredScoring.size(), opt);} else {- return new BooleanTopLevelScorers.ReqMultiOptScorer(req, opt, requiredScoring.size(), coords());+ return new BooleanTopLevelScorers.ReqMultiOptScorer(req, opt, requiredScoring.size(), coords);}}}@@ -395,10 +412,11 @@ public class BooleanWeight extends Weight {} else {float coords[];if (disableCoord) {+ // sneaky: when we do a mixed conjunction/disjunction, we need a fake for the disjunction part.coords = new float[optional.size()+1];Arrays.fill(coords, 1F);} else {- coords = coords();+ coords = this.coords;}if (minShouldMatch > 1) {return new MinShouldMatchSumScorer(this, optional, minShouldMatch, coords);@@ -407,13 +425,4 @@ public class BooleanWeight extends Weight {}}}-- private float[] coords() {- float[] coords = new float[maxCoord+1];- coords[0] = 0F;- for (int i = 1; i < coords.length; i++) {- coords[i] = coord(i, maxCoord);- }- return coords;- }}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.javaindex ffa05684e89..6658c0851de 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanQuery.java@@ -674,7 +674,7 @@ public class TestBooleanQuery extends LuceneTestCase {final Weight weight = searcher.createNormalizedWeight(q, random().nextBoolean());final Scorer scorer = weight.scorer(reader.leaves().get(0), null);- assertTrue(scorer instanceof BoostedScorer);+ assertTrue(scorer instanceof BoostedScorer || scorer instanceof ExactPhraseScorer);assertNotNull(scorer.asTwoPhaseIterator());reader.close();