|
26 | 26 | import org.apache.lucene.index.IndexReader; |
27 | 27 | import org.apache.lucene.index.IndexWriter; |
28 | 28 | import org.apache.lucene.index.IndexWriterConfig; |
| 29 | +import org.apache.lucene.index.LeafReaderContext; |
29 | 30 | import org.apache.lucene.index.Term; |
| 31 | +import org.apache.lucene.search.BulkScorer; |
| 32 | +import org.apache.lucene.search.FilterWeight; |
| 33 | +import org.apache.lucene.search.IndexSearcher; |
30 | 34 | import org.apache.lucene.search.PrefixQuery; |
31 | 35 | import org.apache.lucene.search.Query; |
| 36 | +import org.apache.lucene.search.QueryVisitor; |
| 37 | +import org.apache.lucene.search.ScoreMode; |
| 38 | +import org.apache.lucene.search.Scorer; |
| 39 | +import org.apache.lucene.search.ScorerSupplier; |
32 | 40 | import org.apache.lucene.search.TermQuery; |
| 41 | +import org.apache.lucene.search.Weight; |
33 | 42 | import org.apache.lucene.store.Directory; |
34 | 43 | import org.apache.lucene.tests.util.LuceneTestCase; |
35 | 44 |
|
@@ -73,4 +82,106 @@ public void testRewrite() throws IOException { |
73 | 82 | } |
74 | 83 | } |
75 | 84 | } |
| 85 | + |
| 86 | + public void testBulkScoringIsDisabled() throws IOException { |
| 87 | + |
| 88 | + try (Directory dir = newDirectory(); |
| 89 | + IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(new StandardAnalyzer()))) { |
| 90 | + |
| 91 | + Document doc = new Document(); |
| 92 | + doc.add(new TextField("field", "term1 term2 term3 term4", Field.Store.NO)); |
| 93 | + iw.addDocument(doc); |
| 94 | + iw.commit(); |
| 95 | + |
| 96 | + try (IndexReader reader = DirectoryReader.open(dir)) { |
| 97 | + IndexSearcher searcher = newSearcher(reader); |
| 98 | + // disable query caching, so that we exercise the search path directly rather than |
| 99 | + // any bulk-scoring optimizations the cache may apply internally |
| 100 | + searcher.setQueryCache(null); |
| 101 | + |
| 102 | + Query throwing = new ThrowsOnBulkScoreQuery(new TermQuery(new Term("field", "term1"))); |
| 103 | + |
| 104 | + // sanity check that the wrapped query really does throw when bulk-scored directly |
| 105 | + expectThrows(AssertionError.class, () -> searcher.search(throwing, 10)); |
| 106 | + |
| 107 | + Query wrapped = new ForceNoBulkScoringQuery(throwing); |
| 108 | + // should not throw, because bulk scoring has been disabled |
| 109 | + assertEquals(1, searcher.search(wrapped, 10).totalHits.value()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A query wrapper whose ScorerSupplier throws an AssertionError if bulkScorer() is called, used |
| 116 | + * to check that {@link ForceNoBulkScoringQuery} never calls bulkScorer() on the ScorerSupplier of |
| 117 | + * the query it wraps. |
| 118 | + */ |
| 119 | + private static class ThrowsOnBulkScoreQuery extends Query { |
| 120 | + |
| 121 | + private final Query in; |
| 122 | + |
| 123 | + private ThrowsOnBulkScoreQuery(Query in) { |
| 124 | + this.in = in; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Query rewrite(IndexSearcher indexSearcher) throws IOException { |
| 129 | + Query rewritten = in.rewrite(indexSearcher); |
| 130 | + if (rewritten != in) { |
| 131 | + return new ThrowsOnBulkScoreQuery(rewritten); |
| 132 | + } |
| 133 | + return super.rewrite(indexSearcher); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) |
| 138 | + throws IOException { |
| 139 | + Weight innerWeight = in.createWeight(searcher, scoreMode, boost); |
| 140 | + return new FilterWeight(innerWeight) { |
| 141 | + @Override |
| 142 | + public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { |
| 143 | + ScorerSupplier innerSupplier = super.scorerSupplier(context); |
| 144 | + if (innerSupplier == null) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + return new ScorerSupplier() { |
| 148 | + @Override |
| 149 | + public Scorer get(long leadCost) throws IOException { |
| 150 | + return innerSupplier.get(leadCost); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public long cost() { |
| 155 | + return innerSupplier.cost(); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public BulkScorer bulkScorer() { |
| 160 | + throw new AssertionError("bulkScorer() should not have been called"); |
| 161 | + } |
| 162 | + }; |
| 163 | + } |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String toString(String field) { |
| 169 | + return "ThrowsOnBulkScore(" + in.toString(field) + ")"; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void visit(QueryVisitor visitor) { |
| 174 | + in.visit(visitor); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean equals(Object o) { |
| 179 | + return sameClassAs(o) && in.equals(((ThrowsOnBulkScoreQuery) o).in); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public int hashCode() { |
| 184 | + return classHash() * 31 + in.hashCode(); |
| 185 | + } |
| 186 | + } |
76 | 187 | } |
0 commit comments