|
22 | 22 | import org.apache.lucene.document.Document; |
23 | 23 | import org.apache.lucene.document.Field.Store; |
24 | 24 | import org.apache.lucene.document.StringField; |
| 25 | +import org.apache.lucene.index.DirectoryReader; |
25 | 26 | import org.apache.lucene.index.IndexReader; |
| 27 | +import org.apache.lucene.index.IndexWriter; |
| 28 | +import org.apache.lucene.index.LeafReaderContext; |
26 | 29 | import org.apache.lucene.index.RandomIndexWriter; |
27 | 30 | import org.apache.lucene.index.Term; |
| 31 | +import org.apache.lucene.search.ConstantScoreWeight; |
| 32 | +import org.apache.lucene.search.DocIdSetIterator; |
| 33 | +import org.apache.lucene.search.Explanation; |
28 | 34 | import org.apache.lucene.search.IndexSearcher; |
29 | 35 | import org.apache.lucene.search.LeafCollector; |
30 | 36 | import org.apache.lucene.search.Query; |
31 | 37 | import org.apache.lucene.search.RandomApproximationQuery; |
| 38 | +import org.apache.lucene.search.Scorer; |
| 39 | +import org.apache.lucene.search.ScorerSupplier; |
32 | 40 | import org.apache.lucene.search.Sort; |
33 | 41 | import org.apache.lucene.search.TermQuery; |
34 | 42 | import org.apache.lucene.search.TotalHitCountCollector; |
| 43 | +import org.apache.lucene.search.Weight; |
35 | 44 | import org.apache.lucene.store.Directory; |
36 | 45 | import org.apache.lucene.util.IOUtils; |
37 | 46 | import org.apache.lucene.util.TestUtil; |
|
45 | 54 | import java.io.IOException; |
46 | 55 | import java.util.List; |
47 | 56 | import java.util.Map; |
| 57 | +import java.util.Set; |
48 | 58 |
|
49 | 59 | import static org.hamcrest.Matchers.equalTo; |
50 | 60 | import static org.hamcrest.Matchers.greaterThan; |
@@ -191,4 +201,76 @@ public void testCollector() throws IOException { |
191 | 201 | leafCollector.collect(0); |
192 | 202 | assertThat(profileCollector.getTime(), greaterThan(time)); |
193 | 203 | } |
| 204 | + |
| 205 | + private static class DummyQuery extends Query { |
| 206 | + |
| 207 | + @Override |
| 208 | + public String toString(String field) { |
| 209 | + return getClass().getSimpleName(); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public boolean equals(Object obj) { |
| 214 | + return this == obj; |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public int hashCode() { |
| 219 | + return 0; |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException { |
| 224 | + return new ConstantScoreWeight(this) { |
| 225 | + @Override |
| 226 | + public void extractTerms(Set<Term> terms) { |
| 227 | + throw new UnsupportedOperationException(); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public Explanation explain(LeafReaderContext context, int doc) throws IOException { |
| 232 | + throw new UnsupportedOperationException(); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public Scorer scorer(LeafReaderContext context) throws IOException { |
| 237 | + throw new UnsupportedOperationException(); |
| 238 | + } |
| 239 | + |
| 240 | + @Override |
| 241 | + public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { |
| 242 | + final Weight weight = this; |
| 243 | + return new ScorerSupplier() { |
| 244 | + |
| 245 | + @Override |
| 246 | + public Scorer get(boolean randomAccess) throws IOException { |
| 247 | + throw new UnsupportedOperationException(); |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public long cost() { |
| 252 | + return 42; |
| 253 | + } |
| 254 | + }; |
| 255 | + } |
| 256 | + }; |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + public void testScorerSupplier() throws IOException { |
| 261 | + Directory dir = newDirectory(); |
| 262 | + IndexWriter w = new IndexWriter(dir, newIndexWriterConfig()); |
| 263 | + w.addDocument(new Document()); |
| 264 | + DirectoryReader reader = DirectoryReader.open(w); |
| 265 | + w.close(); |
| 266 | + IndexSearcher s = newSearcher(reader); |
| 267 | + s.setQueryCache(null); |
| 268 | + Weight weight = s.createNormalizedWeight(new DummyQuery(), randomBoolean()); |
| 269 | + // exception when getting the scorer |
| 270 | + expectThrows(UnsupportedOperationException.class, () -> weight.scorer(s.getIndexReader().leaves().get(0))); |
| 271 | + // no exception, means scorerSupplier is delegated |
| 272 | + weight.scorerSupplier(s.getIndexReader().leaves().get(0)); |
| 273 | + reader.close(); |
| 274 | + dir.close(); |
| 275 | + } |
194 | 276 | } |
0 commit comments