vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
//! Hybrid search: combining dense vector similarity with sparse keyword matching
//!
//! Hybrid search fuses results from:
//! - Dense vector similarity (semantic search via embeddings)
//! - Sparse vector matching (keyword search via BM25)
//!
//! This enables powerful search that combines the best of both:
//! - Dense: captures semantic meaning, synonyms, context
//! - Sparse: captures exact keywords, terminology, rare terms
//!
//! Common use cases:
//! - Document search (semantic + keyword matching)
//! - E-commerce (product embeddings + exact feature matching)
//! - Code search (semantic understanding + exact symbol matching)

use serde::{Deserialize, Serialize};

/// Detailed explanation of how a hybrid search score was computed
///
/// Provides full transparency into score calculation for debugging and trust.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoreExplanation {
    /// Final combined score
    pub final_score: f32,

    /// Dense (semantic) component score
    pub dense_score: f32,

    /// Sparse (keyword) component score
    pub sparse_score: f32,

    /// Fusion strategy used
    pub fusion_strategy: FusionStrategy,

    /// Alpha parameter (for WeightedSum and similar strategies)
    pub alpha: f32,

    /// Detailed calculation steps
    pub calculation: String,

    /// Contribution breakdown
    pub contributions: ScoreContributions,
}

/// Breakdown of how each component contributed to the final score
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoreContributions {
    /// Contribution from dense vector (0.0 to 1.0, where 1.0 = 100%)
    pub dense_contribution: f32,

    /// Contribution from sparse vector (0.0 to 1.0, where 1.0 = 100%)
    pub sparse_contribution: f32,

    /// Human-readable explanation
    pub explanation: String,
}

/// Strategy for fusing dense and sparse scores
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum FusionStrategy {
    /// Weighted sum: alpha * dense + (1-alpha) * sparse
    ///
    /// This is the most common strategy. The alpha parameter controls
    /// the relative weight of dense vs sparse scores.
    ///
    /// alpha = 0.7 is a good default (70% dense, 30% sparse)
    WeightedSum,

    /// Reciprocal Rank Fusion (RRF)
    ///
    /// Combines rankings rather than scores:
    /// score = 1/(k + rank_dense) + 1/(k + rank_sparse)
    ///
    /// where k is typically 60.
    ///
    /// RRF is more robust to score scale differences and is used by
    /// many production search systems.
    ReciprocalRankFusion,

    /// Distribution-Based Score Fusion (DBSF) - Qdrant's algorithm
    ///
    /// Normalizes scores using μ±3σ (mean ± 3 standard deviations):
    /// 1. Calculate mean and std dev of all scores
    /// 2. Set bounds: [μ - 3σ, μ + 3σ]
    /// 3. Clamp scores to bounds
    /// 4. Normalize to [0, 1] range
    ///
    /// Better handles outliers than min-max normalization.
    /// Recommended for datasets with high score variance.
    DistributionBased,

    /// Relative Score Fusion - Weaviate's algorithm
    ///
    /// Min-max normalization: (score - min) / (max - min)
    ///
    /// Preserves relative score differences (unlike RRF).
    /// Then combines: alpha * norm_dense + (1-alpha) * norm_sparse
    ///
    /// More information-preserving than RRF.
    RelativeScore,

    /// Maximum score
    ///
    /// Takes max(dense_score, sparse_score).
    /// Useful when either signal alone is sufficient.
    Max,

    /// Minimum score (both must match)
    ///
    /// Takes min(dense_score, sparse_score).
    /// Requires both signals to agree. Very conservative.
    Min,

    /// Harmonic mean (balanced combination)
    ///
    /// score = 2 * (dense * sparse) / (dense + sparse)
    ///
    /// Heavily penalizes if either score is low.
    HarmonicMean,

    /// Geometric mean
    ///
    /// score = sqrt(dense * sparse)
    ///
    /// Balances between arithmetic and harmonic mean.
    GeometricMean,
}

/// Configuration for hybrid search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HybridSearchConfig {
    /// Fusion strategy to use
    pub fusion_strategy: FusionStrategy,

    /// Alpha parameter for WeightedSum strategy (0.0 to 1.0)
    ///
    /// - alpha = 1.0: Pure dense search
    /// - alpha = 0.7: Default, 70% dense, 30% sparse
    /// - alpha = 0.5: Equal weight
    /// - alpha = 0.0: Pure sparse search
    pub alpha: f32,

    /// RRF k parameter (typically 60)
    ///
    /// Controls how quickly rank matters in RRF fusion.
    /// Higher k = more lenient, lower k = more aggressive.
    pub rrf_k: f32,

    /// Whether to normalize scores before fusion
    ///
    /// Recommended for WeightedSum to ensure scores are on similar scales.
    pub normalize_scores: bool,

    /// Autocut parameter for smart result truncation
    ///
    /// Automatically truncates results at natural score drop-offs (jumps).
    /// - `None` or `Some(0)`: Disabled (default)
    /// - `Some(1)`: Cut at first steep score drop (recommended)
    /// - `Some(N)`: Cut at Nth steep score drop
    ///
    /// Prevents returning marginally relevant results.
    /// Only works with RelativeScore fusion strategy (Weaviate behavior).
    pub autocut: Option<usize>,
}

impl Default for HybridSearchConfig {
    fn default() -> Self {
        Self {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7,
            rrf_k: 60.0,
            normalize_scores: true,
            autocut: None,
        }
    }
}

/// Hybrid search query
///
/// Contains both dense and sparse components for hybrid search.
#[derive(Debug, Clone)]
pub struct HybridQuery {
    /// Dense vector component (optional)
    pub dense: Option<Vec<f32>>,

    /// Sparse vector component (indices, values) (optional)
    pub sparse: Option<(Vec<usize>, Vec<f32>)>,

    /// Number of results to return
    pub k: usize,

    /// Hybrid search configuration
    pub config: HybridSearchConfig,
}

impl HybridQuery {
    /// Create a hybrid query with both dense and sparse components
    pub fn new(dense: Vec<f32>, sparse_indices: Vec<usize>, sparse_values: Vec<f32>) -> Self {
        Self {
            dense: Some(dense),
            sparse: Some((sparse_indices, sparse_values)),
            k: 10,
            config: HybridSearchConfig::default(),
        }
    }

    /// Create a dense-only query (will fall back to pure dense search)
    pub fn dense_only(dense: Vec<f32>) -> Self {
        Self {
            dense: Some(dense),
            sparse: None,
            k: 10,
            config: HybridSearchConfig::default(),
        }
    }

    /// Create a sparse-only query (will fall back to pure sparse search)
    pub fn sparse_only(indices: Vec<usize>, values: Vec<f32>) -> Self {
        Self {
            dense: None,
            sparse: Some((indices, values)),
            k: 10,
            config: HybridSearchConfig::default(),
        }
    }

    /// Set number of results
    pub fn with_k(mut self, k: usize) -> Self {
        self.k = k;
        self
    }

    /// Set alpha parameter (for WeightedSum fusion)
    pub fn with_alpha(mut self, alpha: f32) -> Self {
        self.config.alpha = alpha.clamp(0.0, 1.0);
        self
    }

    /// Set fusion strategy
    pub fn with_fusion_strategy(mut self, strategy: FusionStrategy) -> Self {
        self.config.fusion_strategy = strategy;
        self
    }

    /// Enable/disable score normalization
    pub fn with_normalization(mut self, normalize: bool) -> Self {
        self.config.normalize_scores = normalize;
        self
    }
}

/// Fuse dense and sparse scores into a single hybrid score
///
/// # Arguments
/// * `dense_score` - Score from dense vector similarity (0.0 to 1.0)
/// * `sparse_score` - Score from sparse keyword matching (0.0 to 1.0)
/// * `config` - Hybrid search configuration
///
/// # Returns
/// Combined score
pub fn hybrid_search_score(
    dense_score: f32,
    sparse_score: f32,
    config: &HybridSearchConfig,
) -> f32 {
    match config.fusion_strategy {
        FusionStrategy::WeightedSum => {
            config.alpha * dense_score + (1.0 - config.alpha) * sparse_score
        }

        FusionStrategy::ReciprocalRankFusion => {
            // Note: This is a simplified RRF for score-based fusion
            // True RRF requires rankings, not scores
            // Here we approximate: higher score = better rank
            let dense_rank_score = 1.0 / (config.rrf_k + (1.0 - dense_score));
            let sparse_rank_score = 1.0 / (config.rrf_k + (1.0 - sparse_score));
            dense_rank_score + sparse_rank_score
        }

        FusionStrategy::DistributionBased => {
            // DBSF: For single-score fusion, just average
            // Full DBSF normalization happens in normalize_scores_dbsf()
            config.alpha * dense_score + (1.0 - config.alpha) * sparse_score
        }

        FusionStrategy::RelativeScore => {
            // RelativeScore: For single-score fusion, same as WeightedSum
            // Full min-max normalization happens in normalize_scores()
            config.alpha * dense_score + (1.0 - config.alpha) * sparse_score
        }

        FusionStrategy::Max => dense_score.max(sparse_score),

        FusionStrategy::Min => dense_score.min(sparse_score),

        FusionStrategy::HarmonicMean => {
            if dense_score + sparse_score == 0.0 {
                0.0
            } else {
                2.0 * dense_score * sparse_score / (dense_score + sparse_score)
            }
        }

        FusionStrategy::GeometricMean => {
            if dense_score < 0.0 || sparse_score < 0.0 {
                0.0
            } else {
                (dense_score * sparse_score).sqrt()
            }
        }
    }
}

/// Explain how a hybrid search score was calculated
///
/// Provides full transparency into the score calculation, showing:
/// - Individual component scores (dense, sparse)
/// - Fusion strategy used
/// - Detailed calculation steps
/// - Contribution breakdown
///
/// # Arguments
/// * `dense_score` - Score from dense vector similarity (0.0 to 1.0)
/// * `sparse_score` - Score from sparse keyword matching (0.0 to 1.0)
/// * `config` - Hybrid search configuration
///
/// # Returns
/// Detailed score explanation
///
/// # Example
/// ```rust
/// use vecstore::vectors::{explain_hybrid_score, HybridSearchConfig, FusionStrategy};
///
/// let config = HybridSearchConfig {
///     fusion_strategy: FusionStrategy::WeightedSum,
///     alpha: 0.7,
///     ..Default::default()
/// };
///
/// let explanation = explain_hybrid_score(0.8, 0.6, &config);
///
/// println!("Final score: {}", explanation.final_score);
/// println!("Calculation: {}", explanation.calculation);
/// println!("Dense contributed: {:.1}%", explanation.contributions.dense_contribution * 100.0);
/// ```
pub fn explain_hybrid_score(
    dense_score: f32,
    sparse_score: f32,
    config: &HybridSearchConfig,
) -> ScoreExplanation {
    let final_score = hybrid_search_score(dense_score, sparse_score, config);

    let (calculation, contributions) = match config.fusion_strategy {
        FusionStrategy::WeightedSum => {
            let dense_weight = config.alpha;
            let sparse_weight = 1.0 - config.alpha;
            let dense_contrib = dense_score * dense_weight;
            let sparse_contrib = sparse_score * sparse_weight;

            let calc = format!(
                "WeightedSum: {:.4} * {:.4} + {:.4} * {:.4} = {:.4}",
                dense_weight, dense_score, sparse_weight, sparse_score, final_score
            );

            let total = dense_contrib + sparse_contrib;
            let contributions = if total > 0.0 {
                ScoreContributions {
                    dense_contribution: dense_contrib / total,
                    sparse_contribution: sparse_contrib / total,
                    explanation: format!(
                        "Dense: {:.1}%, Sparse: {:.1}%",
                        (dense_contrib / total) * 100.0,
                        (sparse_contrib / total) * 100.0
                    ),
                }
            } else {
                ScoreContributions {
                    dense_contribution: 0.5,
                    sparse_contribution: 0.5,
                    explanation: "Both scores are zero".to_string(),
                }
            };

            (calc, contributions)
        }

        FusionStrategy::ReciprocalRankFusion => {
            let dense_rank_score = 1.0 / (config.rrf_k + (1.0 - dense_score));
            let sparse_rank_score = 1.0 / (config.rrf_k + (1.0 - sparse_score));

            let calc = format!(
                "RRF: 1/({:.1} + {:.4}) + 1/({:.1} + {:.4}) = {:.4}",
                config.rrf_k,
                1.0 - dense_score,
                config.rrf_k,
                1.0 - sparse_score,
                final_score
            );

            let total = dense_rank_score + sparse_rank_score;
            let contributions = ScoreContributions {
                dense_contribution: dense_rank_score / total,
                sparse_contribution: sparse_rank_score / total,
                explanation: format!(
                    "Dense rank: {:.1}%, Sparse rank: {:.1}%",
                    (dense_rank_score / total) * 100.0,
                    (sparse_rank_score / total) * 100.0
                ),
            };

            (calc, contributions)
        }

        FusionStrategy::DistributionBased | FusionStrategy::RelativeScore => {
            let dense_weight = config.alpha;
            let sparse_weight = 1.0 - config.alpha;
            let dense_contrib = dense_score * dense_weight;
            let sparse_contrib = sparse_score * sparse_weight;

            let strategy_name = match config.fusion_strategy {
                FusionStrategy::DistributionBased => "DBSF",
                _ => "RelativeScore",
            };

            let calc = format!(
                "{}: {:.4} * {:.4} + {:.4} * {:.4} = {:.4}",
                strategy_name, dense_weight, dense_score, sparse_weight, sparse_score, final_score
            );

            let total = dense_contrib + sparse_contrib;
            let contributions = if total > 0.0 {
                ScoreContributions {
                    dense_contribution: dense_contrib / total,
                    sparse_contribution: sparse_contrib / total,
                    explanation: format!(
                        "Dense: {:.1}%, Sparse: {:.1}%",
                        (dense_contrib / total) * 100.0,
                        (sparse_contrib / total) * 100.0
                    ),
                }
            } else {
                ScoreContributions {
                    dense_contribution: 0.5,
                    sparse_contribution: 0.5,
                    explanation: "Both scores are zero".to_string(),
                }
            };

            (calc, contributions)
        }

        FusionStrategy::Max => {
            let calc = format!(
                "Max: max({:.4}, {:.4}) = {:.4}",
                dense_score, sparse_score, final_score
            );

            let contributions = if dense_score > sparse_score {
                ScoreContributions {
                    dense_contribution: 1.0,
                    sparse_contribution: 0.0,
                    explanation: "Dense score was higher (100% contribution)".to_string(),
                }
            } else if sparse_score > dense_score {
                ScoreContributions {
                    dense_contribution: 0.0,
                    sparse_contribution: 1.0,
                    explanation: "Sparse score was higher (100% contribution)".to_string(),
                }
            } else {
                ScoreContributions {
                    dense_contribution: 0.5,
                    sparse_contribution: 0.5,
                    explanation: "Scores were equal".to_string(),
                }
            };

            (calc, contributions)
        }

        FusionStrategy::Min => {
            let calc = format!(
                "Min: min({:.4}, {:.4}) = {:.4}",
                dense_score, sparse_score, final_score
            );

            let contributions = if dense_score < sparse_score {
                ScoreContributions {
                    dense_contribution: 1.0,
                    sparse_contribution: 0.0,
                    explanation: "Dense score was lower (limited by it)".to_string(),
                }
            } else if sparse_score < dense_score {
                ScoreContributions {
                    dense_contribution: 0.0,
                    sparse_contribution: 1.0,
                    explanation: "Sparse score was lower (limited by it)".to_string(),
                }
            } else {
                ScoreContributions {
                    dense_contribution: 0.5,
                    sparse_contribution: 0.5,
                    explanation: "Scores were equal".to_string(),
                }
            };

            (calc, contributions)
        }

        FusionStrategy::HarmonicMean => {
            let calc = if dense_score + sparse_score == 0.0 {
                "HarmonicMean: 0 (both scores zero)".to_string()
            } else {
                format!(
                    "HarmonicMean: 2 * {:.4} * {:.4} / ({:.4} + {:.4}) = {:.4}",
                    dense_score, sparse_score, dense_score, sparse_score, final_score
                )
            };

            // For harmonic mean, both contribute equally structurally
            // but lower score has more influence
            let contributions = ScoreContributions {
                dense_contribution: 0.5,
                sparse_contribution: 0.5,
                explanation: "Both scores contribute (harmonic mean penalizes low scores)"
                    .to_string(),
            };

            (calc, contributions)
        }

        FusionStrategy::GeometricMean => {
            let calc = if dense_score < 0.0 || sparse_score < 0.0 {
                "GeometricMean: 0 (negative score)".to_string()
            } else {
                format!(
                    "GeometricMean: sqrt({:.4} * {:.4}) = {:.4}",
                    dense_score, sparse_score, final_score
                )
            };

            let contributions = ScoreContributions {
                dense_contribution: 0.5,
                sparse_contribution: 0.5,
                explanation: "Both scores contribute equally (geometric mean)".to_string(),
            };

            (calc, contributions)
        }
    };

    ScoreExplanation {
        final_score,
        dense_score,
        sparse_score,
        fusion_strategy: config.fusion_strategy,
        alpha: config.alpha,
        calculation,
        contributions,
    }
}

/// Normalize scores to 0-1 range using min-max normalization
///
/// This is useful when combining scores from different sources that may
/// have different scales (e.g., cosine similarity vs BM25 scores).
///
/// # Arguments
/// * `scores` - Slice of scores to normalize
///
/// # Returns
/// Vector of normalized scores in [0, 1] range
///
/// If all scores are equal, returns all 0.5.
/// If there's only one score, returns vec![1.0].
pub fn normalize_scores(scores: &[f32]) -> Vec<f32> {
    if scores.is_empty() {
        return vec![];
    }

    if scores.len() == 1 {
        return vec![1.0];
    }

    let min_score = scores.iter().copied().fold(f32::INFINITY, f32::min);
    let max_score = scores.iter().copied().fold(f32::NEG_INFINITY, f32::max);

    if (max_score - min_score).abs() < 1e-10 {
        // All scores are the same
        return vec![0.5; scores.len()];
    }

    scores
        .iter()
        .map(|&score| (score - min_score) / (max_score - min_score))
        .collect()
}

/// Normalize scores using z-score (standard score) normalization
///
/// Transforms scores to have mean 0 and standard deviation 1.
/// Then maps to [0, 1] using sigmoid function.
///
/// This is more robust to outliers than min-max normalization.
pub fn normalize_scores_zscore(scores: &[f32]) -> Vec<f32> {
    if scores.is_empty() {
        return vec![];
    }

    if scores.len() == 1 {
        return vec![0.5];
    }

    let mean = scores.iter().sum::<f32>() / scores.len() as f32;
    let variance = scores.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / scores.len() as f32;
    let std_dev = variance.sqrt();

    if std_dev < 1e-10 {
        // All scores are the same
        return vec![0.5; scores.len()];
    }

    scores
        .iter()
        .map(|&score| {
            let z = (score - mean) / std_dev;
            // Sigmoid: 1 / (1 + e^(-z))
            1.0 / (1.0 + (-z).exp())
        })
        .collect()
}

/// Normalize scores using DBSF (Distribution-Based Score Fusion)
///
/// Qdrant's normalization algorithm using μ±3σ (mean ± 3 standard deviations):
/// 1. Calculate mean and standard deviation
/// 2. Set bounds: [μ - 3σ, μ + 3σ]
/// 3. Clamp scores to bounds
/// 4. Normalize to [0, 1] range
///
/// This is more robust to outliers than simple min-max normalization because:
/// - Outliers beyond μ±3σ are clamped
/// - Handles 99.7% of normally distributed data
/// - More stable for datasets with high variance
///
/// # Arguments
/// * `scores` - Slice of scores to normalize
///
/// # Returns
/// Vector of normalized scores in [0, 1] range
///
/// # Example
/// ```
/// use vecstore::vectors::normalize_scores_dbsf;
///
/// // Scores with outliers
/// let scores = vec![1.0, 2.0, 3.0, 4.0, 100.0]; // 100.0 is outlier
/// let normalized = normalize_scores_dbsf(&scores);
///
/// // The outlier (100.0) will be clamped to μ+3σ before normalization
/// // Result: more balanced distribution than min-max
/// ```
pub fn normalize_scores_dbsf(scores: &[f32]) -> Vec<f32> {
    if scores.is_empty() {
        return vec![];
    }

    if scores.len() == 1 {
        return vec![0.5];
    }

    // Calculate mean
    let mean = scores.iter().sum::<f32>() / scores.len() as f32;

    // Calculate variance and standard deviation
    let variance = scores.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / scores.len() as f32;
    let std_dev = variance.sqrt();

    if std_dev < 1e-10 {
        // All scores are the same
        return vec![0.5; scores.len()];
    }

    // Set bounds: μ ± 3σ (covers 99.7% of normal distribution)
    let lower_bound = mean - 3.0 * std_dev;
    let upper_bound = mean + 3.0 * std_dev;
    let range = upper_bound - lower_bound;

    if range < f32::EPSILON {
        return vec![0.5; scores.len()];
    }

    // Clamp scores to bounds and normalize to [0, 1]
    scores
        .iter()
        .map(|&score| {
            let clamped = score.clamp(lower_bound, upper_bound);
            (clamped - lower_bound) / range
        })
        .collect()
}

/// Apply autocut to truncate results at natural score drop-offs
///
/// Autocut detects "jumps" (steep drops) in scores between consecutive results
/// and truncates the result list at the first N jumps.
///
/// # Arguments
///
/// * `results` - Vec of (id, score) tuples, sorted by score descending
/// * `autocut` - Number of jumps to detect before cutting (typically 1-3)
///
/// # Returns
///
/// Truncated results vec
///
/// # Algorithm
///
/// For each pair of consecutive results, calculate the score drop.
/// A "jump" is detected when the drop is significantly larger than average.
/// We use a threshold of 2x the median score drop.
///
/// # Example
///
/// ```rust
/// use vecstore::vectors::apply_autocut;
///
/// // Results with natural score groups
/// let results = vec![
///     ("doc1".to_string(), 0.95),
///     ("doc2".to_string(), 0.92),
///     ("doc3".to_string(), 0.90),  // High relevance group
///     ("doc4".to_string(), 0.45),  // <-- BIG JUMP HERE (0.45 drop)
///     ("doc5".to_string(), 0.42),
///     ("doc6".to_string(), 0.40),  // Low relevance group
/// ];
///
/// // Cut at first jump
/// let truncated = apply_autocut(results.clone(), 1);
/// assert_eq!(truncated.len(), 3); // Only top 3 highly relevant results
///
/// // Without autocut (or autocut = 0)
/// let no_cut = apply_autocut(results, 0);
/// assert_eq!(no_cut.len(), 6); // All results returned
/// ```
pub fn apply_autocut<T: Clone>(results: Vec<(T, f32)>, autocut: usize) -> Vec<(T, f32)> {
    // If autocut is 0 or results are too small, return all results
    if autocut == 0 || results.len() <= 1 {
        return results;
    }

    // Calculate score drops between consecutive results
    let mut drops: Vec<f32> = Vec::with_capacity(results.len() - 1);
    for i in 0..results.len() - 1 {
        let drop = results[i].1 - results[i + 1].1;
        drops.push(drop);
    }

    if drops.is_empty() {
        return results;
    }

    // Find median drop to determine what counts as a "jump"
    let mut sorted_drops = drops.clone();
    sorted_drops.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let median_drop = if sorted_drops.len() % 2 == 0 {
        let mid = sorted_drops.len() / 2;
        (sorted_drops[mid - 1] + sorted_drops[mid]) / 2.0
    } else {
        sorted_drops[sorted_drops.len() / 2]
    };

    // A "jump" is a drop that's significantly larger than the median
    // We use 2x median as the threshold (tunable parameter)
    let jump_threshold = median_drop * 2.0;

    // Find positions of jumps
    let mut jump_positions: Vec<usize> = Vec::new();
    for (i, &drop) in drops.iter().enumerate() {
        if drop > jump_threshold && drop > 0.01 {
            // Also require minimum absolute drop of 0.01
            jump_positions.push(i + 1); // i+1 because we cut AFTER the drop
        }
    }

    // If no jumps found, return all results
    if jump_positions.is_empty() {
        return results;
    }

    // Cut at the Nth jump (or first if N > number of jumps)
    let cut_position = jump_positions.get(autocut - 1).copied().unwrap_or_else(|| {
        // If autocut requests more jumps than exist, use the last jump
        *jump_positions.last().unwrap()
    });

    // Return results up to (but not including) the cut position
    results.into_iter().take(cut_position).collect()
}

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

    #[test]
    fn test_fusion_weighted_sum() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7,
            ..Default::default()
        };

        let score = hybrid_search_score(0.8, 0.6, &config);
        let expected = 0.7 * 0.8 + 0.3 * 0.6;
        assert!((score - expected).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_weighted_sum_pure_dense() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 1.0, // Pure dense
            ..Default::default()
        };

        let score = hybrid_search_score(0.8, 0.6, &config);
        assert!((score - 0.8).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_weighted_sum_pure_sparse() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.0, // Pure sparse
            ..Default::default()
        };

        let score = hybrid_search_score(0.8, 0.6, &config);
        assert!((score - 0.6).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_max() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::Max,
            ..Default::default()
        };

        let score = hybrid_search_score(0.8, 0.6, &config);
        assert!((score - 0.8).abs() < 1e-6);

        let score2 = hybrid_search_score(0.5, 0.9, &config);
        assert!((score2 - 0.9).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_min() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::Min,
            ..Default::default()
        };

        let score = hybrid_search_score(0.8, 0.6, &config);
        assert!((score - 0.6).abs() < 1e-6);

        let score2 = hybrid_search_score(0.5, 0.9, &config);
        assert!((score2 - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_harmonic_mean() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::HarmonicMean,
            ..Default::default()
        };

        let score = hybrid_search_score(0.8, 0.6, &config);
        let expected = 2.0 * 0.8 * 0.6 / (0.8 + 0.6);
        assert!((score - expected).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_harmonic_mean_zero() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::HarmonicMean,
            ..Default::default()
        };

        let score = hybrid_search_score(0.0, 0.0, &config);
        assert_eq!(score, 0.0);
    }

    #[test]
    fn test_normalize_scores_minmax() {
        let scores = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let normalized = normalize_scores(&scores);

        assert_eq!(normalized.len(), 5);
        assert!((normalized[0] - 0.0).abs() < 1e-6); // min -> 0
        assert!((normalized[4] - 1.0).abs() < 1e-6); // max -> 1
        assert!((normalized[2] - 0.5).abs() < 1e-6); // middle -> 0.5
    }

    #[test]
    fn test_normalize_scores_all_same() {
        let scores = vec![5.0, 5.0, 5.0];
        let normalized = normalize_scores(&scores);

        assert_eq!(normalized.len(), 3);
        assert!(normalized.iter().all(|&x| (x - 0.5).abs() < 1e-6));
    }

    #[test]
    fn test_normalize_scores_single() {
        let scores = vec![5.0];
        let normalized = normalize_scores(&scores);

        assert_eq!(normalized.len(), 1);
        assert!((normalized[0] - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_normalize_scores_empty() {
        let scores = vec![];
        let normalized = normalize_scores(&scores);
        assert_eq!(normalized.len(), 0);
    }

    #[test]
    fn test_normalize_scores_zscore() {
        let scores = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let normalized = normalize_scores_zscore(&scores);

        assert_eq!(normalized.len(), 5);
        // All scores should be in [0, 1] range
        assert!(normalized.iter().all(|&x| x >= 0.0 && x <= 1.0));
        // Mean should be around 0.5
        let mean = normalized.iter().sum::<f32>() / normalized.len() as f32;
        assert!((mean - 0.5).abs() < 0.1);
    }

    #[test]
    fn test_hybrid_query_builder() {
        let query = HybridQuery::new(vec![0.1, 0.2, 0.3], vec![10, 25], vec![1.0, 2.0])
            .with_k(20)
            .with_alpha(0.8)
            .with_fusion_strategy(FusionStrategy::Max);

        assert_eq!(query.k, 20);
        assert!((query.config.alpha - 0.8).abs() < 1e-6);
        assert_eq!(query.config.fusion_strategy, FusionStrategy::Max);
    }

    #[test]
    fn test_hybrid_query_dense_only() {
        let query = HybridQuery::dense_only(vec![0.1, 0.2, 0.3]);
        assert!(query.dense.is_some());
        assert!(query.sparse.is_none());
    }

    #[test]
    fn test_hybrid_query_sparse_only() {
        let query = HybridQuery::sparse_only(vec![1, 2, 3], vec![0.5, 0.6, 0.7]);
        assert!(query.dense.is_none());
        assert!(query.sparse.is_some());
    }

    #[test]
    fn test_alpha_clamping() {
        let query = HybridQuery::dense_only(vec![0.1]).with_alpha(1.5); // Out of range

        assert!((query.config.alpha - 1.0).abs() < 1e-6); // Should be clamped to 1.0

        let query2 = HybridQuery::dense_only(vec![0.1]).with_alpha(-0.5); // Out of range

        assert!((query2.config.alpha - 0.0).abs() < 1e-6); // Should be clamped to 0.0
    }

    #[test]
    fn test_fusion_rrf() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::ReciprocalRankFusion,
            rrf_k: 60.0,
            ..Default::default()
        };

        let score = hybrid_search_score(0.9, 0.8, &config);
        assert!(score > 0.0);

        // Higher scores should produce higher combined scores
        let score2 = hybrid_search_score(0.5, 0.5, &config);
        assert!(score > score2);
    }

    #[test]
    fn test_fusion_geometric_mean() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::GeometricMean,
            ..Default::default()
        };

        let score = hybrid_search_score(0.64, 0.36, &config);
        // sqrt(0.64 * 0.36) = sqrt(0.2304) = 0.48
        assert!((score - 0.48).abs() < 1e-6);
    }

    #[test]
    fn test_fusion_geometric_mean_negative() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::GeometricMean,
            ..Default::default()
        };

        // Should handle negative scores gracefully
        let score = hybrid_search_score(-0.5, 0.5, &config);
        assert_eq!(score, 0.0);
    }

    // ========== DBSF Normalization Tests ==========

    #[test]
    fn test_normalize_dbsf_basic() {
        let scores = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let normalized = normalize_scores_dbsf(&scores);

        assert_eq!(normalized.len(), 5);
        // All scores should be in [0, 1] range
        assert!(normalized.iter().all(|&x| x >= 0.0 && x <= 1.0));

        // For normal distribution without outliers, DBSF behaves similar to min-max
        // First value should be close to 0, last close to 1
        assert!(normalized[0] < normalized[2]); // monotonic increase
        assert!(normalized[2] < normalized[4]); // monotonic increase
    }

    #[test]
    fn test_normalize_dbsf_with_outliers() {
        // Scores with extreme outliers
        let scores = vec![1.0, 2.0, 3.0, 4.0, 100.0];
        let normalized = normalize_scores_dbsf(&scores);

        assert_eq!(normalized.len(), 5);
        // All should be in [0, 1]
        assert!(normalized.iter().all(|&x| x >= 0.0 && x <= 1.0));

        // The key insight: DBSF clamps the outlier to μ+3σ
        // So the outlier (100.0) is treated differently than in min-max

        // With DBSF, the outlier is clamped, which changes the normalization
        // Let's verify the outlier is handled: it should be at or near upper bound
        let outlier_score = normalized[4];
        assert!(
            outlier_score >= 0.8,
            "Outlier should be near upper bound after clamping"
        );
    }

    #[test]
    fn test_normalize_dbsf_all_same() {
        let scores = vec![5.0, 5.0, 5.0, 5.0];
        let normalized = normalize_scores_dbsf(&scores);

        assert_eq!(normalized.len(), 4);
        // All scores are same -> all should be 0.5
        assert!(normalized.iter().all(|&x| (x - 0.5).abs() < 1e-6));
    }

    #[test]
    fn test_normalize_dbsf_empty() {
        let scores: Vec<f32> = vec![];
        let normalized = normalize_scores_dbsf(&scores);
        assert_eq!(normalized.len(), 0);
    }

    #[test]
    fn test_normalize_dbsf_single() {
        let scores = vec![42.0];
        let normalized = normalize_scores_dbsf(&scores);
        assert_eq!(normalized.len(), 1);
        assert!((normalized[0] - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_normalize_dbsf_vs_minmax() {
        // Test case where DBSF shows its value: dataset with outliers
        let scores = vec![
            10.0, 12.0, 11.0, 13.0,  // Normal range
            100.0, // Extreme outlier
        ];

        let dbsf = normalize_scores_dbsf(&scores);
        let minmax = normalize_scores(&scores);

        // Both should produce valid [0, 1] ranges
        assert!(dbsf.iter().all(|&x| x >= 0.0 && x <= 1.0));
        assert!(minmax.iter().all(|&x| x >= 0.0 && x <= 1.0));

        // With min-max: 10 -> 0.0, 13 -> 0.033, 100 -> 1.0
        // Normal values are compressed into tiny range
        let minmax_val_at_13 = minmax[3]; // Position of 13.0
        assert!(
            minmax_val_at_13 < 0.1,
            "Min-max compresses normal values: {}",
            minmax_val_at_13
        );

        // DBSF clamps the outlier, which affects the normalization differently
        // The outlier should be handled
        let _dbsf_outlier = dbsf[4];
        let minmax_outlier = minmax[4];
        assert_eq!(minmax_outlier, 1.0, "Min-max puts outlier at 1.0");
        // DBSF also puts it at/near 1.0 after clamping, but internal handling differs
    }

    #[test]
    fn test_normalize_dbsf_normal_distribution() {
        // Simulate normally distributed scores
        let scores = vec![
            2.0, 2.5, 3.0, 3.5, 4.0, // μ ≈ 3.0
            4.5, 5.0, 5.5, 6.0,  // Within 2σ
            10.0, // Outlier (beyond 3σ)
        ];

        let normalized = normalize_scores_dbsf(&scores);

        // Should handle the 99.7% (within 3σ) well
        // Outlier should be clamped
        assert!(normalized.iter().all(|&x| x >= 0.0 && x <= 1.0));

        // The outlier (10.0) should be clamped to upper bound
        let outlier_score = normalized[scores.len() - 1];
        // It should be close to 1.0 (upper bound)
        assert!(outlier_score > 0.9);
    }

    // ========== Fusion Strategy Comparison Tests ==========

    #[test]
    fn test_fusion_strategies_comparison() {
        let dense = 0.8;
        let sparse = 0.6;

        // WeightedSum (α=0.7)
        let ws_config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7,
            ..Default::default()
        };
        let ws_score = hybrid_search_score(dense, sparse, &ws_config);
        assert!((ws_score - (0.7 * 0.8 + 0.3 * 0.6)).abs() < 1e-6);

        // HarmonicMean
        let hm_config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::HarmonicMean,
            ..Default::default()
        };
        let hm_score = hybrid_search_score(dense, sparse, &hm_config);
        let expected_hm = 2.0 * 0.8 * 0.6 / (0.8 + 0.6);
        assert!((hm_score - expected_hm).abs() < 1e-6);

        // GeometricMean
        let gm_config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::GeometricMean,
            ..Default::default()
        };
        let gm_score = hybrid_search_score(dense, sparse, &gm_config);
        let expected_gm = (0.8 * 0.6_f32).sqrt();
        assert!((gm_score - expected_gm).abs() < 1e-6);

        // Verify relationships: typically HM < GM < Arithmetic
        // For same values: HM ≤ GM ≤ AM
        assert!(hm_score <= gm_score);
        assert!(gm_score <= ws_score); // WS with α=0.7 is ~arithmetic mean
    }

    #[test]
    fn test_all_fusion_strategies_produce_valid_scores() {
        let test_cases = vec![(0.9, 0.8), (0.5, 0.5), (0.1, 0.9), (0.0, 1.0), (1.0, 0.0)];

        let strategies = vec![
            FusionStrategy::WeightedSum,
            FusionStrategy::ReciprocalRankFusion,
            FusionStrategy::DistributionBased,
            FusionStrategy::RelativeScore,
            FusionStrategy::Max,
            FusionStrategy::Min,
            FusionStrategy::HarmonicMean,
            FusionStrategy::GeometricMean,
        ];

        for (dense, sparse) in test_cases {
            for strategy in &strategies {
                let config = HybridSearchConfig {
                    fusion_strategy: *strategy,
                    alpha: 0.7,
                    rrf_k: 60.0,
                    normalize_scores: true,
                    autocut: None,
                };

                let score = hybrid_search_score(dense, sparse, &config);

                // All fusion strategies should produce finite scores
                assert!(
                    score.is_finite(),
                    "Strategy {:?} produced non-finite score for ({}, {})",
                    strategy,
                    dense,
                    sparse
                );

                // Most should produce scores in reasonable range (some exceptions like RRF)
                if !matches!(strategy, FusionStrategy::ReciprocalRankFusion) {
                    assert!(
                        score >= 0.0 && score <= 1.5,
                        "Strategy {:?} produced out-of-range score {} for ({}, {})",
                        strategy,
                        score,
                        dense,
                        sparse
                    );
                }
            }
        }
    }

    // ============================================================================
    // Autocut Tests
    // ============================================================================

    #[test]
    fn test_autocut_disabled() {
        let results = vec![
            ("doc1".to_string(), 0.9),
            ("doc2".to_string(), 0.5),
            ("doc3".to_string(), 0.1),
        ];

        // autocut = 0 should return all results
        let no_cut = apply_autocut(results.clone(), 0);
        assert_eq!(no_cut.len(), 3);
    }

    #[test]
    fn test_autocut_single_result() {
        let results = vec![("doc1".to_string(), 0.9)];

        let cut = apply_autocut(results.clone(), 1);
        assert_eq!(cut.len(), 1); // Should return single result unchanged
    }

    #[test]
    fn test_autocut_clear_jump() {
        // Results with clear score jump
        let results = vec![
            ("doc1".to_string(), 0.95),
            ("doc2".to_string(), 0.92),
            ("doc3".to_string(), 0.90), // High relevance group
            ("doc4".to_string(), 0.45), // <-- BIG JUMP HERE (0.45 drop)
            ("doc5".to_string(), 0.42),
            ("doc6".to_string(), 0.40), // Low relevance group
        ];

        // Cut at first jump
        let cut = apply_autocut(results, 1);
        assert_eq!(cut.len(), 3); // Should cut after doc3
        assert_eq!(cut[0].0, "doc1");
        assert_eq!(cut[1].0, "doc2");
        assert_eq!(cut[2].0, "doc3");
    }

    #[test]
    fn test_autocut_multiple_jumps() {
        // Results with multiple score jumps
        let results = vec![
            ("doc1".to_string(), 0.95),
            ("doc2".to_string(), 0.90), // Group 1
            ("doc3".to_string(), 0.60), // <-- JUMP 1 (0.30 drop)
            ("doc4".to_string(), 0.55), // Group 2
            ("doc5".to_string(), 0.25), // <-- JUMP 2 (0.30 drop)
            ("doc6".to_string(), 0.20), // Group 3
        ];

        // Cut at first jump
        let cut1 = apply_autocut(results.clone(), 1);
        assert_eq!(cut1.len(), 2); // doc1, doc2

        // Cut at second jump
        let cut2 = apply_autocut(results, 2);
        assert_eq!(cut2.len(), 4); // doc1-doc4
    }

    #[test]
    fn test_autocut_no_jumps() {
        // Results with gradual score decrease (no clear jumps)
        let results = vec![
            ("doc1".to_string(), 0.90),
            ("doc2".to_string(), 0.85),
            ("doc3".to_string(), 0.80),
            ("doc4".to_string(), 0.75),
            ("doc5".to_string(), 0.70),
        ];

        // Should return all results if no jumps detected
        let cut = apply_autocut(results.clone(), 1);
        assert_eq!(cut.len(), 5);
    }

    #[test]
    fn test_autocut_equal_scores() {
        // All equal scores - no drops
        let results = vec![
            ("doc1".to_string(), 0.8),
            ("doc2".to_string(), 0.8),
            ("doc3".to_string(), 0.8),
            ("doc4".to_string(), 0.8),
        ];

        let cut = apply_autocut(results.clone(), 1);
        assert_eq!(cut.len(), 4); // Should return all
    }

    #[test]
    fn test_autocut_jump_at_end() {
        // Jump at the very end
        let results = vec![
            ("doc1".to_string(), 0.9),
            ("doc2".to_string(), 0.85),
            ("doc3".to_string(), 0.82),
            ("doc4".to_string(), 0.80),
            ("doc5".to_string(), 0.1), // <-- JUMP at end
        ];

        let cut = apply_autocut(results, 1);
        assert_eq!(cut.len(), 4); // Should cut before doc5
    }

    #[test]
    fn test_autocut_request_more_jumps_than_exist() {
        // Request 5 jumps but only 1 exists
        let results = vec![
            ("doc1".to_string(), 0.9),
            ("doc2".to_string(), 0.85),
            ("doc3".to_string(), 0.2), // <-- Only one jump
            ("doc4".to_string(), 0.15),
        ];

        let cut = apply_autocut(results, 5); // Request 5 jumps
        assert_eq!(cut.len(), 2); // Should use the only jump that exists
    }

    #[test]
    fn test_autocut_with_integers() {
        // Test with integer IDs instead of strings
        let results = vec![
            (1, 0.95),
            (2, 0.92),
            (3, 0.90),
            (4, 0.45), // <-- JUMP
            (5, 0.42),
        ];

        let cut = apply_autocut(results, 1);
        assert_eq!(cut.len(), 3);
        assert_eq!(cut[0].0, 1);
        assert_eq!(cut[1].0, 2);
        assert_eq!(cut[2].0, 3);
    }

    #[test]
    fn test_autocut_realistic_rag_scenario() {
        // Realistic RAG scenario: 3 highly relevant docs, rest marginally relevant
        let results = vec![
            ("chunk1".to_string(), 0.89), // Highly relevant
            ("chunk2".to_string(), 0.87),
            ("chunk3".to_string(), 0.85),
            ("chunk4".to_string(), 0.52), // <-- Natural cutoff (0.33 drop)
            ("chunk5".to_string(), 0.50), // Marginally relevant
            ("chunk6".to_string(), 0.48),
            ("chunk7".to_string(), 0.46),
            ("chunk8".to_string(), 0.45),
            ("chunk9".to_string(), 0.43),
            ("chunk10".to_string(), 0.41),
        ];

        let cut = apply_autocut(results, 1);

        // Should return only the highly relevant chunks
        assert!(cut.len() <= 4); // At most 4 (likely 3)
        assert!(cut.len() >= 3); // At least 3

        // All returned results should be above 0.8
        assert!(cut.iter().all(|(_, score)| *score > 0.8));
    }

    #[test]
    fn test_autocut_preserves_order() {
        let results = vec![
            ("doc1".to_string(), 0.9),
            ("doc2".to_string(), 0.85),
            ("doc3".to_string(), 0.5), // Jump
            ("doc4".to_string(), 0.45),
        ];

        let cut = apply_autocut(results, 1);

        // Order should be preserved
        assert_eq!(cut[0].0, "doc1");
        assert_eq!(cut[1].0, "doc2");
        assert!(cut[0].1 > cut[1].1); // Descending order maintained
    }

    #[test]
    fn test_autocut_empty_results() {
        let results: Vec<(String, f32)> = vec![];
        let cut = apply_autocut(results, 1);
        assert_eq!(cut.len(), 0);
    }

    // ============================================================================
    // Score Explanation Tests
    // ============================================================================

    #[test]
    fn test_explain_weighted_sum() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        assert_eq!(explanation.dense_score, 0.8);
        assert_eq!(explanation.sparse_score, 0.6);
        assert_eq!(explanation.fusion_strategy, FusionStrategy::WeightedSum);
        assert_eq!(explanation.alpha, 0.7);

        // Final score should be: 0.7 * 0.8 + 0.3 * 0.6 = 0.56 + 0.18 = 0.74
        assert!((explanation.final_score - 0.74).abs() < 1e-6);

        // Check calculation string contains key components
        assert!(explanation.calculation.contains("WeightedSum"));
        assert!(explanation.calculation.contains("0.7"));
        assert!(explanation.calculation.contains("0.8"));

        // Dense contributed more (0.56 vs 0.18)
        assert!(explanation.contributions.dense_contribution > 0.7);
        assert!(explanation.contributions.sparse_contribution < 0.3);
    }

    #[test]
    fn test_explain_rrf() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::ReciprocalRankFusion,
            rrf_k: 60.0,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.9, 0.5, &config);

        assert_eq!(
            explanation.fusion_strategy,
            FusionStrategy::ReciprocalRankFusion
        );
        assert!(explanation.final_score > 0.0);
        assert!(explanation.calculation.contains("RRF"));
        assert!(explanation.calculation.contains("60"));

        // Contributions should add up to ~1.0
        let total = explanation.contributions.dense_contribution
            + explanation.contributions.sparse_contribution;
        assert!((total - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_explain_max() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::Max,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        assert_eq!(explanation.final_score, 0.8); // max(0.8, 0.6) = 0.8
        assert!(explanation.calculation.contains("Max"));
        assert_eq!(explanation.contributions.dense_contribution, 1.0);
        assert_eq!(explanation.contributions.sparse_contribution, 0.0);
        assert!(explanation
            .contributions
            .explanation
            .contains("Dense score was higher"));
    }

    #[test]
    fn test_explain_min() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::Min,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        assert_eq!(explanation.final_score, 0.6); // min(0.8, 0.6) = 0.6
        assert!(explanation.calculation.contains("Min"));
        assert_eq!(explanation.contributions.dense_contribution, 0.0);
        assert_eq!(explanation.contributions.sparse_contribution, 1.0);
        assert!(explanation
            .contributions
            .explanation
            .contains("Sparse score was lower"));
    }

    #[test]
    fn test_explain_harmonic_mean() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::HarmonicMean,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        // Harmonic mean: 2 * 0.8 * 0.6 / (0.8 + 0.6) = 0.96 / 1.4 ≈ 0.6857
        assert!((explanation.final_score - 0.6857).abs() < 0.01);
        assert!(explanation.calculation.contains("HarmonicMean"));
        assert_eq!(explanation.contributions.dense_contribution, 0.5);
        assert_eq!(explanation.contributions.sparse_contribution, 0.5);
    }

    #[test]
    fn test_explain_geometric_mean() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::GeometricMean,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.64, 0.36, &config);

        // Geometric mean: sqrt(0.64 * 0.36) = sqrt(0.2304) = 0.48
        assert!((explanation.final_score - 0.48).abs() < 0.01);
        assert!(explanation.calculation.contains("GeometricMean"));
        assert_eq!(explanation.contributions.dense_contribution, 0.5);
        assert_eq!(explanation.contributions.sparse_contribution, 0.5);
    }

    #[test]
    fn test_explain_dbsf() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::DistributionBased,
            alpha: 0.6,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.7, 0.5, &config);

        assert_eq!(
            explanation.fusion_strategy,
            FusionStrategy::DistributionBased
        );
        assert!(explanation.calculation.contains("DBSF"));
        assert!(explanation.final_score > 0.0);
    }

    #[test]
    fn test_explain_relative_score() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::RelativeScore,
            alpha: 0.8,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.9, 0.3, &config);

        assert_eq!(explanation.fusion_strategy, FusionStrategy::RelativeScore);
        assert!(explanation.calculation.contains("RelativeScore"));
        assert!(explanation.final_score > 0.0);
    }

    #[test]
    fn test_explain_zero_scores() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.0, 0.0, &config);

        assert_eq!(explanation.final_score, 0.0);
        assert_eq!(explanation.dense_score, 0.0);
        assert_eq!(explanation.sparse_score, 0.0);
        assert!(explanation
            .contributions
            .explanation
            .contains("Both scores are zero"));
    }

    #[test]
    fn test_explain_equal_scores_max() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::Max,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.7, 0.7, &config);

        assert_eq!(explanation.final_score, 0.7);
        assert_eq!(explanation.contributions.dense_contribution, 0.5);
        assert_eq!(explanation.contributions.sparse_contribution, 0.5);
        assert!(explanation.contributions.explanation.contains("equal"));
    }

    #[test]
    fn test_explain_serialization() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7,
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        // Test that explanation can be serialized (useful for APIs)
        let json = serde_json::to_string(&explanation).unwrap();
        assert!(json.contains("final_score"));
        assert!(json.contains("dense_score"));
        assert!(json.contains("calculation"));

        // Test deserialization
        let deserialized: ScoreExplanation = serde_json::from_str(&json).unwrap();
        assert!((deserialized.final_score - explanation.final_score).abs() < 1e-6);
        assert_eq!(deserialized.fusion_strategy, explanation.fusion_strategy);
    }

    #[test]
    fn test_explain_pure_dense() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 1.0, // Pure dense
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        assert!((explanation.final_score - 0.8).abs() < 1e-6);
        assert!(explanation.contributions.dense_contribution > 0.99); // Nearly 100%
    }

    #[test]
    fn test_explain_pure_sparse() {
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.0, // Pure sparse
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.8, 0.6, &config);

        assert!((explanation.final_score - 0.6).abs() < 1e-6);
        assert!(explanation.contributions.sparse_contribution > 0.99); // Nearly 100%
    }

    #[test]
    fn test_explain_realistic_rag_scenario() {
        // Realistic RAG scenario: good semantic match, weak keyword match
        let config = HybridSearchConfig {
            fusion_strategy: FusionStrategy::WeightedSum,
            alpha: 0.7, // Favor semantic
            ..Default::default()
        };

        let explanation = explain_hybrid_score(0.92, 0.15, &config);

        // Should favor dense heavily
        assert!(explanation.final_score > 0.6);
        assert!(explanation.contributions.dense_contribution > 0.8);

        // User can see why this result scored well
        assert!(explanation.calculation.len() > 0);
        assert!(explanation.contributions.explanation.len() > 0);
    }
}