From 9ecf566cca1fd40b3d712d9e4741965588e3007c Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 24 May 2024 09:36:38 +0200 Subject: [PATCH] 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`. --- .../monitor/src/java/org/apache/lucene/monitor/Monitor.java | 6 +----- .../src/java/org/apache/lucene/monitor/QueryIndex.java | 4 ++++ .../java/org/apache/lucene/monitor/ReadonlyQueryIndex.java | 5 +++++ .../java/org/apache/lucene/monitor/WritableQueryIndex.java | 6 ++++++ .../lucene/monitor/TestPresearcherMatchCollector.java | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/Monitor.java b/lucene/monitor/src/java/org/apache/lucene/monitor/Monitor.java index 25d187a8a6a..affb851ee51 100644 --- a/lucene/monitor/src/java/org/apache/lucene/monitor/Monitor.java +++ b/lucene/monitor/src/java/org/apache/lucene/monitor/Monitor.java @@ -34,8 +34,6 @@ import org.apache.lucene.search.Matches; import org.apache.lucene.search.MatchesIterator; import org.apache.lucene.search.Query; 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 @@ -377,9 +375,7 @@ public class Monitor implements Closeable { @Override public void matchQuery(final String id, QueryCacheEntry query, QueryIndex.DataValues dataValues) throws IOException { - Scorer scorer = ((Scorer) dataValues.scorer); - Weight w = scorer.getWeight(); - Matches matches = w.matches(dataValues.ctx, scorer.docID()); + Matches matches = dataValues.weight.matches(dataValues.ctx, dataValues.docID); for (String field : matches) { MatchesIterator mi = matches.getMatches(field); while (mi.next()) { diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java index 5868ec53ea8..6e6a67f121d 100644 --- a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java +++ b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java @@ -38,6 +38,7 @@ import org.apache.lucene.search.Scorable; import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.SearcherManager; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.Weight; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRefHash; @@ -121,8 +122,10 @@ abstract class QueryIndex implements Closeable { SortedDocValues queryId; SortedDocValues cacheId; BinaryDocValues mq; + Weight weight; Scorable scorer; LeafReaderContext ctx; + int docID; void advanceTo(int doc) throws IOException { queryId.advanceExact(doc); @@ -130,6 +133,7 @@ abstract class QueryIndex implements Closeable { if (mq != null) { mq.advanceExact(doc); } + this.docID = doc; } } diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/ReadonlyQueryIndex.java b/lucene/monitor/src/java/org/apache/lucene/monitor/ReadonlyQueryIndex.java index e15a9b0cb70..da5074cfade 100644 --- a/lucene/monitor/src/java/org/apache/lucene/monitor/ReadonlyQueryIndex.java +++ b/lucene/monitor/src/java/org/apache/lucene/monitor/ReadonlyQueryIndex.java @@ -155,6 +155,11 @@ class ReadonlyQueryIndex extends QueryIndex { this.decomposer = decomposer; } + @Override + public void setWeight(Weight weight) { + this.dataValues.weight = weight; + } + @Override public void setScorer(Scorable scorer) { this.dataValues.scorer = scorer; diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java b/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java index 980e1083ce8..688070a0961 100644 --- a/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java +++ b/lucene/monitor/src/java/org/apache/lucene/monitor/WritableQueryIndex.java @@ -45,6 +45,7 @@ import org.apache.lucene.search.Scorable; import org.apache.lucene.search.ScoreMode; import org.apache.lucene.search.SearcherManager; import org.apache.lucene.search.SimpleCollector; +import org.apache.lucene.search.Weight; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.NamedThreadFactory; @@ -363,6 +364,11 @@ class WritableQueryIndex extends QueryIndex { this.matcher = matcher; } + @Override + public void setWeight(Weight weight) { + this.dataValues.weight = weight; + } + @Override public void setScorer(Scorable scorer) { this.dataValues.scorer = scorer; diff --git a/lucene/monitor/src/test/org/apache/lucene/monitor/TestPresearcherMatchCollector.java b/lucene/monitor/src/test/org/apache/lucene/monitor/TestPresearcherMatchCollector.java index a6d355e330c..8ff1f8a289f 100644 --- a/lucene/monitor/src/test/org/apache/lucene/monitor/TestPresearcherMatchCollector.java +++ b/lucene/monitor/src/test/org/apache/lucene/monitor/TestPresearcherMatchCollector.java @@ -42,7 +42,7 @@ public class TestPresearcherMatchCollector extends MonitorTestBase { PresearcherMatches matches = monitor.debug(doc, QueryMatch.SIMPLE_MATCHER); 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("2", 0)); @@ -51,7 +51,7 @@ public class TestPresearcherMatchCollector extends MonitorTestBase { MatcherAssert.assertThat(pm, containsString("f2:(quuz)")); 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("4", 0));