Remove unchecked Scorable -> Scorer cast in lucene/monitor. (#13405)

While doing an unrelated refactoring, I got hit by this unchecked cast, which
is incorrecw when the presearcher query produces some specialized `BulkScorer`.
This commit is contained in:
Adrien Grand 2024-05-24 09:36:38 +02:00 committed by GitHub
parent 8773725ac0
commit 9ecf566cca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 18 additions and 7 deletions

View File

@ -34,8 +34,6 @@ import org.apache.lucene.search.Matches;
import org.apache.lucene.search.MatchesIterator; import org.apache.lucene.search.MatchesIterator;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Weight;
/** /**
* A Monitor contains a set of {@link Query} objects with associated IDs, and efficiently matches * A Monitor contains a set of {@link Query} objects with associated IDs, and efficiently matches
@ -377,9 +375,7 @@ public class Monitor implements Closeable {
@Override @Override
public void matchQuery(final String id, QueryCacheEntry query, QueryIndex.DataValues dataValues) public void matchQuery(final String id, QueryCacheEntry query, QueryIndex.DataValues dataValues)
throws IOException { throws IOException {
Scorer scorer = ((Scorer) dataValues.scorer); Matches matches = dataValues.weight.matches(dataValues.ctx, dataValues.docID);
Weight w = scorer.getWeight();
Matches matches = w.matches(dataValues.ctx, scorer.docID());
for (String field : matches) { for (String field : matches) {
MatchesIterator mi = matches.getMatches(field); MatchesIterator mi = matches.getMatches(field);
while (mi.next()) { while (mi.next()) {

View File

@ -38,6 +38,7 @@ import org.apache.lucene.search.Scorable;
import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.SearcherManager; import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefHash; import org.apache.lucene.util.BytesRefHash;
@ -121,8 +122,10 @@ abstract class QueryIndex implements Closeable {
SortedDocValues queryId; SortedDocValues queryId;
SortedDocValues cacheId; SortedDocValues cacheId;
BinaryDocValues mq; BinaryDocValues mq;
Weight weight;
Scorable scorer; Scorable scorer;
LeafReaderContext ctx; LeafReaderContext ctx;
int docID;
void advanceTo(int doc) throws IOException { void advanceTo(int doc) throws IOException {
queryId.advanceExact(doc); queryId.advanceExact(doc);
@ -130,6 +133,7 @@ abstract class QueryIndex implements Closeable {
if (mq != null) { if (mq != null) {
mq.advanceExact(doc); mq.advanceExact(doc);
} }
this.docID = doc;
} }
} }

View File

@ -155,6 +155,11 @@ class ReadonlyQueryIndex extends QueryIndex {
this.decomposer = decomposer; this.decomposer = decomposer;
} }
@Override
public void setWeight(Weight weight) {
this.dataValues.weight = weight;
}
@Override @Override
public void setScorer(Scorable scorer) { public void setScorer(Scorable scorer) {
this.dataValues.scorer = scorer; this.dataValues.scorer = scorer;

View File

@ -45,6 +45,7 @@ import org.apache.lucene.search.Scorable;
import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.SearcherManager; import org.apache.lucene.search.SearcherManager;
import org.apache.lucene.search.SimpleCollector; import org.apache.lucene.search.SimpleCollector;
import org.apache.lucene.search.Weight;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.NamedThreadFactory; import org.apache.lucene.util.NamedThreadFactory;
@ -363,6 +364,11 @@ class WritableQueryIndex extends QueryIndex {
this.matcher = matcher; this.matcher = matcher;
} }
@Override
public void setWeight(Weight weight) {
this.dataValues.weight = weight;
}
@Override @Override
public void setScorer(Scorable scorer) { public void setScorer(Scorable scorer) {
this.dataValues.scorer = scorer; this.dataValues.scorer = scorer;

View File

@ -42,7 +42,7 @@ public class TestPresearcherMatchCollector extends MonitorTestBase {
PresearcherMatches<QueryMatch> matches = monitor.debug(doc, QueryMatch.SIMPLE_MATCHER); PresearcherMatches<QueryMatch> matches = monitor.debug(doc, QueryMatch.SIMPLE_MATCHER);
assertNotNull(matches.match("1", 0)); assertNotNull(matches.match("1", 0));
assertEquals(" field:test", matches.match("1", 0).presearcherMatches); assertEquals(" field:(foo test)", matches.match("1", 0).presearcherMatches);
assertNotNull(matches.match("1", 0).queryMatch); assertNotNull(matches.match("1", 0).queryMatch);
assertNotNull(matches.match("2", 0)); assertNotNull(matches.match("2", 0));
@ -51,7 +51,7 @@ public class TestPresearcherMatchCollector extends MonitorTestBase {
MatcherAssert.assertThat(pm, containsString("f2:(quuz)")); MatcherAssert.assertThat(pm, containsString("f2:(quuz)"));
assertNotNull(matches.match("3", 0)); assertNotNull(matches.match("3", 0));
assertEquals(" field:foo", matches.match("3", 0).presearcherMatches); assertEquals(" field:(foo test)", matches.match("3", 0).presearcherMatches);
assertNull(matches.match("3", 0).queryMatch); assertNull(matches.match("3", 0).queryMatch);
assertNull(matches.match("4", 0)); assertNull(matches.match("4", 0));