Apache Solr open-source search software
LUCENE-8751: Weight#matches now use the ScorerSupplier to build scorers with a lead cost of 1 (single document).
jimczi81fe8144280d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex cdf70f0840f..ee5fb8b1909 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -111,6 +111,9 @@ Improvementschildren (avg, max, min). Additionally the score mode `none` that assigns a constant score toeach parent can early terminate top scores's collection. (Jim Ferenczi)+* LUCENE-8751: Weight#matches now use the ScorerSupplier to build scorers with a lead cost of 1+ (single document). (Jim Ferenczi)+Changes in Runtime Behavior* LUCENE-8671: Load FST off-heap also for ID-like fields if reader is not openeddiff --git a/lucene/core/src/java/org/apache/lucene/search/Weight.java b/lucene/core/src/java/org/apache/lucene/search/Weight.javaindex 2f86f4feacd..8284d14e198 100644--- a/lucene/core/src/java/org/apache/lucene/search/Weight.java+++ b/lucene/core/src/java/org/apache/lucene/search/Weight.java@@ -71,10 +71,11 @@ public abstract class Weight implements SegmentCacheable {* @lucene.experimental*/public Matches matches(LeafReaderContext context, int doc) throws IOException {- Scorer scorer = scorer(context);- if (scorer == null) {+ ScorerSupplier scorerSupplier = scorerSupplier(context);+ if (scorerSupplier == null) {return null;}+ Scorer scorer = scorerSupplier.get(1);final TwoPhaseIterator twoPhase = scorer.twoPhaseIterator();if (twoPhase == null) {if (scorer.iterator().advance(doc) != doc) {diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java b/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.javaindex 0bc243d02b7..d80622e4123 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestMatchesIterator.java@@ -28,11 +28,13 @@ import java.util.stream.Collectors;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.FieldType;+import org.apache.lucene.document.IntPoint;import org.apache.lucene.document.NumericDocValuesField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.IndexOptions;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.LeafReaderContext;+import org.apache.lucene.index.NumericDocValues;import org.apache.lucene.index.PostingsEnum;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.ReaderUtil;@@ -54,6 +56,7 @@ public class TestMatchesIterator extends LuceneTestCase {private static final String FIELD_NO_OFFSETS = "field_no_offsets";private static final String FIELD_DOCS_ONLY = "field_docs_only";private static final String FIELD_FREQS = "field_freqs";+ private static final String FIELD_POINT = "field_point";private static final FieldType OFFSETS = new FieldType(TextField.TYPE_STORED);static {@@ -89,6 +92,8 @@ public class TestMatchesIterator extends LuceneTestCase {doc.add(newField(FIELD_NO_OFFSETS, docFields[i], TextField.TYPE_STORED));doc.add(newField(FIELD_DOCS_ONLY, docFields[i], DOCS));doc.add(newField(FIELD_FREQS, docFields[i], DOCS_AND_FREQS));+ doc.add(new IntPoint(FIELD_POINT, 10));+ doc.add(new NumericDocValuesField(FIELD_POINT, 10));doc.add(new NumericDocValuesField("id", i));doc.add(newField("id", Integer.toString(i), TextField.TYPE_STORED));writer.addDocument(doc);@@ -652,4 +657,49 @@ public class TestMatchesIterator extends LuceneTestCase {});}+ public void testPointQuery() throws IOException {+ IndexOrDocValuesQuery pointQuery = new IndexOrDocValuesQuery(+ IntPoint.newExactQuery(FIELD_POINT, 10),+ NumericDocValuesField.newSlowExactQuery(FIELD_POINT, 10)+ );+ Term t = new Term(FIELD_WITH_OFFSETS, "w1");+ Query query = new BooleanQuery.Builder()+ .add(new TermQuery(t), BooleanClause.Occur.MUST)+ .add(pointQuery, BooleanClause.Occur.MUST)+ .build();++ checkMatches(pointQuery, FIELD_WITH_OFFSETS, new int[][]{});++ checkMatches(query, FIELD_WITH_OFFSETS, new int[][]{+ { 0, 0, 0, 0, 2 },+ { 1, 0, 0, 0, 2 },+ { 2, 0, 0, 0, 2 },+ { 3, 0, 0, 0, 2, 2, 2, 6, 8 },+ { 4 }+ });++ pointQuery = new IndexOrDocValuesQuery(+ IntPoint.newExactQuery(FIELD_POINT, 11),+ NumericDocValuesField.newSlowExactQuery(FIELD_POINT, 11)+ );++ query = new BooleanQuery.Builder()+ .add(new TermQuery(t), BooleanClause.Occur.MUST)+ .add(pointQuery, BooleanClause.Occur.MUST)+ .build();+ checkMatches(query, FIELD_WITH_OFFSETS, new int[][]{});++ query = new BooleanQuery.Builder()+ .add(new TermQuery(t), BooleanClause.Occur.MUST)+ .add(pointQuery, BooleanClause.Occur.SHOULD)+ .build();+ checkMatches(query, FIELD_WITH_OFFSETS, new int[][]{+ {0, 0, 0, 0, 2},+ {1, 0, 0, 0, 2},+ {2, 0, 0, 0, 2},+ {3, 0, 0, 0, 2, 2, 2, 6, 8},+ {4}+ });+ }+}