|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.util.ArrayList; |
21 | 21 | import java.util.List; |
| 22 | +import java.util.Locale; |
22 | 23 | import java.util.Random; |
23 | 24 | import java.util.function.LongUnaryOperator; |
24 | 25 | import org.apache.lucene.codecs.lucene104.Lucene104Codec; |
25 | 26 | import org.apache.lucene.document.Document; |
26 | 27 | import org.apache.lucene.document.NumericDocValuesField; |
| 28 | +import org.apache.lucene.document.SortedDocValuesField; |
27 | 29 | import org.apache.lucene.document.SortedNumericDocValuesField; |
28 | 30 | import org.apache.lucene.index.DirectoryReader; |
29 | 31 | import org.apache.lucene.index.DocValues; |
|
32 | 34 | import org.apache.lucene.index.IndexWriterConfig; |
33 | 35 | import org.apache.lucene.index.LeafReaderContext; |
34 | 36 | import org.apache.lucene.index.NumericDocValues; |
| 37 | +import org.apache.lucene.index.SortedDocValues; |
35 | 38 | import org.apache.lucene.index.SortedNumericDocValues; |
36 | 39 | import org.apache.lucene.search.BooleanClause.Occur; |
37 | 40 | import org.apache.lucene.store.Directory; |
| 41 | +import org.apache.lucene.util.BytesRef; |
38 | 42 | import org.apache.lucene.util.FixedBitSet; |
39 | 43 |
|
40 | 44 | /** |
@@ -579,6 +583,175 @@ public void testAllBlockTypesWithFakeSkipper() throws Exception { |
579 | 583 | } |
580 | 584 | } |
581 | 585 |
|
| 586 | + /** |
| 587 | + * Tests the intoBitSet bulk path for SortedDocValues ordinal ranges. Dense field with values |
| 588 | + * repeating every 100 docs, queried over an ordinal range that exercises YES, MAYBE, and NO |
| 589 | + * blocks. |
| 590 | + */ |
| 591 | + public void testOrdinalIntoBitSetMatchesLinearScan() throws Exception { |
| 592 | + try (Directory ordDir = newDirectory()) { |
| 593 | + IndexWriterConfig iwc = new IndexWriterConfig().setCodec(new Lucene104Codec()); |
| 594 | + try (IndexWriter w = new IndexWriter(ordDir, iwc)) { |
| 595 | + for (int i = 0; i < DOC_COUNT; i++) { |
| 596 | + Document doc = new Document(); |
| 597 | + // ordinals = i % 100, deterministic, formatted as zero-padded strings |
| 598 | + String val = String.format(Locale.ROOT, "%03d", i % 100); |
| 599 | + doc.add(SortedDocValuesField.indexedField("ord", new BytesRef(val))); |
| 600 | + w.addDocument(doc); |
| 601 | + } |
| 602 | + w.forceMerge(1); |
| 603 | + } |
| 604 | + |
| 605 | + try (DirectoryReader ordReader = DirectoryReader.open(ordDir)) { |
| 606 | + LeafReaderContext ctx = ordReader.leaves().get(0); |
| 607 | + int maxDoc = ctx.reader().maxDoc(); |
| 608 | + int windowSize = DenseConjunctionBulkScorer.WINDOW_SIZE; |
| 609 | + |
| 610 | + // Linear scan reference |
| 611 | + FixedBitSet expected = new FixedBitSet(windowSize); |
| 612 | + SortedDocValues refValues = ctx.reader().getSortedDocValues("ord"); |
| 613 | + for (int d = 0; d < Math.min(maxDoc, windowSize); d++) { |
| 614 | + if (refValues.advanceExact(d)) { |
| 615 | + int ord = refValues.ordValue(); |
| 616 | + if (ord >= 20 && ord <= 40) { |
| 617 | + expected.set(d); |
| 618 | + } |
| 619 | + } |
| 620 | + } |
| 621 | + |
| 622 | + DocValuesSkipper skipper = ctx.reader().getDocValuesSkipper("ord"); |
| 623 | + SortedDocValues dv = ctx.reader().getSortedDocValues("ord"); |
| 624 | + assertNotNull("Field must have a skip index", skipper); |
| 625 | + |
| 626 | + DocValuesRangeIterator iter = DocValuesRangeIterator.forOrdinalRange(dv, skipper, 20, 40); |
| 627 | + iter.approximation().nextDoc(); |
| 628 | + |
| 629 | + FixedBitSet actual = new FixedBitSet(windowSize); |
| 630 | + iter.intoBitSet(Math.min(maxDoc, windowSize), actual, 0); |
| 631 | + |
| 632 | + assertEquals( |
| 633 | + "Ordinal intoBitSet must match linear scan", |
| 634 | + expected.cardinality(), |
| 635 | + actual.cardinality()); |
| 636 | + FixedBitSet diff = expected.clone(); |
| 637 | + diff.xor(actual); |
| 638 | + assertEquals("No bits should differ for ordinal range", 0, diff.cardinality()); |
| 639 | + } |
| 640 | + } |
| 641 | + } |
| 642 | + |
| 643 | + /** |
| 644 | + * Tests that the two-phase iterator is wired for SortedDocValues ordinal ranges with a skip |
| 645 | + * index. |
| 646 | + */ |
| 647 | + public void testOrdinalTwoPhaseIteratorIsWired() throws Exception { |
| 648 | + try (Directory ordDir = newDirectory()) { |
| 649 | + IndexWriterConfig iwc = new IndexWriterConfig().setCodec(new Lucene104Codec()); |
| 650 | + try (IndexWriter w = new IndexWriter(ordDir, iwc)) { |
| 651 | + for (int i = 0; i < 10000; i++) { |
| 652 | + Document doc = new Document(); |
| 653 | + String val = String.format(Locale.ROOT, "%03d", i % 100); |
| 654 | + doc.add(SortedDocValuesField.indexedField("ord", new BytesRef(val))); |
| 655 | + w.addDocument(doc); |
| 656 | + } |
| 657 | + w.forceMerge(1); |
| 658 | + } |
| 659 | + |
| 660 | + try (DirectoryReader ordReader = DirectoryReader.open(ordDir)) { |
| 661 | + LeafReaderContext ctx = ordReader.leaves().get(0); |
| 662 | + Query q = |
| 663 | + SortedDocValuesField.newSlowRangeQuery( |
| 664 | + "ord", new BytesRef("020"), new BytesRef("040"), true, true); |
| 665 | + IndexSearcher searcher = new IndexSearcher(ordReader); |
| 666 | + Weight weight = q.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f); |
| 667 | + ScorerSupplier ss = weight.scorerSupplier(ctx); |
| 668 | + assertNotNull("ScorerSupplier must not be null", ss); |
| 669 | + assertTrue( |
| 670 | + "ScorerSupplier must be a ConstantScoreScorerSupplier", |
| 671 | + ss instanceof ConstantScoreScorerSupplier); |
| 672 | + DocIdSetIterator iter = ((ConstantScoreScorerSupplier) ss).iterator(Long.MAX_VALUE); |
| 673 | + TwoPhaseIterator twoPhase = TwoPhaseIterator.unwrap(iter); |
| 674 | + assertTrue( |
| 675 | + "Ordinal range query on single-valued field with skip index must use a" |
| 676 | + + " DocValuesRangeIterator, but got: " |
| 677 | + + (twoPhase == null ? iter.getClass() : twoPhase.getClass()).getSimpleName(), |
| 678 | + twoPhase instanceof DocValuesRangeIterator); |
| 679 | + } |
| 680 | + } |
| 681 | + } |
| 682 | + |
| 683 | + /** |
| 684 | + * Tests that ordinalRangeIntoBitSet (bulk/fast path) produces the exact same results as per-doc |
| 685 | + * evaluation (slow path) across random data with various densities, range selectivities, and edge |
| 686 | + * cases (all-match, no-match, single-doc blocks, boundary docs). |
| 687 | + */ |
| 688 | + public void testOrdinalRangeIntoBitSetMatchesPerDocEvaluation() throws Exception { |
| 689 | + Random rng = random(); |
| 690 | + for (int iter = 0; iter < 10; iter++) { |
| 691 | + int numDocs = rng.nextInt(4096, 4096 * 5); |
| 692 | + int numUnique = rng.nextInt(10, 500); |
| 693 | + // Random range selectivity in terms of ordinals |
| 694 | + int minOrd = rng.nextInt(0, numUnique / 2); |
| 695 | + int maxOrd = minOrd + rng.nextInt(1, numUnique / 2); |
| 696 | + |
| 697 | + try (Directory dir = newDirectory()) { |
| 698 | + IndexWriterConfig iwc = new IndexWriterConfig().setCodec(new Lucene104Codec()); |
| 699 | + try (IndexWriter w = new IndexWriter(dir, iwc)) { |
| 700 | + for (int i = 0; i < numDocs; i++) { |
| 701 | + Document doc = new Document(); |
| 702 | + // Use string values so ordinal ordering is deterministic and independent of |
| 703 | + // byte-value ordering. |
| 704 | + String val = String.format(Locale.ROOT, "%05d", i % numUnique); |
| 705 | + doc.add(SortedDocValuesField.indexedField("val", new BytesRef(val))); |
| 706 | + w.addDocument(doc); |
| 707 | + } |
| 708 | + w.forceMerge(1); |
| 709 | + } |
| 710 | + |
| 711 | + try (DirectoryReader reader = DirectoryReader.open(dir)) { |
| 712 | + LeafReaderContext ctx = reader.leaves().get(0); |
| 713 | + |
| 714 | + // Slow path: per-doc evaluation via advanceExact + ordValue |
| 715 | + FixedBitSet expected = new FixedBitSet(numDocs); |
| 716 | + SortedDocValues slowDv = ctx.reader().getSortedDocValues("val"); |
| 717 | + for (int d = 0; d < numDocs; d++) { |
| 718 | + if (slowDv.advanceExact(d)) { |
| 719 | + int ord = slowDv.ordValue(); |
| 720 | + if (ord >= minOrd && ord <= maxOrd) { |
| 721 | + expected.set(d); |
| 722 | + } |
| 723 | + } |
| 724 | + } |
| 725 | + |
| 726 | + // Fast path: DocValuesRangeIterator.forOrdinalRange with intoBitSet |
| 727 | + DocValuesSkipper skipper = ctx.reader().getDocValuesSkipper("val"); |
| 728 | + SortedDocValues fastDv = ctx.reader().getSortedDocValues("val"); |
| 729 | + assertNotNull("Field must have a skip index for randomized test", skipper); |
| 730 | + |
| 731 | + DocValuesRangeIterator rangeIter = |
| 732 | + DocValuesRangeIterator.forOrdinalRange(fastDv, skipper, minOrd, maxOrd); |
| 733 | + rangeIter.approximation().nextDoc(); |
| 734 | + |
| 735 | + FixedBitSet actual = new FixedBitSet(numDocs); |
| 736 | + rangeIter.intoBitSet(numDocs, actual, 0); |
| 737 | + |
| 738 | + assertEquals( |
| 739 | + "ordinal intoBitSet must match per-doc evaluation (numDocs=" |
| 740 | + + numDocs |
| 741 | + + ", numUnique=" |
| 742 | + + numUnique |
| 743 | + + ", ordRange=[" |
| 744 | + + minOrd |
| 745 | + + "," |
| 746 | + + maxOrd |
| 747 | + + "])", |
| 748 | + expected, |
| 749 | + actual); |
| 750 | + } |
| 751 | + } |
| 752 | + } |
| 753 | + } |
| 754 | + |
582 | 755 | /** |
583 | 756 | * Tests that rangeIntoBitSet (SIMD/fast path) produces the exact same results as per-doc |
584 | 757 | * evaluation (slow path) across random data with various densities and range selectivities. |
|
0 commit comments