oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
//! Result reranking for retrieval pipelines.
//!
//! Provides [`Reranker`]: a flexible reranking engine that supports
//! cross-encoder scoring simulation, BM25 reranking, reciprocal rank fusion,
//! score normalisation, top-k selection, threshold filtering, and batch
//! reranking with rich statistics.
//!
//! ## Example
//!
//! ```rust
//! use oxirs_embed::reranker::{Reranker, RerankerConfig, RerankMethod, Document};
//!
//! let config = RerankerConfig {
//!     method: RerankMethod::Bm25,
//!     top_k: 3,
//!     score_threshold: Some(0.0),
//!     normalize_scores: true,
//! };
//! let reranker = Reranker::new(config);
//!
//! let docs = vec![
//!     Document { id: "d1".into(), text: "Rust programming language".into(), initial_score: 0.6 },
//!     Document { id: "d2".into(), text: "Rust toolchain cargo".into(), initial_score: 0.4 },
//! ];
//! let results = reranker.rerank("cargo build", &docs);
//! assert!(!results.is_empty());
//! ```

use std::collections::HashMap;

// ─────────────────────────────────────────────────────────────────────────────
// Public types
// ─────────────────────────────────────────────────────────────────────────────

/// A document candidate to be reranked.
#[derive(Debug, Clone, PartialEq)]
pub struct Document {
    /// Unique identifier.
    pub id: String,
    /// Full text of the document.
    pub text: String,
    /// Score assigned by the initial retriever (e.g. cosine similarity).
    pub initial_score: f64,
}

/// A ranked result produced by the reranker.
#[derive(Debug, Clone, PartialEq)]
pub struct RankedResult {
    /// Document identifier.
    pub id: String,
    /// Reranking score (semantic of the value depends on the chosen method).
    pub score: f64,
    /// 1-based rank position in the final result set.
    pub rank: usize,
    /// Score delta compared with initial retriever score.
    pub rank_shift: f64,
}

/// Reranking method to apply.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RerankMethod {
    /// Simulated cross-encoder scoring: score(query, doc) via token overlap
    /// and length normalisation.
    CrossEncoder,
    /// BM25 reranking using TF×IDF approximation.
    Bm25,
    /// Reciprocal Rank Fusion — combines the initial retriever rank with a
    /// secondary BM25 ranking.
    ReciprocalRankFusion,
}

/// Configuration for the [`Reranker`].
#[derive(Debug, Clone)]
pub struct RerankerConfig {
    /// Reranking algorithm to use.
    pub method: RerankMethod,
    /// Maximum number of results to return after reranking.
    pub top_k: usize,
    /// Minimum score to keep a result (applied after normalisation if enabled).
    /// `None` means no threshold.
    pub score_threshold: Option<f64>,
    /// When `true`, final scores are min-max normalised to `[0, 1]`.
    pub normalize_scores: bool,
}

impl Default for RerankerConfig {
    fn default() -> Self {
        Self {
            method: RerankMethod::Bm25,
            top_k: 10,
            score_threshold: None,
            normalize_scores: false,
        }
    }
}

/// Score distribution statistics over a reranked set.
#[derive(Debug, Clone)]
pub struct RerankStats {
    /// Number of documents in the reranked list.
    pub count: usize,
    /// Minimum score in the result set.
    pub min_score: f64,
    /// Maximum score in the result set.
    pub max_score: f64,
    /// Arithmetic mean of scores.
    pub mean_score: f64,
    /// Standard deviation of scores.
    pub std_dev: f64,
    /// Mean absolute rank shift relative to initial order.
    pub mean_rank_shift: f64,
}

/// Batch input for reranking multiple queries at once.
#[derive(Debug, Clone)]
pub struct BatchRerankInput {
    /// Query string.
    pub query: String,
    /// Candidate documents for this query.
    pub documents: Vec<Document>,
}

/// Batch output for a single query's reranked results.
#[derive(Debug, Clone)]
pub struct BatchRerankOutput {
    /// The original query string.
    pub query: String,
    /// Ranked results for this query.
    pub results: Vec<RankedResult>,
    /// Statistics over the results.
    pub stats: RerankStats,
}

// ─────────────────────────────────────────────────────────────────────────────
// BM25 helpers
// ─────────────────────────────────────────────────────────────────────────────

/// BM25 tuning parameters.
const BM25_K1: f64 = 1.5;
const BM25_B: f64 = 0.75;

/// Tokenise text into lowercase terms.
fn tokenise(text: &str) -> Vec<String> {
    text.split_whitespace()
        .map(|w| {
            w.chars()
                .filter(|c| c.is_alphanumeric())
                .collect::<String>()
                .to_lowercase()
        })
        .filter(|w| !w.is_empty())
        .collect()
}

/// Build term-frequency map for a token list.
fn term_freq(tokens: &[String]) -> HashMap<String, usize> {
    let mut tf = HashMap::new();
    for t in tokens {
        *tf.entry(t.clone()).or_insert(0) += 1;
    }
    tf
}

/// Compute IDF for a term given document frequency and corpus size.
/// Uses the classic Okapi BM25 IDF formula.
fn idf(doc_freq: usize, num_docs: usize) -> f64 {
    let n = num_docs as f64;
    let df = doc_freq as f64;
    ((n - df + 0.5) / (df + 0.5) + 1.0).ln()
}

/// Score a single document against a query using BM25.
fn bm25_score(
    query_terms: &[String],
    doc_tokens: &[String],
    df_map: &HashMap<String, usize>,
    num_docs: usize,
    avg_dl: f64,
) -> f64 {
    let tf_map = term_freq(doc_tokens);
    let dl = doc_tokens.len() as f64;
    let mut score = 0.0_f64;
    for term in query_terms {
        let tf = *tf_map.get(term).unwrap_or(&0) as f64;
        if tf == 0.0 {
            continue;
        }
        let df = *df_map.get(term).unwrap_or(&0);
        let idf_val = idf(df, num_docs);
        let numerator = tf * (BM25_K1 + 1.0);
        let denominator = tf + BM25_K1 * (1.0 - BM25_B + BM25_B * dl / avg_dl.max(1.0));
        score += idf_val * numerator / denominator;
    }
    score
}

// ─────────────────────────────────────────────────────────────────────────────
// Score normalisation helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Min-max normalise a slice of scores to `[0, 1]`.
fn min_max_normalize(scores: &[f64]) -> Vec<f64> {
    let min = scores.iter().cloned().fold(f64::INFINITY, f64::min);
    let max = scores.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
    let range = max - min;
    if range < f64::EPSILON {
        return vec![1.0; scores.len()];
    }
    scores.iter().map(|s| (s - min) / range).collect()
}

/// Z-score normalise a slice of scores (mean 0, std 1).
pub fn z_score_normalize(scores: &[f64]) -> Vec<f64> {
    if scores.is_empty() {
        return Vec::new();
    }
    let n = scores.len() as f64;
    let mean = scores.iter().sum::<f64>() / n;
    let variance = scores.iter().map(|s| (s - mean).powi(2)).sum::<f64>() / n;
    let std = variance.sqrt();
    if std < f64::EPSILON {
        return vec![0.0; scores.len()];
    }
    scores.iter().map(|s| (s - mean) / std).collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Reranker
// ─────────────────────────────────────────────────────────────────────────────

/// Reranker supporting cross-encoder simulation, BM25, and reciprocal rank
/// fusion.
pub struct Reranker {
    config: RerankerConfig,
}

impl Reranker {
    /// Create a new reranker with the given configuration.
    pub fn new(config: RerankerConfig) -> Self {
        Self { config }
    }

    /// Create a reranker with the default configuration.
    pub fn with_defaults() -> Self {
        Self::new(RerankerConfig::default())
    }

    /// Rerank `docs` for `query` and return top-k [`RankedResult`]s.
    pub fn rerank(&self, query: &str, docs: &[Document]) -> Vec<RankedResult> {
        if docs.is_empty() {
            return Vec::new();
        }
        let scores = match self.config.method {
            RerankMethod::CrossEncoder => self.cross_encoder_scores(query, docs),
            RerankMethod::Bm25 => self.bm25_scores(query, docs),
            RerankMethod::ReciprocalRankFusion => self.rrf_scores(query, docs),
        };

        self.finalize(docs, scores)
    }

    /// Rerank multiple (query, docs) pairs in a single call.
    pub fn rerank_batch(&self, inputs: &[BatchRerankInput]) -> Vec<BatchRerankOutput> {
        inputs
            .iter()
            .map(|input| {
                let results = self.rerank(&input.query, &input.documents);
                let stats = self.compute_stats(&results);
                BatchRerankOutput {
                    query: input.query.clone(),
                    results,
                    stats,
                }
            })
            .collect()
    }

    /// Return statistics for the given ranked results.
    pub fn compute_stats(&self, results: &[RankedResult]) -> RerankStats {
        if results.is_empty() {
            return RerankStats {
                count: 0,
                min_score: 0.0,
                max_score: 0.0,
                mean_score: 0.0,
                std_dev: 0.0,
                mean_rank_shift: 0.0,
            };
        }
        let n = results.len() as f64;
        let scores: Vec<f64> = results.iter().map(|r| r.score).collect();
        let min_score = scores.iter().cloned().fold(f64::INFINITY, f64::min);
        let max_score = scores.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
        let mean_score = scores.iter().sum::<f64>() / n;
        let variance = scores.iter().map(|s| (s - mean_score).powi(2)).sum::<f64>() / n;
        let std_dev = variance.sqrt();
        let mean_rank_shift = results.iter().map(|r| r.rank_shift.abs()).sum::<f64>() / n;
        RerankStats {
            count: results.len(),
            min_score,
            max_score,
            mean_score,
            std_dev,
            mean_rank_shift,
        }
    }

    // ── Private scoring methods ──────────────────────────────────────────────

    /// Simulated cross-encoder: token-overlap score normalised by query length.
    fn cross_encoder_scores(&self, query: &str, docs: &[Document]) -> Vec<f64> {
        let query_tokens: Vec<String> = tokenise(query);
        let q_set: std::collections::HashSet<String> = query_tokens.iter().cloned().collect();
        docs.iter()
            .map(|doc| {
                let doc_tokens = tokenise(&doc.text);
                if q_set.is_empty() || doc_tokens.is_empty() {
                    return 0.0;
                }
                let matches = doc_tokens.iter().filter(|t| q_set.contains(*t)).count();
                let tf_norm = matches as f64 / doc_tokens.len() as f64;
                let idf_weight = (matches as f64 + 1.0).ln() / (q_set.len() as f64 + 1.0).ln();
                // Blend with initial score for cross-encoder flavour
                0.6 * tf_norm + 0.2 * idf_weight + 0.2 * doc.initial_score
            })
            .collect()
    }

    /// BM25 scores over the document corpus.
    fn bm25_scores(&self, query: &str, docs: &[Document]) -> Vec<f64> {
        let query_terms = tokenise(query);
        let tokenised: Vec<Vec<String>> = docs.iter().map(|d| tokenise(&d.text)).collect();
        let num_docs = docs.len();
        let total_len: usize = tokenised.iter().map(|t| t.len()).sum();
        let avg_dl = total_len as f64 / num_docs as f64;

        // Build document-frequency map
        let mut df_map: HashMap<String, usize> = HashMap::new();
        for toks in &tokenised {
            let unique: std::collections::HashSet<&String> = toks.iter().collect();
            for t in unique {
                *df_map.entry(t.clone()).or_insert(0) += 1;
            }
        }

        tokenised
            .iter()
            .map(|toks| bm25_score(&query_terms, toks, &df_map, num_docs, avg_dl))
            .collect()
    }

    /// Reciprocal Rank Fusion: combines initial retriever rank + BM25 rank.
    ///
    /// RRF formula: `score = Σ 1 / (k + rank_i)` where `k = 60` (standard).
    fn rrf_scores(&self, query: &str, docs: &[Document]) -> Vec<f64> {
        const K: f64 = 60.0;

        // Rank by initial score (descending)
        let n = docs.len();
        let mut initial_order: Vec<usize> = (0..n).collect();
        initial_order.sort_by(|&a, &b| {
            docs[b]
                .initial_score
                .partial_cmp(&docs[a].initial_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let mut rank_initial = vec![0usize; n];
        for (rank, &idx) in initial_order.iter().enumerate() {
            rank_initial[idx] = rank + 1;
        }

        // Rank by BM25 score (descending)
        let bm25 = self.bm25_scores(query, docs);
        let mut bm25_order: Vec<usize> = (0..n).collect();
        bm25_order.sort_by(|&a, &b| {
            bm25[b]
                .partial_cmp(&bm25[a])
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let mut rank_bm25 = vec![0usize; n];
        for (rank, &idx) in bm25_order.iter().enumerate() {
            rank_bm25[idx] = rank + 1;
        }

        (0..n)
            .map(|i| 1.0 / (K + rank_initial[i] as f64) + 1.0 / (K + rank_bm25[i] as f64))
            .collect()
    }

    /// Apply normalisation, threshold filtering, and top-k selection.
    fn finalize(&self, docs: &[Document], raw_scores: Vec<f64>) -> Vec<RankedResult> {
        // Build initial ranks from initial_score ordering
        let n = docs.len();
        let mut initial_order: Vec<usize> = (0..n).collect();
        initial_order.sort_by(|&a, &b| {
            docs[b]
                .initial_score
                .partial_cmp(&docs[a].initial_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        let mut initial_rank = vec![0usize; n];
        for (rank, &idx) in initial_order.iter().enumerate() {
            initial_rank[idx] = rank + 1;
        }

        // Optionally normalise
        let final_scores = if self.config.normalize_scores {
            min_max_normalize(&raw_scores)
        } else {
            raw_scores.clone()
        };

        // Sort descending by reranked score
        let mut order: Vec<usize> = (0..n).collect();
        order.sort_by(|&a, &b| {
            final_scores[b]
                .partial_cmp(&final_scores[a])
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let mut results: Vec<RankedResult> = order
            .iter()
            .enumerate()
            .map(|(new_rank, &idx)| {
                let rank_shift = initial_rank[idx] as f64 - (new_rank + 1) as f64;
                RankedResult {
                    id: docs[idx].id.clone(),
                    score: final_scores[idx],
                    rank: new_rank + 1,
                    rank_shift,
                }
            })
            .collect();

        // Apply score threshold
        if let Some(threshold) = self.config.score_threshold {
            results.retain(|r| r.score >= threshold);
        }

        // Apply top-k
        results.truncate(self.config.top_k);

        // Re-assign rank after truncation
        for (i, r) in results.iter_mut().enumerate() {
            r.rank = i + 1;
        }

        results
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_docs() -> Vec<Document> {
        vec![
            Document {
                id: "d1".into(),
                text: "Rust is a systems programming language".into(),
                initial_score: 0.9,
            },
            Document {
                id: "d2".into(),
                text: "Python is a high-level scripting language".into(),
                initial_score: 0.7,
            },
            Document {
                id: "d3".into(),
                text: "Cargo is the Rust package manager and build tool".into(),
                initial_score: 0.5,
            },
            Document {
                id: "d4".into(),
                text: "JavaScript runs in the browser".into(),
                initial_score: 0.3,
            },
            Document {
                id: "d5".into(),
                text: "Rust ownership model ensures memory safety".into(),
                initial_score: 0.6,
            },
        ]
    }

    // ── Cross-encoder ────────────────────────────────────────────────────────

    #[test]
    fn test_cross_encoder_rerank_returns_results() {
        let config = RerankerConfig {
            method: RerankMethod::CrossEncoder,
            top_k: 3,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust systems language", &sample_docs());
        assert!(!results.is_empty());
        assert!(results.len() <= 3);
    }

    #[test]
    fn test_cross_encoder_ranks_rust_docs_higher() {
        let config = RerankerConfig {
            method: RerankMethod::CrossEncoder,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust programming", &sample_docs());
        // First result should be a Rust-related document
        assert!(results[0].id == "d1" || results[0].id == "d3" || results[0].id == "d5");
    }

    #[test]
    fn test_cross_encoder_rank_order() {
        let config = RerankerConfig {
            method: RerankMethod::CrossEncoder,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        for (i, result) in results.iter().enumerate() {
            assert_eq!(result.rank, i + 1);
        }
    }

    #[test]
    fn test_cross_encoder_scores_non_negative() {
        let reranker = Reranker::with_defaults();
        let results = reranker.rerank("cargo build", &sample_docs());
        for r in &results {
            assert!(r.score >= 0.0);
        }
    }

    // ── BM25 ─────────────────────────────────────────────────────────────────

    #[test]
    fn test_bm25_rerank_basic() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust ownership memory", &sample_docs());
        assert!(!results.is_empty());
    }

    #[test]
    fn test_bm25_top_k_respected() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 2,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("language programming", &sample_docs());
        assert!(results.len() <= 2);
    }

    #[test]
    fn test_bm25_term_frequency_effect() {
        // A document containing the query term more often should score higher
        let docs = vec![
            Document {
                id: "rare".into(),
                text: "Rust is a language".into(),
                initial_score: 0.5,
            },
            Document {
                id: "frequent".into(),
                text: "Rust Rust Rust Rust Rust performance systems Rust".into(),
                initial_score: 0.5,
            },
        ];
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 2,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &docs);
        // The "frequent" document should rank first
        assert_eq!(results[0].id, "frequent");
    }

    #[test]
    fn test_bm25_scores_are_non_negative() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("systems", &sample_docs());
        for r in &results {
            assert!(r.score >= 0.0);
        }
    }

    // ── Reciprocal Rank Fusion ───────────────────────────────────────────────

    #[test]
    fn test_rrf_rerank_returns_results() {
        let config = RerankerConfig {
            method: RerankMethod::ReciprocalRankFusion,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust cargo build", &sample_docs());
        assert!(!results.is_empty());
    }

    #[test]
    fn test_rrf_scores_positive() {
        let config = RerankerConfig {
            method: RerankMethod::ReciprocalRankFusion,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("language", &sample_docs());
        for r in &results {
            assert!(r.score > 0.0);
        }
    }

    #[test]
    fn test_rrf_top_k_applied() {
        let config = RerankerConfig {
            method: RerankMethod::ReciprocalRankFusion,
            top_k: 2,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("language", &sample_docs());
        assert!(results.len() <= 2);
    }

    // ── Score normalisation ──────────────────────────────────────────────────

    #[test]
    fn test_min_max_normalize_range() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            normalize_scores: true,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        for r in &results {
            assert!(r.score >= 0.0 && r.score <= 1.0 + 1e-10);
        }
    }

    #[test]
    fn test_z_score_normalize_identity_for_equal_values() {
        let scores = vec![5.0, 5.0, 5.0];
        let normalized = z_score_normalize(&scores);
        for v in normalized {
            assert!((v - 0.0).abs() < 1e-10);
        }
    }

    #[test]
    fn test_z_score_normalize_basic() {
        let scores = vec![1.0, 2.0, 3.0];
        let normalized = z_score_normalize(&scores);
        assert_eq!(normalized.len(), 3);
        // Mean of normalised should be ~0
        let mean: f64 = normalized.iter().sum::<f64>() / 3.0;
        assert!(mean.abs() < 1e-10);
    }

    #[test]
    fn test_min_max_normalize_empty() {
        let scores: Vec<f64> = vec![];
        let normalized = min_max_normalize(&scores);
        assert!(normalized.is_empty());
    }

    #[test]
    fn test_min_max_normalize_single_value() {
        let scores = vec![3.7];
        let normalized = min_max_normalize(&scores);
        assert_eq!(normalized.len(), 1);
        assert!((normalized[0] - 1.0).abs() < 1e-10);
    }

    // ── Score threshold ──────────────────────────────────────────────────────

    #[test]
    fn test_score_threshold_filters_low_scores() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 10,
            normalize_scores: true,
            score_threshold: Some(0.5),
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        for r in &results {
            assert!(r.score >= 0.5);
        }
    }

    #[test]
    fn test_score_threshold_zero_keeps_all_non_negative() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 10,
            score_threshold: Some(0.0),
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        for r in &results {
            assert!(r.score >= 0.0);
        }
    }

    // ── Top-k ────────────────────────────────────────────────────────────────

    #[test]
    fn test_top_k_one() {
        let config = RerankerConfig {
            method: RerankMethod::CrossEncoder,
            top_k: 1,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].rank, 1);
    }

    #[test]
    fn test_top_k_larger_than_docs() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 100,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        assert!(results.len() <= sample_docs().len());
    }

    // ── Rank shift ───────────────────────────────────────────────────────────

    #[test]
    fn test_rank_shift_computed() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("ownership memory", &sample_docs());
        // rank_shift values must be finite
        for r in &results {
            assert!(r.rank_shift.is_finite());
        }
    }

    // ── Empty input ──────────────────────────────────────────────────────────

    #[test]
    fn test_empty_docs_returns_empty() {
        let reranker = Reranker::with_defaults();
        let results = reranker.rerank("Rust", &[]);
        assert!(results.is_empty());
    }

    #[test]
    fn test_empty_query_bm25() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("", &sample_docs());
        // All BM25 scores will be 0 — we still get (up to) 5 results
        assert!(results.len() <= 5);
    }

    // ── Batch reranking ──────────────────────────────────────────────────────

    #[test]
    fn test_batch_rerank_multiple_queries() {
        let reranker = Reranker::with_defaults();
        let inputs = vec![
            BatchRerankInput {
                query: "Rust".into(),
                documents: sample_docs(),
            },
            BatchRerankInput {
                query: "Python".into(),
                documents: sample_docs(),
            },
        ];
        let outputs = reranker.rerank_batch(&inputs);
        assert_eq!(outputs.len(), 2);
        assert_eq!(outputs[0].query, "Rust");
        assert_eq!(outputs[1].query, "Python");
    }

    #[test]
    fn test_batch_rerank_stats_populated() {
        let reranker = Reranker::with_defaults();
        let inputs = vec![BatchRerankInput {
            query: "Rust".into(),
            documents: sample_docs(),
        }];
        let outputs = reranker.rerank_batch(&inputs);
        let stats = &outputs[0].stats;
        assert!(stats.count > 0);
        assert!(stats.max_score >= stats.min_score);
    }

    #[test]
    fn test_batch_rerank_empty_inputs() {
        let reranker = Reranker::with_defaults();
        let outputs = reranker.rerank_batch(&[]);
        assert!(outputs.is_empty());
    }

    // ── Statistics ───────────────────────────────────────────────────────────

    #[test]
    fn test_compute_stats_empty_results() {
        let reranker = Reranker::with_defaults();
        let stats = reranker.compute_stats(&[]);
        assert_eq!(stats.count, 0);
        assert_eq!(stats.mean_score, 0.0);
    }

    #[test]
    fn test_compute_stats_single_result() {
        let reranker = Reranker::with_defaults();
        let results = vec![RankedResult {
            id: "d1".into(),
            score: 0.75,
            rank: 1,
            rank_shift: 0.0,
        }];
        let stats = reranker.compute_stats(&results);
        assert_eq!(stats.count, 1);
        assert!((stats.min_score - 0.75).abs() < 1e-10);
        assert!((stats.max_score - 0.75).abs() < 1e-10);
        assert!((stats.mean_score - 0.75).abs() < 1e-10);
    }

    #[test]
    fn test_compute_stats_std_dev() {
        let reranker = Reranker::with_defaults();
        let results = vec![
            RankedResult {
                id: "a".into(),
                score: 1.0,
                rank: 1,
                rank_shift: 0.0,
            },
            RankedResult {
                id: "b".into(),
                score: 3.0,
                rank: 2,
                rank_shift: 0.0,
            },
        ];
        let stats = reranker.compute_stats(&results);
        assert!((stats.std_dev - 1.0).abs() < 1e-10);
    }

    // ── Config defaults ──────────────────────────────────────────────────────

    #[test]
    fn test_reranker_config_default() {
        let cfg = RerankerConfig::default();
        assert_eq!(cfg.method, RerankMethod::Bm25);
        assert_eq!(cfg.top_k, 10);
        assert!(cfg.score_threshold.is_none());
        assert!(!cfg.normalize_scores);
    }

    #[test]
    fn test_tokenise_lowercases_and_strips_punct() {
        let tokens = tokenise("Hello, World! Rust.");
        assert!(tokens.contains(&"hello".to_string()));
        assert!(tokens.contains(&"world".to_string()));
        assert!(tokens.contains(&"rust".to_string()));
    }

    #[test]
    fn test_z_score_normalize_empty() {
        let result = z_score_normalize(&[]);
        assert!(result.is_empty());
    }

    #[test]
    fn test_idf_formula() {
        // When df == 1 and num_docs == 10: idf = ln((10-1+0.5)/(1+0.5)+1) = ln(7.33) ≈ 1.99
        let v = idf(1, 10);
        assert!(v > 0.0);
    }

    #[test]
    fn test_rerank_rank_contiguous() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        for (i, r) in results.iter().enumerate() {
            assert_eq!(r.rank, i + 1);
        }
    }

    #[test]
    fn test_cross_encoder_single_doc() {
        let docs = vec![Document {
            id: "only".into(),
            text: "Rust is great".into(),
            initial_score: 0.8,
        }];
        let config = RerankerConfig {
            method: RerankMethod::CrossEncoder,
            top_k: 1,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &docs);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "only");
    }

    #[test]
    fn test_rrf_single_doc() {
        let docs = vec![Document {
            id: "only".into(),
            text: "unique content here".into(),
            initial_score: 1.0,
        }];
        let config = RerankerConfig {
            method: RerankMethod::ReciprocalRankFusion,
            top_k: 1,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("content", &docs);
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_term_freq_counts_correctly() {
        let tokens = tokenise("rust rust cargo");
        let tf = term_freq(&tokens);
        assert_eq!(*tf.get("rust").unwrap_or(&0), 2);
        assert_eq!(*tf.get("cargo").unwrap_or(&0), 1);
    }

    #[test]
    fn test_document_clone() {
        let doc = Document {
            id: "d1".into(),
            text: "Rust language".into(),
            initial_score: 0.9,
        };
        let cloned = doc.clone();
        assert_eq!(cloned.id, "d1");
        assert!((cloned.initial_score - 0.9).abs() < 1e-10);
    }

    #[test]
    fn test_ranked_result_fields() {
        let r = RankedResult {
            id: "x".into(),
            score: 0.5,
            rank: 2,
            rank_shift: -1.0,
        };
        assert_eq!(r.id, "x");
        assert_eq!(r.rank, 2);
        assert!((r.rank_shift + 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_batch_rerank_stats_count_matches_results() {
        let reranker = Reranker::with_defaults();
        let inputs = vec![BatchRerankInput {
            query: "language".into(),
            documents: sample_docs(),
        }];
        let outputs = reranker.rerank_batch(&inputs);
        assert_eq!(outputs[0].stats.count, outputs[0].results.len());
    }

    #[test]
    fn test_rerank_descending_score_order() {
        let config = RerankerConfig {
            method: RerankMethod::Bm25,
            top_k: 5,
            ..Default::default()
        };
        let reranker = Reranker::new(config);
        let results = reranker.rerank("Rust", &sample_docs());
        for w in results.windows(2) {
            assert!(w[0].score >= w[1].score - 1e-10);
        }
    }
}