Skip to content

Apache Lucene open-source search software

Clean up how the test framework creates asserting scorables. (#14452)

Adrien Grand0a40f9e151ab
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSloppyPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestSloppyPhraseQuery.java
index 80bbf76b782..95e08ebcae9 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSloppyPhraseQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSloppyPhraseQuery.java
@@ -29,7 +29,6 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.analysis.MockTokenizer;
import org.apache.lucene.tests.index.RandomIndexWriter;
-import org.apache.lucene.tests.search.AssertingScorable;
import org.apache.lucene.tests.search.QueryUtils;
import org.apache.lucene.tests.store.MockDirectoryWrapper;
import org.apache.lucene.tests.util.LuceneTestCase;
@@ -157,7 +156,8 @@ public class TestSloppyPhraseQuery extends LuceneTestCase {
IndexReader reader = writer.getReader();
- IndexSearcher searcher = newSearcher(reader);
+ // Disable assertions as the collector expects a PhraseScorer, not an asserting wrapper.
+ IndexSearcher searcher = newSearcher(reader, random().nextBoolean(), false);
Result result = searcher.search(query, new MaxFreqCollectorManager());
assertEquals(
"slop: " + slop + " query: " + query + " doc: " + doc + " Wrong number of hits",
@@ -218,7 +218,7 @@ public class TestSloppyPhraseQuery extends LuceneTestCase {
@Override
public void setScorer(Scorable scorer) throws IOException {
- this.scorer = (Scorer) AssertingScorable.unwrap(scorer);
+ this.scorer = (Scorer) scorer;
}
@Override
@@ -250,7 +250,7 @@ public class TestSloppyPhraseQuery extends LuceneTestCase {
@Override
public void setScorer(Scorable scorer) {
- this.scorer = (Scorer) AssertingScorable.unwrap(scorer);
+ this.scorer = (Scorer) scorer;
}
@Override
@@ -323,7 +323,8 @@ public class TestSloppyPhraseQuery extends LuceneTestCase {
IndexReader ir = iw.getReader();
iw.close();
- IndexSearcher is = newSearcher(ir);
+ // Disable assertions as the collector expects a PhraseScorer, not an asserting wrapper.
+ IndexSearcher is = newSearcher(ir, random().nextBoolean(), false);
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.add(new Term("lyrics", "drug"), 1);
builder.add(new Term("lyrics", "drug"), 3);
@@ -377,7 +378,8 @@ public class TestSloppyPhraseQuery extends LuceneTestCase {
IndexReader ir = iw.getReader();
iw.close();
- IndexSearcher is = newSearcher(ir);
+ // Disable assertions as the collector expects a PhraseScorer, not an asserting wrapper.
+ IndexSearcher is = newSearcher(ir, random().nextBoolean(), false);
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.add(new Term("lyrics", "drug"), 1);
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java
index 7acdd2a224a..d66000b2b9f 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorable.java
@@ -19,7 +19,6 @@ package org.apache.lucene.tests.search;
import java.io.IOException;
import org.apache.lucene.search.FilterScorable;
-import org.apache.lucene.search.FilterScorer;
import org.apache.lucene.search.Scorable;
import org.apache.lucene.search.Scorer;
@@ -44,45 +43,16 @@ public class AssertingScorable extends FilterScorable {
}
public static Scorable wrap(Scorable in) {
- if (in instanceof WrappedScorer || in instanceof AssertingScorable) {
+ if (in instanceof AssertingScorable || in instanceof AssertingScorer) {
return in;
}
+
// If `in` is Scorer, we need to wrap it as a Scorer instead of Scorable because
// NumericComparator uses the iterator cost of a Scorer in sort optimization.
- if (in instanceof Scorer) {
- return new WrappedScorer((Scorer) in);
+ if (in instanceof Scorer scorer) {
+ return AssertingScorer.wrap(scorer, true, true);
} else {
return new AssertingScorable(in);
}
}
-
- private static class WrappedScorer extends FilterScorer {
- WrappedScorer(Scorer in) {
- super(in);
- }
-
- @Override
- public float score() throws IOException {
- return new AssertingScorable(in).score();
- }
-
- @Override
- public void setMinCompetitiveScore(float minScore) throws IOException {
- in.setMinCompetitiveScore(minScore);
- }
-
- @Override
- public float getMaxScore(int upTo) throws IOException {
- return in.getMaxScore(upTo);
- }
- }
-
- public static Scorable unwrap(Scorable in) {
- while (true) {
- if (in instanceof AssertingScorable) in = ((AssertingScorable) in).in;
- else if (in instanceof AssertingScorer) in = ((AssertingScorer) in).in;
- else if (in instanceof WrappedScorer) in = ((WrappedScorer) in).unwrap();
- else return in;
- }
- }
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java
index 0e9d8bfa9ba..9cb5e384b0d 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java
@@ -19,10 +19,8 @@ package org.apache.lucene.tests.search;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
-import java.util.Random;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.FilterDocIdSetIterator;
-import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.TwoPhaseIterator;
import org.apache.lucene.util.FixedBitSet;
@@ -37,31 +35,37 @@ public class AssertingScorer extends Scorer {
FINISHED
};
- public static Scorer wrap(
- Random random, Scorer other, ScoreMode scoreMode, boolean canCallMinCompetitiveScore) {
+ /**
+ * Wrap the given scorer with additional checks.
+ *
+ * @param other the scorer
+ * @param canScore whether the scorer is allowed to compute scores, typically true for scoring
+ * clauses of the query if the collector needs scores
+ * @param canSetMinCompetitiveScore whether the scorer is allowed to set min competitive scores,
+ * typically true if the score mode is TOP_SCORES and this scorer is the top-level scoring
+ * clause
+ */
+ public static Scorer wrap(Scorer other, boolean canScore, boolean canSetMinCompetitiveScore) {
if (other == null) {
return null;
}
- return new AssertingScorer(random, other, scoreMode, canCallMinCompetitiveScore);
+ return new AssertingScorer(other, canScore, canSetMinCompetitiveScore);
}
- final Random random;
final Scorer in;
- final ScoreMode scoreMode;
- final boolean canCallMinCompetitiveScore;
+ final boolean canScore;
+ final boolean canSetMinCompetitiveScore;
IteratorState state = IteratorState.ITERATING;
int doc;
float minCompetitiveScore = 0;
int lastShallowTarget = -1;
- private AssertingScorer(
- Random random, Scorer in, ScoreMode scoreMode, boolean canCallMinCompetitiveScore) {
- this.random = random;
+ private AssertingScorer(Scorer in, boolean canScore, boolean canSetMinCompetitiveScore) {
this.in = in;
- this.scoreMode = scoreMode;
+ this.canScore = canScore;
+ this.canSetMinCompetitiveScore = canSetMinCompetitiveScore;
this.doc = in.docID();
- this.canCallMinCompetitiveScore = canCallMinCompetitiveScore;
}
public Scorer getIn() {
@@ -81,8 +85,8 @@ public class AssertingScorer extends Scorer {
@Override
public void setMinCompetitiveScore(float score) throws IOException {
- assert scoreMode == ScoreMode.TOP_SCORES;
- assert canCallMinCompetitiveScore;
+ assert canScore;
+ assert canSetMinCompetitiveScore;
assert Float.isNaN(score) == false;
assert score >= minCompetitiveScore;
in.setMinCompetitiveScore(score);
@@ -91,7 +95,7 @@ public class AssertingScorer extends Scorer {
@Override
public int advanceShallow(int target) throws IOException {
- assert scoreMode.needsScores();
+ assert canScore;
assert target >= lastShallowTarget
: "called on decreasing targets: target = "
+ target
@@ -107,7 +111,7 @@ public class AssertingScorer extends Scorer {
@Override
public float getMaxScore(int upTo) throws IOException {
- assert scoreMode.needsScores();
+ assert canScore;
assert upTo >= lastShallowTarget : "uTo = " + upTo + " < last target = " + lastShallowTarget;
assert docID() >= 0 || lastShallowTarget >= 0
: "Cannot get max scores until the iterator is positioned or advanceShallow has been called";
@@ -117,7 +121,7 @@ public class AssertingScorer extends Scorer {
@Override
public float score() throws IOException {
- assert scoreMode.needsScores();
+ assert canScore;
assert iterating() : state;
final float score = in.score();
assert !Float.isNaN(score) : "NaN score for in=" + in;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java
index 0bfc84615b6..f46158fb2b1 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java
@@ -71,11 +71,11 @@ class AssertingWeight extends FilterWeight {
assert getCalled == false;
getCalled = true;
assert leadCost >= 0 : leadCost;
+ boolean canScore = scoreMode.needsScores();
+ boolean canSetMinCompetitiveScore =
+ scoreMode == ScoreMode.TOP_SCORES && topLevelScoringClause;
return AssertingScorer.wrap(
- new Random(random.nextLong()),
- inScorerSupplier.get(leadCost),
- scoreMode,
- topLevelScoringClause);
+ inScorerSupplier.get(leadCost), canScore, canSetMinCompetitiveScore);
}
@Override