lucisearch 0.8.0

Embeddable, in-process search engine — the SQLite/DuckDB of Elasticsearch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! MatchPhraseQuery: terms must appear in order at consecutive positions.
//!
//! Uses position posting lists directly for both conjunction and position
//! verification, eliminating the overhead of maintaining separate posting
//! lists. The lead reader (fewest docs) drives iteration; other readers
//! advance() to each candidate doc, skipping intermediate positions.
//!
//! See [[query-dsl#Full-Text Queries]] and [[architecture-query-execution#Step 9]].

use crate::core::{DocId, FieldId, NO_MORE_DOCS, Result, ScoreMode, Scorer, TwoPhaseIterator};

use crate::query::term::TermQuery;
use crate::query::{BoundQuery, Query, ScorerSupplier};
use crate::search::bm25::{bm25_idf, bm25_score};
use crate::search::searcher::Searcher;
use crate::segment::reader::SegmentReader;

pub struct MatchPhraseQuery {
    pub field: String,
    pub query_text: String,
    pub analyzer: Option<String>,
}

impl Query for MatchPhraseQuery {
    fn bind(&self, searcher: &Searcher, score_mode: ScoreMode) -> Result<Box<dyn BoundQuery>> {
        let analyzer_name = searcher.resolve_search_analyzer(&self.field, self.analyzer.as_deref());
        let analyzer = searcher.analyzers().get(analyzer_name);
        let tokens = analyzer.analyze(&self.query_text);

        if tokens.is_empty() {
            return Ok(Box::new(BoundEmptyQuery));
        }

        if tokens.len() == 1 {
            let tq = TermQuery {
                field: self.field.clone(),
                value: tokens[0].text.clone(),
            };
            return tq.bind(searcher, score_mode);
        }

        let terms: Vec<String> = tokens.iter().map(|t| t.text.clone()).collect();

        Ok(Box::new(BoundPhraseQuery {
            field: self.field.clone(),
            terms,
            total_docs: searcher.total_docs(),
            avg_field_length: searcher.avg_field_length(&self.field),
        }))
    }
}

struct BoundPhraseQuery {
    field: String,
    terms: Vec<String>,
    total_docs: u32,
    avg_field_length: f32,
}

impl BoundQuery for BoundPhraseQuery {
    fn scorer_supplier(&self, reader: &SegmentReader) -> Result<Option<Box<dyn ScorerSupplier>>> {
        let field_id = match reader
            .header()
            .fields
            .iter()
            .find(|f| f.field_name == self.field)
            .map(|f| f.field_id)
        {
            Some(id) => id,
            None => return Ok(None),
        };

        // All terms must exist for the phrase to match
        let mut term_doc_freqs = Vec::new();
        for term in &self.terms {
            let df = reader.doc_freq(field_id, term);
            if df == 0 {
                return Ok(None);
            }
            term_doc_freqs.push(df);
        }

        let cost = *term_doc_freqs.iter().min().unwrap() as u64;

        Ok(Some(Box::new(PhraseScorerSupplier {
            field_id,
            terms: self.terms.clone(),
            term_doc_freqs,
            total_docs: self.total_docs,
            avg_field_length: self.avg_field_length,
            cost,
            segment_data: reader as *const SegmentReader,
        })))
    }
}

struct PhraseScorerSupplier {
    field_id: FieldId,
    terms: Vec<String>,
    term_doc_freqs: Vec<u32>,
    total_docs: u32,
    avg_field_length: f32,
    cost: u64,
    segment_data: *const SegmentReader,
}

unsafe impl Send for PhraseScorerSupplier {}

impl ScorerSupplier for PhraseScorerSupplier {
    fn cost(&self) -> u64 {
        self.cost
    }

    fn scorer(self: Box<Self>) -> Result<Box<dyn Scorer>> {
        let reader = unsafe { &*self.segment_data };

        // Open position posting lists — used for BOTH conjunction and
        // position verification. This eliminates the separate simple posting
        // lists that the old approach maintained in parallel.
        let mut position_readers: Vec<crate::inverted::postings::PositionPostingListReader> =
            Vec::new();
        for term in &self.terms {
            match reader.postings_with_positions(self.field_id, term) {
                Some(r) => position_readers.push(r),
                None => return Ok(Box::new(SimpleIterScorer::empty())),
            }
        }

        // Sort by doc frequency (cheapest = fewest docs = lead iterator)
        // Use index mapping to track original term order for position offsets
        let mut term_order: Vec<usize> = (0..self.terms.len()).collect();
        term_order.sort_by_key(|&i| self.term_doc_freqs[i]);

        let sorted_readers: Vec<_> = term_order
            .iter()
            .map(|&i| {
                std::mem::replace(
                    &mut position_readers[i],
                    crate::inverted::postings::PositionPostingListReader::new(&[]),
                )
            })
            .collect();
        let sorted_offsets: Vec<u32> = term_order.iter().map(|&i| i as u32).collect();

        // Compute phrase IDF as sum of individual IDFs
        let idf: f32 = self
            .term_doc_freqs
            .iter()
            .map(|&df| bm25_idf(self.total_docs, df))
            .sum();

        // Pre-read first entry from lead reader
        let reader_state: Vec<(u32, Vec<u32>)> = sorted_readers
            .iter()
            .map(|_| (u32::MAX, Vec::new()))
            .collect();

        let mut scorer = PhraseScorer {
            readers: sorted_readers,
            term_offsets: sorted_offsets,
            reader_state,
            current: NO_MORE_DOCS,
            idf,
            avg_field_length: self.avg_field_length,
            norms: reader.norms(self.field_id),
            constant_score: reader
                .norms(self.field_id)
                .and_then(|n| n.uniform_norm())
                .map(|dl| bm25_score(idf, 1.0, dl, self.avg_field_length)),
            ptrs_buf: Vec::new(),
            phrase_freq: 0,
        };

        // Position on the first phrase match
        scorer.advance_to_next_phrase();

        Ok(Box::new(scorer))
    }
}

/// Phrase scorer using position posting lists directly for both conjunction
/// and position verification. Eliminates the separate simple posting lists
/// that the old two-phase approach maintained in parallel.
///
/// The lead reader (fewest docs) drives iteration. For each lead doc, all
/// other readers advance to that doc. If all terms are present, positions
/// are checked for consecutive sequence.
struct PhraseScorer<'a> {
    /// Position readers sorted by doc frequency (lead = index 0).
    readers: Vec<crate::inverted::postings::PositionPostingListReader<'a>>,
    /// Original term index for each reader (for position offset calculation).
    /// term_offsets[i] = the position offset of the i-th sorted reader's
    /// term in the original phrase.
    term_offsets: Vec<u32>,
    /// Per-reader: (current_doc_id, current_positions). Reused across docs.
    reader_state: Vec<(u32, Vec<u32>)>,
    /// Current matched doc.
    current: DocId,
    idf: f32,
    avg_field_length: f32,
    /// Cached norms reader for scoring (avoids per-doc lookup).
    norms: Option<crate::inverted::norms::FieldNormsReader<'a>>,
    /// Precomputed constant score when norms are uniform AND phrase_freq=1.
    constant_score: Option<f32>,
    /// Reusable pointer array for two-pointer merge.
    ptrs_buf: Vec<usize>,
    /// Phrase frequency (count of phrase occurrences) in the current matched
    /// document. Used as TF in BM25 scoring.
    /// See [[investigation-20260405-02-phrase-tf-always-one]].
    phrase_freq: u32,
}

// SAFETY: segment_data is only dereferenced within the search call that
// holds the SegmentReader, so the pointer remains valid.
unsafe impl Send for PhraseScorer<'_> {}

impl PhraseScorer<'_> {
    /// Advance lead reader and find the next document where all terms are
    /// present with consecutive positions.
    ///
    /// See [[fix-phrase-nterm-skip-bug]].
    fn advance_to_next_phrase(&mut self) {
        let num_readers = self.readers.len();
        if num_readers == 0 {
            self.current = NO_MORE_DOCS;
            return;
        }

        if num_readers == 2 {
            self.advance_two_term_phrase();
            return;
        }

        // N-term path (3+ terms). Uses advance(current+1) for the lead
        // because next_doc() is only available on PositionPostingListReader
        // and the general TF>1 position check needs reader_state anyway.

        // Initial lead advance — advance(current+1) handles both
        // Scorer::next() and Scorer::advance(target) correctly.
        let mut lead_doc =
            match self.readers[0].advance(DocId::new(if self.current == NO_MORE_DOCS {
                0
            } else {
                self.current.as_u32() + 1
            })) {
                Some(id) => id,
                None => {
                    self.current = NO_MORE_DOCS;
                    return;
                }
            };

        loop {
            // Converge all readers onto the same document. When any reader
            // overshoots, the lead catches up and alignment restarts from
            // reader[1]. lead_doc strictly increases each round.
            'align: loop {
                let target = lead_doc.as_u32();
                let mut aligned = true;

                for i in 1..num_readers {
                    match self.readers[i].advance(lead_doc) {
                        Some(id) if id.as_u32() == target => {}
                        Some(id) => match self.readers[0].advance(id) {
                            Some(new_lead) => {
                                lead_doc = new_lead;
                                aligned = false;
                                break;
                            }
                            None => {
                                self.current = NO_MORE_DOCS;
                                return;
                            }
                        },
                        None => {
                            self.current = NO_MORE_DOCS;
                            return;
                        }
                    }
                }

                if aligned {
                    break 'align;
                }
            }

            // All readers at lead_doc — check positions and count phrase TF
            let freq = if num_readers == 1 {
                1
            } else {
                self.count_phrase_positions(lead_doc)
            };
            if freq > 0 {
                self.phrase_freq = freq;
                self.current = lead_doc;
                return;
            }

            // Position mismatch — advance lead to next doc
            self.current = lead_doc;
            lead_doc = match self.readers[0].advance(DocId::new(lead_doc.as_u32() + 1)) {
                Some(id) => id,
                None => {
                    self.current = NO_MORE_DOCS;
                    return;
                }
            };
        }
    }

    /// 2-term phrase fast path. Uses next_doc() for sequential lead
    /// iteration and an inner convergence loop for catch-up.
    ///
    /// See [[fix-phrase-nterm-skip-bug]].
    fn advance_two_term_phrase(&mut self) {
        let off0 = self.term_offsets[0];
        let off1 = self.term_offsets[1];

        // Initial lead advance uses advance(current+1) to handle
        // Scorer::advance(target) correctly. Subsequent iterations
        // use next_doc() (faster — no target comparison).
        let mut lead_doc =
            match self.readers[0].advance(DocId::new(if self.current == NO_MORE_DOCS {
                0
            } else {
                self.current.as_u32() + 1
            })) {
                Some(id) => id,
                None => {
                    self.current = NO_MORE_DOCS;
                    return;
                }
            };

        loop {
            // Converge both readers onto the same document
            loop {
                match self.readers[1].advance(lead_doc) {
                    Some(id) if id == lead_doc => break,
                    Some(id) => match self.readers[0].advance(id) {
                        Some(new_lead) => {
                            lead_doc = new_lead;
                        }
                        None => {
                            self.current = NO_MORE_DOCS;
                            return;
                        }
                    },
                    None => {
                        self.current = NO_MORE_DOCS;
                        return;
                    }
                }
            }

            // Both readers at lead_doc — check positions
            let tf0 = self.readers[0].current_tf();
            let tf1 = self.readers[1].current_tf();

            if tf0 == 1 && tf1 == 1 {
                // Both terms have TF=1 → at most one phrase occurrence.
                let pos0 = self.readers[0].first_position();
                let pos1 = self.readers[1].first_position();
                if pos0.wrapping_sub(off0) == pos1.wrapping_sub(off1) {
                    self.phrase_freq = 1;
                    self.current = lead_doc;
                    return;
                }
            } else {
                self.reader_state[0].0 = lead_doc.as_u32();
                self.reader_state[0].1.clear();
                self.reader_state[0]
                    .1
                    .extend_from_slice(self.readers[0].positions());
                self.reader_state[1].0 = lead_doc.as_u32();
                self.reader_state[1].1.clear();
                self.reader_state[1]
                    .1
                    .extend_from_slice(self.readers[1].positions());

                let freq = self.count_positions();
                if freq > 0 {
                    self.phrase_freq = freq;
                    self.current = lead_doc;
                    return;
                }
            }

            // Position mismatch — advance to next doc via next_doc()
            self.current = lead_doc;
            lead_doc = match self.readers[0].next_doc() {
                Some(id) => id,
                None => {
                    self.current = NO_MORE_DOCS;
                    return;
                }
            };
        }
    }

    /// Count phrase occurrences in the current document. Returns 0 if no
    /// match. Uses a TF=1 fast path (common case: each term appears once)
    /// without Vec copies, falling back to multi-pointer merge for
    /// high-TF documents.
    fn count_phrase_positions(&mut self, lead_doc: DocId) -> u32 {
        let num_readers = self.readers.len();

        // Fast path: all readers have TF=1 — at most one phrase occurrence.
        if self.readers.iter().all(|r| r.current_tf() == 1) {
            let base = self.readers[0]
                .first_position()
                .wrapping_sub(self.term_offsets[0]);
            let aligned = (1..num_readers).all(|i| {
                self.readers[i]
                    .first_position()
                    .wrapping_sub(self.term_offsets[i])
                    == base
            });
            return if aligned { 1 } else { 0 };
        }

        // General path: copy positions into reader_state, run multi-pointer merge
        let target = lead_doc.as_u32();
        for i in 0..num_readers {
            self.reader_state[i].0 = target;
            self.reader_state[i].1.clear();
            self.reader_state[i]
                .1
                .extend_from_slice(self.readers[i].positions());
        }
        self.count_positions()
    }

    /// Multi-pointer merge over reader_state positions. Returns the number
    /// of phrase occurrences (TF) in the current document.
    /// Uses term_offsets to account for the original phrase order
    /// (readers are sorted by doc frequency, not phrase order).
    fn count_positions(&mut self) -> u32 {
        let num = self.readers.len();
        self.ptrs_buf.clear();
        self.ptrs_buf.resize(num, 0);

        // Find the reader with the smallest term_offset to use as anchor
        let anchor = self
            .term_offsets
            .iter()
            .enumerate()
            .min_by_key(|(_, off)| *off)
            .map(|(i, _)| i)
            .unwrap();
        let anchor_offset = self.term_offsets[anchor];
        let anchor_positions = &self.reader_state[anchor].1;

        let mut count: u32 = 0;

        for &anchor_pos in anchor_positions.iter() {
            let start = anchor_pos - anchor_offset; // phrase start position
            let mut matched = true;

            for i in 0..num {
                if i == anchor {
                    continue;
                }
                let expected = start + self.term_offsets[i];
                let positions = &self.reader_state[i].1;

                while self.ptrs_buf[i] < positions.len() && positions[self.ptrs_buf[i]] < expected {
                    self.ptrs_buf[i] += 1;
                }

                if self.ptrs_buf[i] >= positions.len() || positions[self.ptrs_buf[i]] != expected {
                    matched = false;
                    break;
                }
            }

            if matched {
                count += 1;
            }
        }
        count
    }
}

impl Scorer for PhraseScorer<'_> {
    fn doc_id(&self) -> DocId {
        self.current
    }

    fn next(&mut self) -> DocId {
        self.advance_to_next_phrase();
        self.current
    }

    fn advance(&mut self, target: DocId) -> DocId {
        if self.current < target {
            self.current = DocId::new(target.as_u32().saturating_sub(1));
        }
        self.advance_to_next_phrase();
        self.current
    }

    fn score(&mut self) -> f32 {
        // The constant_score optimization assumes uniform norms AND TF=1.
        // Only use it when phrase_freq=1 (the most common case).
        if self.phrase_freq <= 1 {
            if let Some(cs) = self.constant_score {
                return cs;
            }
        }
        let dl = self
            .norms
            .as_ref()
            .map(|n| n.norm(self.doc_id()))
            .unwrap_or(1.0);
        bm25_score(self.idf, self.phrase_freq as f32, dl, self.avg_field_length)
    }

    fn two_phase(&mut self) -> Option<&mut dyn TwoPhaseIterator> {
        None
    }
}

struct SimpleIterScorer<'a> {
    postings: crate::inverted::postings::PostingListReader<'a>,
    current: DocId,
}

impl<'a> SimpleIterScorer<'a> {
    fn empty() -> Self {
        Self {
            postings: crate::inverted::postings::PostingListReader::new(&[0, 0, 0, 0, 0]),
            current: NO_MORE_DOCS,
        }
    }
}

impl Scorer for SimpleIterScorer<'_> {
    fn doc_id(&self) -> DocId {
        self.current
    }
    fn next(&mut self) -> DocId {
        self.current = match self.postings.next() {
            Some((id, _)) => id,
            None => NO_MORE_DOCS,
        };
        self.current
    }
    fn advance(&mut self, target: DocId) -> DocId {
        while self.current < target && self.current != NO_MORE_DOCS {
            self.next();
        }
        self.current
    }
    fn score(&mut self) -> f32 {
        1.0
    }
    fn two_phase(&mut self) -> Option<&mut dyn TwoPhaseIterator> {
        None
    }
}

struct BoundEmptyQuery;
impl BoundQuery for BoundEmptyQuery {
    fn scorer_supplier(&self, _: &SegmentReader) -> Result<Option<Box<dyn ScorerSupplier>>> {
        Ok(None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analysis::{AnalyzerRegistry, Token};
    use crate::core::SegmentId;
    use crate::mapping::{FieldType, Mapping};
    use crate::segment::builder::SegmentBuilder;

    fn make_tokens(terms: &[&str]) -> Vec<Token> {
        terms
            .iter()
            .enumerate()
            .map(|(i, t)| Token::new(*t, 0, t.len(), i as u32))
            .collect()
    }

    fn build_phrase_store() -> crate::search::segment_store::SegmentStore {
        let schema = Mapping::builder().field("body", FieldType::Text).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);

        // Doc 0: "the quick brown fox" — positions [0,1,2,3]
        builder.add_document(
            &[(
                FieldId::new(0),
                make_tokens(&["the", "quick", "brown", "fox"]),
            )],
            br#"{"body":"the quick brown fox"}"#,
        );

        // Doc 1: "the brown quick fox" — positions [0,1,2,3] but different order
        builder.add_document(
            &[(
                FieldId::new(0),
                make_tokens(&["the", "brown", "quick", "fox"]),
            )],
            br#"{"body":"the brown quick fox"}"#,
        );

        // Doc 2: "quick fox brown" — no "the", different order
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["quick", "fox", "brown"]))],
            br#"{"body":"quick fox brown"}"#,
        );

        let reader = SegmentReader::open(builder.build()).unwrap();
        crate::search::segment_store::SegmentStore::new(
            vec![reader],
            AnalyzerRegistry::new(),
            None,
            None,
        )
    }

    #[test]
    fn phrase_exact_match() {
        let store = build_phrase_store();
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "quick brown".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // "quick brown" at consecutive positions only in doc 0
        assert_eq!(scorer.doc_id(), DocId::new(0));
        assert_eq!(scorer.next(), NO_MORE_DOCS);
    }

    #[test]
    fn phrase_wrong_order_no_match() {
        let store = build_phrase_store();
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "brown quick".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // "brown quick" is consecutive in doc 1 (positions 1,2)
        assert_eq!(scorer.doc_id(), DocId::new(1));
        assert_eq!(scorer.next(), NO_MORE_DOCS);
    }

    #[test]
    fn phrase_no_match() {
        let store = build_phrase_store();
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "fox quick".into(), // never consecutive
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let scorer = supplier.scorer().unwrap();

        // "fox" is always after "quick" in all docs, never before
        // Doc 0: fox@3, quick@1 → not consecutive
        // Doc 1: fox@3, quick@2 → not consecutive
        // Doc 2: fox@1, quick@0 → consecutive! "quick fox" but query is "fox quick"
        // Wait — "fox quick" means fox at position X, quick at X+1. Doc 2 has quick@0, fox@1.
        // So "fox quick" would need fox before quick at consecutive positions.
        // Doc 2: fox@1, quick@0 → fox@1 then quick@1+1=2, but quick is at 0, not 2 → no match

        assert_eq!(scorer.doc_id(), NO_MORE_DOCS);
    }

    #[test]
    fn phrase_single_term_degenerates() {
        let store = build_phrase_store();
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "quick".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // Single term → just a term query. "quick" in docs 0, 1, 2
        let mut ids = Vec::new();
        while scorer.doc_id() != NO_MORE_DOCS {
            ids.push(scorer.doc_id().as_u32());
            scorer.next();
        }
        assert_eq!(ids, vec![0, 1, 2]);
    }

    #[test]
    fn phrase_three_terms() {
        let store = build_phrase_store();
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "the quick brown".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // "the quick brown" at positions 0,1,2 only in doc 0
        assert_eq!(scorer.doc_id(), DocId::new(0));
        assert_eq!(scorer.next(), NO_MORE_DOCS);
    }

    #[test]
    fn phrase_has_positive_score() {
        let store = build_phrase_store();
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "quick brown".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        assert_eq!(scorer.doc_id(), DocId::new(0));
        let score = scorer.score();
        assert!(score > 0.0, "phrase score should be positive, got {score}");
    }

    /// Regression test for [[investigation-20260405-01-phrase-2term-skip-bug]].
    ///
    /// When reader[1] overshoots and reader[0] catches up via advance(),
    /// the catch-up document must still be checked for a phrase match.
    /// The bug: next_doc() after catch-up advances past the catch-up doc.
    #[test]
    fn phrase_2term_catchup_not_skipped() {
        let schema = Mapping::builder().field("body", FieldType::Text).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);

        // Term "alpha": docs 0, 3, 5 (3 docs — rarer, becomes reader[0])
        // Term "beta":  docs 1, 2, 3, 4, 5 (5 docs — more common, becomes reader[1])
        //
        // Iteration 1: reader[0] at doc 0, reader[1].advance(0) → doc 1 (overshoot).
        //   reader[0].advance(1) → doc 3. current = 2, continue.
        // Iteration 2 (BUG): next_doc() on reader[0] at doc 3 → doc 5. Doc 3 skipped!
        // Iteration 2 (FIX): advance(3) on reader[0] at doc 3 → doc 3 (no-op). Doc 3 checked.

        // Doc 0: has alpha, no beta
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["alpha", "gamma"]))],
            b"{}",
        );
        // Doc 1: has beta, no alpha
        builder.add_document(&[(FieldId::new(0), make_tokens(&["beta", "delta"]))], b"{}");
        // Doc 2: has beta, no alpha
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["beta", "epsilon"]))],
            b"{}",
        );
        // Doc 3: "alpha beta" — phrase match (consecutive positions)
        builder.add_document(&[(FieldId::new(0), make_tokens(&["alpha", "beta"]))], b"{}");
        // Doc 4: has beta, no alpha
        builder.add_document(&[(FieldId::new(0), make_tokens(&["beta", "zeta"]))], b"{}");
        // Doc 5: "alpha beta" — another phrase match
        builder.add_document(&[(FieldId::new(0), make_tokens(&["alpha", "beta"]))], b"{}");

        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "alpha beta".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // Both doc 3 and doc 5 must be found
        assert_eq!(
            scorer.doc_id(),
            DocId::new(3),
            "doc 3 must not be skipped after catch-up"
        );
        assert_eq!(scorer.next(), DocId::new(5));
        assert_eq!(scorer.next(), NO_MORE_DOCS);
    }

    /// Same catch-up skip bug as above, but for the N-term (3+) general path.
    ///
    /// The N-term path uses `advance(current + 1)` after catch-up, but
    /// `PositionPostingListReader::advance()` always reads the next stream
    /// entry — it does not check if `current_doc_id >= target` first.
    /// So `advance(new_lead)` when already at `new_lead` skips past it.
    #[test]
    fn phrase_nterm_catchup_not_skipped() {
        let schema = Mapping::builder().field("body", FieldType::Text).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);

        // 3 terms forces the N-term path (2-term fast path only for exactly 2).
        //
        // Term "alpha": docs 0, 3, 5 (df=3, reader[0] after sort)
        // Term "gamma": docs 0, 3, 5 (df=3, reader[1] after sort)
        // Term "beta":  docs 1, 2, 3, 4, 5 (df=5, reader[2] after sort)
        //
        // Iteration 1 (current = NO_MORE_DOCS):
        //   reader[0] (alpha).advance(0) → doc 0
        //   reader[1] (gamma).advance(0) → doc 0. OK.
        //   reader[2] (beta).advance(0) → doc 1 (overshoot!)
        //     reader[0].advance(1) → doc 3. current = 2, continue.
        //
        // Iteration 2 (current = 2):
        //   reader[0].advance(3) — already at doc 3, but advance() reads next → doc 5!
        //   Doc 3 is SKIPPED.

        // Doc 0: alpha, gamma (no beta)
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["alpha", "gamma", "delta"]))],
            b"{}",
        );
        // Doc 1: beta only
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["beta", "delta", "epsilon"]))],
            b"{}",
        );
        // Doc 2: beta only
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["beta", "epsilon", "zeta"]))],
            b"{}",
        );
        // Doc 3: "alpha beta gamma" — phrase match
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["alpha", "beta", "gamma"]))],
            b"{}",
        );
        // Doc 4: beta only
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["beta", "eta", "theta"]))],
            b"{}",
        );
        // Doc 5: "alpha beta gamma" — another phrase match
        builder.add_document(
            &[(FieldId::new(0), make_tokens(&["alpha", "beta", "gamma"]))],
            b"{}",
        );

        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "alpha beta gamma".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // Both doc 3 and doc 5 must be found
        assert_eq!(
            scorer.doc_id(),
            DocId::new(3),
            "N-term: doc 3 must not be skipped after catch-up"
        );
        assert_eq!(scorer.next(), DocId::new(5));
        assert_eq!(scorer.next(), NO_MORE_DOCS);
    }

    /// Regression test for bug 3: `Scorer::advance(target)` must return
    /// a doc `>= target`. The 2-term path used `next_doc()` which iterates
    /// from the reader's current position, ignoring the target — it could
    /// return a match before the requested target.
    #[test]
    fn phrase_advance_respects_target() {
        let schema = Mapping::builder().field("body", FieldType::Text).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);

        // Phrase matches at docs 0, 5, 10. Gaps filled with non-matching docs.
        // Doc 0: "alpha beta"
        builder.add_document(&[(FieldId::new(0), make_tokens(&["alpha", "beta"]))], b"{}");
        // Docs 1-4: filler (no alpha)
        for _ in 1..5 {
            builder.add_document(&[(FieldId::new(0), make_tokens(&["gamma"]))], b"{}");
        }
        // Doc 5: "alpha beta"
        builder.add_document(&[(FieldId::new(0), make_tokens(&["alpha", "beta"]))], b"{}");
        // Docs 6-9: filler
        for _ in 6..10 {
            builder.add_document(&[(FieldId::new(0), make_tokens(&["gamma"]))], b"{}");
        }
        // Doc 10: "alpha beta"
        builder.add_document(&[(FieldId::new(0), make_tokens(&["alpha", "beta"]))], b"{}");

        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "alpha beta".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // Initial match at doc 0
        assert_eq!(scorer.doc_id(), DocId::new(0));

        // advance(7) must return >= 7 — must NOT return doc 5
        let result = scorer.advance(DocId::new(7));
        assert!(
            result >= DocId::new(7),
            "advance(7) returned {result:?}, expected >= DocId(7)"
        );
        assert_eq!(result, DocId::new(10));
    }

    /// Regression test for [[investigation-20260405-02-phrase-tf-always-one]].
    ///
    /// Phrase scorer hardcoded TF=1.0 in BM25, so a doc with the phrase
    /// repeated multiple times scored the same as a doc with one
    /// occurrence (per-token-length terms equal). The fix counts phrase
    /// occurrences and uses that as TF.
    #[test]
    fn phrase_freq_repeated_phrase_scores_higher() {
        let schema = Mapping::builder().field("body", FieldType::Text).build();
        let mut builder = SegmentBuilder::new(SegmentId::new(1), &schema);

        // Doc 0: phrase "alpha beta" appears 3 times
        // body length = 6 tokens
        builder.add_document(
            &[(
                FieldId::new(0),
                make_tokens(&["alpha", "beta", "alpha", "beta", "alpha", "beta"]),
            )],
            b"{}",
        );
        // Doc 1: phrase "alpha beta" appears 1 time
        // body length = 6 tokens (same as doc 0 — eliminates dl-norm differences)
        builder.add_document(
            &[(
                FieldId::new(0),
                make_tokens(&["alpha", "beta", "gamma", "delta", "epsilon", "zeta"]),
            )],
            b"{}",
        );

        let reader = SegmentReader::open(builder.build()).unwrap();
        let store = crate::search::segment_store::SegmentStore::new(
            vec![reader],
            AnalyzerRegistry::new(),
            None,
            None,
        );
        let searcher = Searcher::new(&store);
        let query = MatchPhraseQuery {
            field: "body".into(),
            query_text: "alpha beta".into(),
            analyzer: None,
        };

        let weight = query.bind(&searcher, ScoreMode::Complete).unwrap();
        let supplier = weight
            .scorer_supplier(&searcher.segments()[0])
            .unwrap()
            .unwrap();
        let mut scorer = supplier.scorer().unwrap();

        // Doc 0 has phrase 3 times, doc 1 has it 1 time
        assert_eq!(scorer.doc_id(), DocId::new(0));
        let doc0_score = scorer.score();
        scorer.next();
        assert_eq!(scorer.doc_id(), DocId::new(1));
        let doc1_score = scorer.score();

        // BM25 with higher TF should produce a strictly higher score.
        // Both docs have the same field length, so the only score
        // difference comes from TF.
        assert!(
            doc0_score > doc1_score,
            "doc with 3 phrase occurrences ({doc0_score}) must score higher than \
             doc with 1 occurrence ({doc1_score}) — phrase TF must be counted"
        );
    }
}