Apache Lucene open-source search software
LUCENE-6262: Don't wrap the inner weight in ConstantScoreQuery when scores are not required.
Adrien Grand487e9849e0dc
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 3f600a70f65..4345f240797 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -98,6 +98,9 @@ Optimizationsfilter out non-index files with some custom usage, you may want to look atthe IndexFileNames class. (Robert Muir)+* LUCENE-6262: ConstantScoreQuery does not wrap the inner weight anymore when+ scores are not required. (Adrien Grand)+API Changes* LUCENE-6204, LUCENE-6208: Simplify CompoundFormat: remove files()diff --git a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.javaindex 33760ed4347..228e70306cd 100644--- a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java@@ -78,9 +78,9 @@ public class ConstantScoreQuery extends Query {private float queryNorm;private float queryWeight;- public ConstantWeight(IndexSearcher searcher) throws IOException {+ public ConstantWeight(Weight innerWeight) throws IOException {super(ConstantScoreQuery.this);- this.innerWeight = query.createWeight(searcher, false);+ this.innerWeight = innerWeight;}@Override@@ -277,7 +277,12 @@ public class ConstantScoreQuery extends Query {@Overridepublic Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {- return new ConstantScoreQuery.ConstantWeight(searcher);+ final Weight innerWeight = query.createWeight(searcher, false);+ if (needsScores) {+ return new ConstantScoreQuery.ConstantWeight(innerWeight);+ } else {+ return innerWeight;+ }}@Overridediff --git a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.javaindex d1830d70a71..ecf0c7797c8 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestConstantScoreQuery.java@@ -115,8 +115,8 @@ public class TestConstantScoreQuery extends LuceneTestCase {final Query csqbq = new ConstantScoreQuery(bq);csqbq.setBoost(17.0f);- checkHits(searcher, csq1, csq1.getBoost(), ConstantScoreQuery.ConstantScoreScorer.class.getName(), null);- checkHits(searcher, csq2, csq2.getBoost(), ConstantScoreQuery.ConstantScoreScorer.class.getName(), ConstantScoreQuery.ConstantScoreScorer.class.getName());+ checkHits(searcher, csq1, csq1.getBoost(), ConstantScoreQuery.ConstantScoreScorer.class.getName(), TermScorer.class.getName());+ checkHits(searcher, csq2, csq2.getBoost(), ConstantScoreQuery.ConstantScoreScorer.class.getName(), TermScorer.class.getName());// for the combined BQ, the scorer should always be BooleanScorer's BucketScorer, because our scorer supports out-of order collection!final String bucketScorerClass = FakeScorer.class.getName();