|
24 | 24 | import org.apache.lucene.document.Field;
|
25 | 25 | import org.apache.lucene.document.Field.Store;
|
26 | 26 | import org.apache.lucene.document.LatLonDocValuesField;
|
| 27 | +import org.apache.lucene.document.NumericDocValuesField; |
27 | 28 | import org.apache.lucene.document.StringField;
|
28 | 29 | import org.apache.lucene.document.TextField;
|
29 | 30 | import org.apache.lucene.index.DirectoryReader;
|
| 31 | +import org.apache.lucene.index.IndexReader; |
30 | 32 | import org.apache.lucene.index.IndexWriter;
|
31 | 33 | import org.apache.lucene.index.IndexWriterConfig;
|
32 | 34 | import org.apache.lucene.index.LeafReaderContext;
|
|
36 | 38 | import org.apache.lucene.index.SegmentInfos;
|
37 | 39 | import org.apache.lucene.index.SoftDeletesRetentionMergePolicy;
|
38 | 40 | import org.apache.lucene.index.Term;
|
| 41 | +import org.apache.lucene.search.Explanation; |
| 42 | +import org.apache.lucene.search.IndexOrDocValuesQuery; |
39 | 43 | import org.apache.lucene.search.IndexSearcher;
|
40 | 44 | import org.apache.lucene.search.MatchAllDocsQuery;
|
| 45 | +import org.apache.lucene.search.Query; |
41 | 46 | import org.apache.lucene.search.ScoreDoc;
|
42 | 47 | import org.apache.lucene.search.ScoreMode;
|
| 48 | +import org.apache.lucene.search.Scorer; |
| 49 | +import org.apache.lucene.search.ScorerSupplier; |
43 | 50 | import org.apache.lucene.search.SortField;
|
44 | 51 | import org.apache.lucene.search.SortedNumericSortField;
|
45 | 52 | import org.apache.lucene.search.SortedSetSelector;
|
@@ -420,6 +427,101 @@ public void testAsSequentialAccessBits() throws Exception {
|
420 | 427 | dir.close();
|
421 | 428 | }
|
422 | 429 |
|
| 430 | + private static class UnsupportedQuery extends Query { |
| 431 | + |
| 432 | + @Override |
| 433 | + public String toString(String field) { |
| 434 | + return "Unsupported"; |
| 435 | + } |
| 436 | + |
| 437 | + @Override |
| 438 | + public boolean equals(Object obj) { |
| 439 | + return obj instanceof UnsupportedQuery; |
| 440 | + } |
| 441 | + |
| 442 | + @Override |
| 443 | + public int hashCode() { |
| 444 | + return 42; |
| 445 | + } |
| 446 | + |
| 447 | + @Override |
| 448 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 449 | + return new Weight(this) { |
| 450 | + |
| 451 | + @Override |
| 452 | + public boolean isCacheable(LeafReaderContext ctx) { |
| 453 | + return true; |
| 454 | + } |
| 455 | + |
| 456 | + @Override |
| 457 | + public void extractTerms(Set<Term> terms) { |
| 458 | + throw new UnsupportedOperationException(); |
| 459 | + } |
| 460 | + |
| 461 | + @Override |
| 462 | + public Explanation explain(LeafReaderContext context, int doc) throws IOException { |
| 463 | + throw new UnsupportedOperationException(); |
| 464 | + } |
| 465 | + |
| 466 | + @Override |
| 467 | + public Scorer scorer(LeafReaderContext context) throws IOException { |
| 468 | + throw new UnsupportedOperationException(); |
| 469 | + } |
| 470 | + |
| 471 | + @Override |
| 472 | + public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException { |
| 473 | + return new ScorerSupplier() { |
| 474 | + |
| 475 | + @Override |
| 476 | + public Scorer get(long leadCost) throws IOException { |
| 477 | + throw new UnsupportedOperationException(); |
| 478 | + } |
| 479 | + |
| 480 | + @Override |
| 481 | + public long cost() { |
| 482 | + return context.reader().maxDoc(); |
| 483 | + } |
| 484 | + |
| 485 | + }; |
| 486 | + } |
| 487 | + |
| 488 | + }; |
| 489 | + } |
| 490 | + |
| 491 | + } |
| 492 | + |
| 493 | + public void testAsSequentialBitsUsesRandomAccess() throws IOException { |
| 494 | + try (Directory dir = newDirectory()) { |
| 495 | + try (IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(new KeywordAnalyzer()))) { |
| 496 | + Document doc = new Document(); |
| 497 | + doc.add(new NumericDocValuesField("foo", 5L)); |
| 498 | + // we need more than 8 documents because doc values are artificially penalized by IndexOrDocValuesQuery |
| 499 | + for (int i = 0; i < 10; ++i) { |
| 500 | + w.addDocument(doc); |
| 501 | + } |
| 502 | + w.forceMerge(1); |
| 503 | + try (IndexReader reader = DirectoryReader.open(w)) { |
| 504 | + IndexSearcher searcher = newSearcher(reader); |
| 505 | + searcher.setQueryCache(null); |
| 506 | + Query query = new IndexOrDocValuesQuery( |
| 507 | + new UnsupportedQuery(), NumericDocValuesField.newSlowRangeQuery("foo", 3L, 5L)); |
| 508 | + Weight weight = searcher.createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1f); |
| 509 | + |
| 510 | + // Random access by default |
| 511 | + ScorerSupplier scorerSupplier = weight.scorerSupplier(reader.leaves().get(0)); |
| 512 | + Bits bits = Lucene.asSequentialAccessBits(reader.maxDoc(), scorerSupplier); |
| 513 | + assertNotNull(bits); |
| 514 | + assertTrue(bits.get(0)); |
| 515 | + |
| 516 | + // Moves to sequential access if Bits#get is called more than the number of matches |
| 517 | + ScorerSupplier scorerSupplier2 = weight.scorerSupplier(reader.leaves().get(0)); |
| 518 | + expectThrows(UnsupportedOperationException.class, |
| 519 | + () -> Lucene.asSequentialAccessBits(reader.maxDoc(), scorerSupplier2, reader.maxDoc())); |
| 520 | + } |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 524 | + |
423 | 525 | /**
|
424 | 526 | * Test that the "unmap hack" is detected as supported by lucene.
|
425 | 527 | * This works around the following bug: https://bugs.openjdk.java.net/browse/JDK-4724038
|
|
0 commit comments