Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUCENE-10415: FunctionScoreQuery and IndexOrDocValuesQuery delegate Weight#count. #685

Merged
merged 4 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ New Features
the number of matching range docs when each doc has at-most one point and the points are 1-dimensional.
(Gautam Worah, Ignacio Vera, Adrien Grand)

* LUCENE-10415: FunctionScoreQuery and IndexOrDocValuesQuery delegate Weight#count. (Ignacio Vera)

Improvements
---------------------

Expand Down Expand Up @@ -181,7 +183,7 @@ Improvements

* LUCENE-10371: Make IndexRearranger able to arrange segment in a determined order.
(Patrick Zhai)

Optimizations
---------------------

Expand Down Expand Up @@ -614,7 +616,7 @@ Improvements
(David Smiley)

* LUCENE-10062: Switch taxonomy faceting to use numeric doc values for storing ordinals instead of binary doc values
with its own custom encoding. (Greg Miller)
with its own custom encoding. (Greg Miller)

Bug fixes
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ public BulkScorer bulkScorer(LeafReaderContext context) throws IOException {
return indexWeight.bulkScorer(context);
}

@Override
public int count(LeafReaderContext context) throws IOException {
final int count = indexWeight.count(context);
if (count != -1) {
return count;
}
return dvWeight.count(context);
}

@Override
public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {
final ScorerSupplier indexScorerSupplier = indexWeight.scorerSupplier(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
Expand Down Expand Up @@ -182,4 +183,42 @@ public void testUseIndexForSelectiveMultiValueQueries() throws IOException {
w.close();
dir.close();
}

// Weight#count is delegated to the inner weight
public void testQueryMatchesCount() throws Exception {
Directory dir = newDirectory();
IndexWriter w =
new IndexWriter(
dir,
newIndexWriterConfig()
// relies on costs and PointValues.estimateCost so we need the default codec
.setCodec(TestUtil.getDefaultCodec()));
final int numDocs = random().nextInt(5000);
for (int i = 0; i < numDocs; ++i) {
Document doc = new Document();
doc.add(new LongPoint("f2", 42L));
doc.add(new SortedNumericDocValuesField("f2", 42L));
w.addDocument(doc);
}
w.forceMerge(1);
IndexReader reader = DirectoryReader.open(w);
IndexSearcher searcher = newSearcher(reader);

final IndexOrDocValuesQuery query =
new IndexOrDocValuesQuery(
LongPoint.newExactQuery("f2", 42),
SortedNumericDocValuesField.newSlowRangeQuery("f2", 42, 42L));

final int searchCount = searcher.count(query);
final Weight weight = searcher.createWeight(query, ScoreMode.COMPLETE, 1);
int weightCount = 0;
for (LeafReaderContext leafReaderContext : reader.leaves()) {
weightCount += weight.count(leafReaderContext);
}
assertEquals(searchCount, weightCount);

reader.close();
w.close();
dir.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ public float getMaxScore(int upTo) throws IOException {
};
}

@Override
public int count(LeafReaderContext context) throws IOException {
return inner.count(context);
}

@Override
public boolean isCacheable(LeafReaderContext ctx) {
return inner.isCacheable(ctx) && valueSource.isCacheable(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
Expand Down Expand Up @@ -361,4 +362,19 @@ public void testScoreCalledTwice() throws Exception {
}
}
}

// Weight#count is delegated to the inner weight
public void testQueryMatchesCount() throws Exception {
TermQuery query = new TermQuery(new Term(TEXT_FIELD, "first"));
FunctionScoreQuery fq =
FunctionScoreQuery.boostByValue(query, DoubleValuesSource.fromIntField("iii"));

final int searchCount = searcher.count(fq);
final Weight weight = searcher.createWeight(fq, ScoreMode.COMPLETE, 1);
int weightCount = 0;
for (LeafReaderContext leafReaderContext : reader.leaves()) {
weightCount += weight.count(leafReaderContext);
}
assertEquals(searchCount, weightCount);
}
}