oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
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
//! Hybrid search combining dense (vector) and sparse (BM25) retrieval.
//!
//! This module provides a comprehensive hybrid search implementation that combines:
//! - Dense retrieval using embedding vectors (semantic similarity)
//! - Sparse retrieval using BM25 weighting (lexical matching)
//!
//! The combination of both approaches typically yields better retrieval performance
//! than either method alone, as they capture complementary aspects of relevance.

#![allow(clippy::cast_precision_loss)] // Intentional: usize to f32 for scoring

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use tokio::sync::RwLock;

use crate::error::VectorStoreError;
use crate::types::DocumentId;

/// A sparse vector representation for BM25-based retrieval.
///
/// Sparse vectors are efficient for representing term-based document features
/// where most dimensions are zero. Only non-zero values are stored.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SparseVector {
    /// Indices of non-zero elements.
    pub indices: Vec<usize>,
    /// Values at the corresponding indices.
    pub values: Vec<f32>,
    /// The total dimension of the vector space.
    pub dimension: usize,
}

impl SparseVector {
    /// Create a new sparse vector.
    ///
    /// # Arguments
    /// * `indices` - Indices of non-zero elements (must be sorted and unique)
    /// * `values` - Values at the corresponding indices
    /// * `dimension` - Total dimension of the vector space
    ///
    /// # Panics
    /// Panics if indices and values have different lengths.
    #[must_use]
    pub fn new(indices: Vec<usize>, values: Vec<f32>, dimension: usize) -> Self {
        assert_eq!(
            indices.len(),
            values.len(),
            "Indices and values must have the same length"
        );
        Self {
            indices,
            values,
            dimension,
        }
    }

    /// Create an empty sparse vector with the given dimension.
    #[must_use]
    pub fn empty(dimension: usize) -> Self {
        Self {
            indices: Vec::new(),
            values: Vec::new(),
            dimension,
        }
    }

    /// Get the number of non-zero elements.
    #[must_use]
    pub fn nnz(&self) -> usize {
        self.indices.len()
    }

    /// Check if the vector is empty (all zeros).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.indices.is_empty()
    }

    /// Compute the dot product with another sparse vector.
    #[must_use]
    pub fn dot(&self, other: &Self) -> f32 {
        let mut result = 0.0;
        let mut i = 0;
        let mut j = 0;

        while i < self.indices.len() && j < other.indices.len() {
            match self.indices[i].cmp(&other.indices[j]) {
                std::cmp::Ordering::Less => i += 1,
                std::cmp::Ordering::Greater => j += 1,
                std::cmp::Ordering::Equal => {
                    result += self.values[i] * other.values[j];
                    i += 1;
                    j += 1;
                }
            }
        }

        result
    }

    /// Compute the L2 norm of the vector.
    #[must_use]
    pub fn norm(&self) -> f32 {
        self.values.iter().map(|v| v * v).sum::<f32>().sqrt()
    }

    /// Compute cosine similarity with another sparse vector.
    #[must_use]
    pub fn cosine_similarity(&self, other: &Self) -> f32 {
        let dot = self.dot(other);
        let norm_a = self.norm();
        let norm_b = other.norm();

        if norm_a == 0.0 || norm_b == 0.0 {
            return 0.0;
        }

        dot / (norm_a * norm_b)
    }

    /// Convert to a dense vector representation.
    #[must_use]
    pub fn to_dense(&self) -> Vec<f32> {
        let mut dense = vec![0.0; self.dimension];
        for (idx, val) in self.indices.iter().zip(self.values.iter()) {
            if *idx < self.dimension {
                dense[*idx] = *val;
            }
        }
        dense
    }

    /// Create from a dense vector, keeping only non-zero values.
    #[must_use]
    pub fn from_dense(dense: &[f32]) -> Self {
        let mut indices = Vec::new();
        let mut values = Vec::new();

        for (i, &v) in dense.iter().enumerate() {
            if v.abs() > f32::EPSILON {
                indices.push(i);
                values.push(v);
            }
        }

        Self {
            indices,
            values,
            dimension: dense.len(),
        }
    }
}

impl Default for SparseVector {
    fn default() -> Self {
        Self::empty(0)
    }
}

/// Trait for sparse vector storage with similarity search.
#[async_trait]
pub trait SparseVectorStore: Send + Sync {
    /// Insert a document with its sparse vector representation.
    ///
    /// # Arguments
    /// * `id` - The document identifier
    /// * `vector` - The sparse vector representation
    async fn insert(
        &mut self,
        id: DocumentId,
        vector: SparseVector,
    ) -> Result<(), VectorStoreError>;

    /// Search for documents similar to the query vector.
    ///
    /// # Arguments
    /// * `query` - The query sparse vector
    /// * `top_k` - Maximum number of results to return
    ///
    /// # Returns
    /// A vector of `(DocumentId, f32)` pairs sorted by descending score.
    ///
    /// # Errors
    /// Returns an error if the search operation fails.
    async fn search(
        &self,
        query: &SparseVector,
        top_k: usize,
    ) -> Result<Vec<(DocumentId, f32)>, VectorStoreError>;

    /// Get the sparse vector for a document.
    async fn get(&self, id: &DocumentId) -> Result<Option<SparseVector>, VectorStoreError>;

    /// Delete a document from the store.
    async fn delete(&mut self, id: &DocumentId) -> Result<bool, VectorStoreError>;

    /// Get the number of documents in the store.
    async fn count(&self) -> usize;

    /// Clear all documents from the store.
    async fn clear(&mut self) -> Result<(), VectorStoreError>;
}

/// BM25 parameters for scoring.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct BM25Params {
    /// Controls the impact of term frequency saturation (typically 1.2-2.0).
    pub k1: f32,
    /// Controls the impact of document length normalization (typically 0.75).
    pub b: f32,
    /// Smoothing factor for IDF calculation (typically 0.5).
    pub delta: f32,
}

impl Default for BM25Params {
    fn default() -> Self {
        Self {
            k1: 1.5,
            b: 0.75,
            delta: 0.5,
        }
    }
}

/// BM25 encoder for converting text to sparse vectors.
///
/// Implements the BM25 weighting scheme with IDF (Inverse Document Frequency)
/// for effective lexical matching.
pub struct BM25Encoder {
    /// Vocabulary mapping from terms to indices.
    vocabulary: HashMap<String, usize>,
    /// Inverse document frequency for each term.
    idf: HashMap<String, f32>,
    /// Document frequency for each term.
    document_frequencies: HashMap<String, usize>,
    /// Total number of documents indexed.
    total_documents: usize,
    /// Average document length (in terms).
    average_doc_length: f32,
    /// Sum of all document lengths for computing average.
    total_terms: usize,
    /// BM25 parameters.
    params: BM25Params,
    /// Vocabulary size (dimension of sparse vectors).
    vocab_size: usize,
}

impl BM25Encoder {
    /// Create a new BM25 encoder with default parameters.
    #[must_use]
    pub fn new() -> Self {
        Self {
            vocabulary: HashMap::new(),
            idf: HashMap::new(),
            document_frequencies: HashMap::new(),
            total_documents: 0,
            average_doc_length: 0.0,
            total_terms: 0,
            params: BM25Params::default(),
            vocab_size: 0,
        }
    }

    /// Create a BM25 encoder with custom parameters.
    #[must_use]
    pub fn with_params(params: BM25Params) -> Self {
        Self {
            vocabulary: HashMap::new(),
            idf: HashMap::new(),
            document_frequencies: HashMap::new(),
            total_documents: 0,
            average_doc_length: 0.0,
            total_terms: 0,
            params,
            vocab_size: 0,
        }
    }

    /// Get the vocabulary size.
    #[must_use]
    pub fn vocab_size(&self) -> usize {
        self.vocab_size
    }

    /// Get the total number of documents indexed.
    #[must_use]
    pub fn total_documents(&self) -> usize {
        self.total_documents
    }

    /// Get the BM25 parameters.
    #[must_use]
    pub fn params(&self) -> &BM25Params {
        &self.params
    }

    /// Tokenize text into terms.
    fn tokenize(text: &str) -> Vec<String> {
        text.to_lowercase()
            .split(|c: char| !c.is_alphanumeric())
            .filter(|s| !s.is_empty() && s.len() > 1)
            .map(String::from)
            .collect()
    }

    /// Fit the encoder on a corpus of documents.
    ///
    /// This builds the vocabulary and computes IDF values.
    pub fn fit(&mut self, documents: &[&str]) {
        self.vocabulary.clear();
        self.document_frequencies.clear();
        self.idf.clear();
        self.total_documents = documents.len();
        self.total_terms = 0;

        // First pass: build vocabulary and count document frequencies
        for doc in documents {
            let terms = Self::tokenize(doc);
            self.total_terms += terms.len();

            let unique_terms: HashSet<_> = terms.into_iter().collect();
            for term in unique_terms {
                // Add to vocabulary if new
                if !self.vocabulary.contains_key(&term) {
                    let idx = self.vocabulary.len();
                    self.vocabulary.insert(term.clone(), idx);
                }

                // Increment document frequency
                *self.document_frequencies.entry(term).or_insert(0) += 1;
            }
        }

        self.vocab_size = self.vocabulary.len();
        self.average_doc_length = if self.total_documents > 0 {
            self.total_terms as f32 / self.total_documents as f32
        } else {
            0.0
        };

        // Compute IDF for all terms using BM25 IDF variant with +1 smoothing
        let n = self.total_documents as f32;
        for (term, df) in &self.document_frequencies {
            let df_f = *df as f32;
            // BM25 IDF formula: log((N + 1) / (df + delta))
            // This variant ensures IDF is always positive
            let idf = ((n + 1.0) / (df_f + self.params.delta)).ln();
            self.idf.insert(term.clone(), idf.max(f32::EPSILON));
        }
    }

    /// Fit the encoder on owned strings.
    pub fn fit_owned(&mut self, documents: &[String]) {
        let refs: Vec<&str> = documents.iter().map(String::as_str).collect();
        self.fit(&refs);
    }

    /// Encode a single text into a sparse vector.
    ///
    /// # Arguments
    /// * `text` - The text to encode
    ///
    /// # Returns
    /// A sparse vector with BM25-weighted term frequencies.
    #[must_use]
    pub fn encode(&self, text: &str) -> SparseVector {
        if self.vocab_size == 0 {
            return SparseVector::empty(0);
        }

        let terms = Self::tokenize(text);
        let doc_length = terms.len() as f32;

        // Count term frequencies
        let mut term_freqs: HashMap<String, usize> = HashMap::new();
        for term in terms {
            *term_freqs.entry(term).or_insert(0) += 1;
        }

        // Compute BM25 scores
        let mut indices = Vec::new();
        let mut values = Vec::new();

        for (term, tf) in term_freqs {
            if let Some(&idx) = self.vocabulary.get(&term) {
                let idf = self.idf.get(&term).copied().unwrap_or(0.0);
                let tf_f = tf as f32;

                // BM25 term frequency component
                let length_norm = 1.0 - self.params.b
                    + self.params.b * doc_length / self.average_doc_length.max(1.0);
                let tf_score =
                    (tf_f * (self.params.k1 + 1.0)) / (tf_f + self.params.k1 * length_norm);

                let score = idf * tf_score;
                if score > 0.0 {
                    indices.push(idx);
                    values.push(score);
                }
            }
        }

        // Sort by indices for efficient sparse operations
        let mut pairs: Vec<_> = indices.into_iter().zip(values).collect();
        pairs.sort_by_key(|(idx, _)| *idx);

        let (indices, values): (Vec<_>, Vec<_>) = pairs.into_iter().unzip();

        SparseVector::new(indices, values, self.vocab_size)
    }

    /// Encode multiple texts into sparse vectors.
    ///
    /// # Arguments
    /// * `texts` - The texts to encode
    ///
    /// # Returns
    /// A vector of sparse vectors.
    #[must_use]
    pub fn encode_batch(&self, texts: &[&str]) -> Vec<SparseVector> {
        texts.iter().map(|text| self.encode(text)).collect()
    }

    /// Encode multiple owned strings into sparse vectors.
    #[must_use]
    pub fn encode_batch_owned(&self, texts: &[String]) -> Vec<SparseVector> {
        texts.iter().map(|text| self.encode(text)).collect()
    }
}

impl Default for BM25Encoder {
    fn default() -> Self {
        Self::new()
    }
}

/// Result from hybrid search combining dense and sparse scores.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HybridResult {
    /// The document identifier.
    pub document_id: DocumentId,
    /// Score from dense (vector) retrieval.
    pub dense_score: f32,
    /// Score from sparse (BM25) retrieval.
    pub sparse_score: f32,
    /// Combined score after fusion.
    pub combined_score: f32,
    /// Rank in the final result set.
    pub rank: usize,
}

impl HybridResult {
    /// Create a new hybrid result.
    #[must_use]
    pub fn new(
        document_id: DocumentId,
        dense_score: f32,
        sparse_score: f32,
        combined_score: f32,
        rank: usize,
    ) -> Self {
        Self {
            document_id,
            dense_score,
            sparse_score,
            combined_score,
            rank,
        }
    }
}

/// Strategy for fusing dense and sparse retrieval scores.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FusionStrategy {
    /// Weighted sum of normalized scores.
    ///
    /// `Combined = dense_weight * dense_score + sparse_weight * sparse_score`
    WeightedSum {
        /// Weight for dense scores (0.0 to 1.0).
        dense_weight: f32,
        /// Weight for sparse scores (0.0 to 1.0).
        sparse_weight: f32,
    },

    /// Reciprocal Rank Fusion (RRF).
    ///
    /// `RRF = sum(1 / (k + rank_i))` for each ranking.
    /// This method is robust and doesn't require score normalization.
    ReciprocalRankFusion {
        /// The `k` parameter (typically 60).
        k: usize,
    },

    /// Distribution-based normalization fusion.
    ///
    /// Normalizes scores based on their distribution (mean and std dev)
    /// before combining with weights.
    DistributionBased {
        /// Weight for dense scores after normalization.
        dense_weight: f32,
        /// Weight for sparse scores after normalization.
        sparse_weight: f32,
    },
}

impl Default for FusionStrategy {
    fn default() -> Self {
        Self::WeightedSum {
            dense_weight: 0.5,
            sparse_weight: 0.5,
        }
    }
}

impl FusionStrategy {
    /// Create a weighted sum fusion strategy.
    #[must_use]
    pub fn weighted_sum(dense_weight: f32, sparse_weight: f32) -> Self {
        Self::WeightedSum {
            dense_weight,
            sparse_weight,
        }
    }

    /// Create a reciprocal rank fusion strategy.
    #[must_use]
    pub fn rrf(k: usize) -> Self {
        Self::ReciprocalRankFusion { k }
    }

    /// Create a distribution-based fusion strategy.
    #[must_use]
    pub fn distribution_based(dense_weight: f32, sparse_weight: f32) -> Self {
        Self::DistributionBased {
            dense_weight,
            sparse_weight,
        }
    }
}

/// Configuration for hybrid search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HybridConfig {
    /// The fusion strategy to use.
    pub fusion_strategy: FusionStrategy,
    /// Weight for dense scores (used by some strategies).
    pub dense_weight: f32,
    /// Weight for sparse scores (used by some strategies).
    pub sparse_weight: f32,
    /// Whether to normalize scores before fusion.
    pub normalize_scores: bool,
    /// Minimum score threshold for results.
    pub min_score: Option<f32>,
}

impl Default for HybridConfig {
    fn default() -> Self {
        Self {
            fusion_strategy: FusionStrategy::default(),
            dense_weight: 0.5,
            sparse_weight: 0.5,
            normalize_scores: true,
            min_score: None,
        }
    }
}

impl HybridConfig {
    /// Create a new hybrid config with weighted sum fusion.
    #[must_use]
    pub fn weighted_sum(dense_weight: f32, sparse_weight: f32) -> Self {
        Self {
            fusion_strategy: FusionStrategy::weighted_sum(dense_weight, sparse_weight),
            dense_weight,
            sparse_weight,
            normalize_scores: true,
            min_score: None,
        }
    }

    /// Create a new hybrid config with RRF fusion.
    #[must_use]
    pub fn rrf(k: usize) -> Self {
        Self {
            fusion_strategy: FusionStrategy::rrf(k),
            dense_weight: 0.5,
            sparse_weight: 0.5,
            normalize_scores: false,
            min_score: None,
        }
    }

    /// Set the minimum score threshold.
    #[must_use]
    pub fn with_min_score(mut self, min_score: f32) -> Self {
        self.min_score = Some(min_score);
        self
    }

    /// Set whether to normalize scores.
    #[must_use]
    pub fn with_normalize(mut self, normalize: bool) -> Self {
        self.normalize_scores = normalize;
        self
    }
}

/// Hybrid searcher combining dense and sparse retrieval.
pub struct HybridSearcher<S: SparseVectorStore> {
    /// The sparse vector store for BM25-based retrieval.
    sparse_store: S,
    /// The BM25 encoder for query encoding.
    encoder: BM25Encoder,
    /// Configuration for hybrid search.
    config: HybridConfig,
}

impl<S: SparseVectorStore> HybridSearcher<S> {
    /// Create a new hybrid searcher.
    #[must_use]
    pub fn new(sparse_store: S, encoder: BM25Encoder, config: HybridConfig) -> Self {
        Self {
            sparse_store,
            encoder,
            config,
        }
    }

    /// Get a reference to the sparse store.
    #[must_use]
    pub fn sparse_store(&self) -> &S {
        &self.sparse_store
    }

    /// Get a mutable reference to the sparse store.
    pub fn sparse_store_mut(&mut self) -> &mut S {
        &mut self.sparse_store
    }

    /// Get a reference to the encoder.
    #[must_use]
    pub fn encoder(&self) -> &BM25Encoder {
        &self.encoder
    }

    /// Get a reference to the config.
    #[must_use]
    pub fn config(&self) -> &HybridConfig {
        &self.config
    }

    /// Perform hybrid search combining dense and sparse results.
    ///
    /// # Arguments
    /// * `query` - The text query for sparse search
    /// * `dense_results` - Pre-computed dense search results as `(id, score)` pairs
    /// * `top_k` - Maximum number of results to return
    ///
    /// # Returns
    /// A vector of hybrid results sorted by combined score.
    ///
    /// # Errors
    /// Returns an error if the sparse search operation fails.
    pub async fn search(
        &self,
        query: &str,
        dense_results: &[(DocumentId, f32)],
        top_k: usize,
    ) -> Result<Vec<HybridResult>, VectorStoreError> {
        // Encode query for sparse search
        let sparse_query = self.encoder.encode(query);

        // Perform sparse search
        let sparse_results = self.sparse_store.search(&sparse_query, top_k * 2).await?;

        // Fuse results
        let results = self.fuse_results(dense_results, &sparse_results, top_k);

        Ok(results)
    }

    /// Perform hybrid search with a pre-encoded sparse query.
    ///
    /// # Arguments
    /// * `sparse_query` - Pre-encoded sparse query vector
    /// * `dense_results` - Pre-computed dense search results
    /// * `top_k` - Maximum number of results to return
    ///
    /// # Errors
    /// Returns an error if the sparse search operation fails.
    pub async fn search_with_sparse(
        &self,
        sparse_query: &SparseVector,
        dense_results: &[(DocumentId, f32)],
        top_k: usize,
    ) -> Result<Vec<HybridResult>, VectorStoreError> {
        let sparse_results = self.sparse_store.search(sparse_query, top_k * 2).await?;
        let results = self.fuse_results(dense_results, &sparse_results, top_k);
        Ok(results)
    }

    /// Fuse dense and sparse results according to the configured strategy.
    fn fuse_results(
        &self,
        dense_results: &[(DocumentId, f32)],
        sparse_results: &[(DocumentId, f32)],
        top_k: usize,
    ) -> Vec<HybridResult> {
        match &self.config.fusion_strategy {
            FusionStrategy::WeightedSum {
                dense_weight,
                sparse_weight,
            } => self.fuse_weighted_sum(
                dense_results,
                sparse_results,
                *dense_weight,
                *sparse_weight,
                top_k,
            ),
            FusionStrategy::ReciprocalRankFusion { k } => {
                self.fuse_rrf(dense_results, sparse_results, *k, top_k)
            }
            FusionStrategy::DistributionBased {
                dense_weight,
                sparse_weight,
            } => self.fuse_distribution_based(
                dense_results,
                sparse_results,
                *dense_weight,
                *sparse_weight,
                top_k,
            ),
        }
    }

    /// Fuse using weighted sum.
    fn fuse_weighted_sum(
        &self,
        dense_results: &[(DocumentId, f32)],
        sparse_results: &[(DocumentId, f32)],
        dense_weight: f32,
        sparse_weight: f32,
        top_k: usize,
    ) -> Vec<HybridResult> {
        let mut scores: HashMap<DocumentId, (f32, f32)> = HashMap::new();

        // Normalize dense scores if configured
        let (dense_min, dense_max) = if self.config.normalize_scores {
            Self::score_range(dense_results)
        } else {
            (0.0, 1.0)
        };

        let (sparse_min, sparse_max) = if self.config.normalize_scores {
            Self::score_range(sparse_results)
        } else {
            (0.0, 1.0)
        };

        // Collect dense scores
        for (id, score) in dense_results {
            let normalized = Self::normalize_score(*score, dense_min, dense_max);
            scores.entry(id.clone()).or_insert((0.0, 0.0)).0 = normalized;
        }

        // Collect sparse scores
        for (id, score) in sparse_results {
            let normalized = Self::normalize_score(*score, sparse_min, sparse_max);
            scores.entry(id.clone()).or_insert((0.0, 0.0)).1 = normalized;
        }

        // Compute combined scores
        let mut results: Vec<HybridResult> = scores
            .into_iter()
            .map(|(id, (dense, sparse))| {
                let combined = dense_weight * dense + sparse_weight * sparse;
                HybridResult::new(id, dense, sparse, combined, 0)
            })
            .filter(|r| {
                self.config
                    .min_score
                    .is_none_or(|min| r.combined_score >= min)
            })
            .collect();

        // Sort by combined score (descending)
        results.sort_by(|a, b| {
            b.combined_score
                .partial_cmp(&a.combined_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Assign ranks and truncate
        for (i, result) in results.iter_mut().enumerate() {
            result.rank = i;
        }
        results.truncate(top_k);

        results
    }

    /// Fuse using Reciprocal Rank Fusion.
    fn fuse_rrf(
        &self,
        dense_results: &[(DocumentId, f32)],
        sparse_results: &[(DocumentId, f32)],
        k: usize,
        top_k: usize,
    ) -> Vec<HybridResult> {
        let mut rrf_scores: HashMap<DocumentId, (f32, f32, f32)> = HashMap::new();

        // Create sorted rankings
        let mut dense_sorted: Vec<_> = dense_results.to_vec();
        dense_sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let mut sparse_sorted: Vec<_> = sparse_results.to_vec();
        sparse_sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Compute RRF scores from dense ranking
        for (rank, (id, score)) in dense_sorted.iter().enumerate() {
            let rrf = 1.0 / (k as f32 + rank as f32 + 1.0);
            let entry = rrf_scores.entry(id.clone()).or_insert((0.0, 0.0, 0.0));
            entry.0 = *score; // dense score
            entry.2 += rrf; // combined RRF
        }

        // Compute RRF scores from sparse ranking
        for (rank, (id, score)) in sparse_sorted.iter().enumerate() {
            let rrf = 1.0 / (k as f32 + rank as f32 + 1.0);
            let entry = rrf_scores.entry(id.clone()).or_insert((0.0, 0.0, 0.0));
            entry.1 = *score; // sparse score
            entry.2 += rrf; // combined RRF
        }

        // Build results
        let mut results: Vec<HybridResult> = rrf_scores
            .into_iter()
            .map(|(id, (dense, sparse, combined))| {
                HybridResult::new(id, dense, sparse, combined, 0)
            })
            .filter(|r| {
                self.config
                    .min_score
                    .is_none_or(|min| r.combined_score >= min)
            })
            .collect();

        // Sort by RRF score (descending)
        results.sort_by(|a, b| {
            b.combined_score
                .partial_cmp(&a.combined_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Assign ranks and truncate
        for (i, result) in results.iter_mut().enumerate() {
            result.rank = i;
        }
        results.truncate(top_k);

        results
    }

    /// Fuse using distribution-based normalization.
    fn fuse_distribution_based(
        &self,
        dense_results: &[(DocumentId, f32)],
        sparse_results: &[(DocumentId, f32)],
        dense_weight: f32,
        sparse_weight: f32,
        top_k: usize,
    ) -> Vec<HybridResult> {
        let mut scores: HashMap<DocumentId, (f32, f32)> = HashMap::new();

        // Compute statistics for dense scores
        let (dense_mean, dense_std) = Self::score_stats(dense_results);
        let (sparse_mean, sparse_std) = Self::score_stats(sparse_results);

        // Collect and normalize dense scores
        for (id, score) in dense_results {
            let normalized = Self::z_normalize(*score, dense_mean, dense_std);
            scores.entry(id.clone()).or_insert((0.0, 0.0)).0 = normalized;
        }

        // Collect and normalize sparse scores
        for (id, score) in sparse_results {
            let normalized = Self::z_normalize(*score, sparse_mean, sparse_std);
            scores.entry(id.clone()).or_insert((0.0, 0.0)).1 = normalized;
        }

        // Compute combined scores
        let mut results: Vec<HybridResult> = scores
            .into_iter()
            .map(|(id, (dense, sparse))| {
                let combined = dense_weight * dense + sparse_weight * sparse;
                HybridResult::new(id, dense, sparse, combined, 0)
            })
            .filter(|r| {
                self.config
                    .min_score
                    .is_none_or(|min| r.combined_score >= min)
            })
            .collect();

        // Sort by combined score (descending)
        results.sort_by(|a, b| {
            b.combined_score
                .partial_cmp(&a.combined_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Assign ranks and truncate
        for (i, result) in results.iter_mut().enumerate() {
            result.rank = i;
        }
        results.truncate(top_k);

        results
    }

    /// Compute the min and max scores from results.
    fn score_range(results: &[(DocumentId, f32)]) -> (f32, f32) {
        if results.is_empty() {
            return (0.0, 1.0);
        }

        let min = results
            .iter()
            .map(|(_, s)| *s)
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(0.0);
        let max = results
            .iter()
            .map(|(_, s)| *s)
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(1.0);

        (min, max)
    }

    /// Normalize a score to [0, 1] range.
    fn normalize_score(score: f32, min: f32, max: f32) -> f32 {
        if (max - min).abs() < f32::EPSILON {
            return 0.5;
        }
        (score - min) / (max - min)
    }

    /// Compute mean and standard deviation of scores.
    fn score_stats(results: &[(DocumentId, f32)]) -> (f32, f32) {
        if results.is_empty() {
            return (0.0, 1.0);
        }

        let scores: Vec<f32> = results.iter().map(|(_, s)| *s).collect();
        let n = scores.len() as f32;
        let mean = scores.iter().sum::<f32>() / n;
        let variance = scores.iter().map(|s| (s - mean).powi(2)).sum::<f32>() / n;
        let std = variance.sqrt().max(f32::EPSILON);

        (mean, std)
    }

    /// Z-score normalization.
    fn z_normalize(score: f32, mean: f32, std: f32) -> f32 {
        (score - mean) / std
    }
}

/// In-memory sparse vector store for development and testing.
pub struct InMemorySparseStore {
    /// Stored sparse vectors by document ID.
    vectors: RwLock<HashMap<DocumentId, SparseVector>>,
    /// Inverted index: term index -> set of document IDs.
    inverted_index: RwLock<HashMap<usize, HashSet<DocumentId>>>,
}

impl InMemorySparseStore {
    /// Create a new in-memory sparse store.
    #[must_use]
    pub fn new() -> Self {
        Self {
            vectors: RwLock::new(HashMap::new()),
            inverted_index: RwLock::new(HashMap::new()),
        }
    }
}

impl Default for InMemorySparseStore {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl SparseVectorStore for InMemorySparseStore {
    async fn insert(
        &mut self,
        id: DocumentId,
        vector: SparseVector,
    ) -> Result<(), VectorStoreError> {
        // Update inverted index
        {
            let mut index = self.inverted_index.write().await;
            for &idx in &vector.indices {
                index.entry(idx).or_default().insert(id.clone());
            }
        }

        // Store the vector
        {
            let mut vectors = self.vectors.write().await;
            vectors.insert(id, vector);
        }

        Ok(())
    }

    async fn search(
        &self,
        query: &SparseVector,
        top_k: usize,
    ) -> Result<Vec<(DocumentId, f32)>, VectorStoreError> {
        let vectors = self.vectors.read().await;
        let inverted_index = self.inverted_index.read().await;

        // Find candidate documents using inverted index
        let mut candidates: HashSet<DocumentId> = HashSet::new();
        for &idx in &query.indices {
            if let Some(doc_ids) = inverted_index.get(&idx) {
                candidates.extend(doc_ids.iter().cloned());
            }
        }

        // Score candidates
        let mut scored: Vec<(DocumentId, f32)> = candidates
            .into_iter()
            .filter_map(|id| {
                vectors.get(&id).map(|vec| {
                    let score = query.dot(vec);
                    (id, score)
                })
            })
            .filter(|(_, score)| *score > 0.0)
            .collect();

        // Sort by score descending
        scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        scored.truncate(top_k);
        Ok(scored)
    }

    async fn get(&self, id: &DocumentId) -> Result<Option<SparseVector>, VectorStoreError> {
        let vectors = self.vectors.read().await;
        Ok(vectors.get(id).cloned())
    }

    async fn delete(&mut self, id: &DocumentId) -> Result<bool, VectorStoreError> {
        // Remove from vectors and get the vector for index cleanup
        let vector = {
            let mut vectors = self.vectors.write().await;
            vectors.remove(id)
        };

        if let Some(vec) = vector {
            // Clean up inverted index
            let mut index = self.inverted_index.write().await;
            for &idx in &vec.indices {
                if let Some(doc_ids) = index.get_mut(&idx) {
                    doc_ids.remove(id);
                    if doc_ids.is_empty() {
                        index.remove(&idx);
                    }
                }
            }
            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn count(&self) -> usize {
        self.vectors.read().await.len()
    }

    async fn clear(&mut self) -> Result<(), VectorStoreError> {
        self.vectors.write().await.clear();
        self.inverted_index.write().await.clear();
        Ok(())
    }
}

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

    #[test]
    fn test_sparse_vector_new() {
        let v = SparseVector::new(vec![0, 2, 5], vec![1.0, 2.0, 3.0], 10);
        assert_eq!(v.indices, vec![0, 2, 5]);
        assert_eq!(v.values, vec![1.0, 2.0, 3.0]);
        assert_eq!(v.dimension, 10);
        assert_eq!(v.nnz(), 3);
    }

    #[test]
    fn test_sparse_vector_empty() {
        let v = SparseVector::empty(100);
        assert!(v.is_empty());
        assert_eq!(v.nnz(), 0);
        assert_eq!(v.dimension, 100);
    }

    #[test]
    fn test_sparse_vector_dot() {
        let a = SparseVector::new(vec![0, 2, 4], vec![1.0, 2.0, 3.0], 10);
        let b = SparseVector::new(vec![1, 2, 4], vec![1.0, 2.0, 1.0], 10);
        // Matching indices: 2 (2.0*2.0=4.0) and 4 (3.0*1.0=3.0)
        assert!((a.dot(&b) - 7.0).abs() < 1e-6);
    }

    #[test]
    fn test_sparse_vector_norm() {
        let v = SparseVector::new(vec![0, 1], vec![3.0, 4.0], 10);
        assert!((v.norm() - 5.0).abs() < 1e-6);
    }

    #[test]
    fn test_sparse_vector_cosine_similarity() {
        let a = SparseVector::new(vec![0, 1], vec![1.0, 0.0], 10);
        let b = SparseVector::new(vec![0, 1], vec![1.0, 0.0], 10);
        assert!((a.cosine_similarity(&b) - 1.0).abs() < 1e-6);

        let c = SparseVector::new(vec![0, 1], vec![0.0, 1.0], 10);
        assert!(a.cosine_similarity(&c).abs() < 1e-6);
    }

    #[test]
    fn test_sparse_vector_to_dense() {
        let v = SparseVector::new(vec![0, 2, 4], vec![1.0, 2.0, 3.0], 5);
        let dense = v.to_dense();
        assert_eq!(dense, vec![1.0, 0.0, 2.0, 0.0, 3.0]);
    }

    #[test]
    fn test_sparse_vector_from_dense() {
        let dense = vec![1.0, 0.0, 2.0, 0.0, 3.0];
        let sparse = SparseVector::from_dense(&dense);
        assert_eq!(sparse.indices, vec![0, 2, 4]);
        assert_eq!(sparse.values, vec![1.0, 2.0, 3.0]);
        assert_eq!(sparse.dimension, 5);
    }

    #[test]
    fn test_bm25_encoder_tokenize() {
        let tokens = BM25Encoder::tokenize("Hello, World! This is a test.");
        assert!(tokens.contains(&"hello".to_string()));
        assert!(tokens.contains(&"world".to_string()));
        assert!(tokens.contains(&"this".to_string()));
        assert!(tokens.contains(&"test".to_string()));
        assert!(tokens.contains(&"is".to_string())); // "is" has length 2, so it's included
        // Single character words should be filtered
        assert!(!tokens.contains(&"a".to_string()));
    }

    #[test]
    fn test_bm25_encoder_fit() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["the quick brown fox", "the lazy dog", "quick brown dog"];
        encoder.fit(&docs);

        assert_eq!(encoder.total_documents(), 3);
        assert!(encoder.vocab_size() > 0);
        // "the" appears in 2 docs, should have lower IDF
        // "fox" appears in 1 doc, should have higher IDF
    }

    #[test]
    fn test_bm25_encoder_encode() {
        let mut encoder = BM25Encoder::new();
        let docs = vec![
            "the quick brown fox jumps",
            "the lazy dog sleeps",
            "quick brown dog runs",
        ];
        encoder.fit(&docs);

        let query = "quick brown";
        let sparse = encoder.encode(query);

        assert!(!sparse.is_empty());
        assert!(sparse.nnz() > 0);
    }

    #[test]
    fn test_bm25_encoder_encode_batch() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["document one", "document two"];
        encoder.fit(&docs);

        let queries = vec!["query one", "query two"];
        let vectors = encoder.encode_batch(&queries);

        assert_eq!(vectors.len(), 2);
    }

    #[test]
    fn test_bm25_params_default() {
        let params = BM25Params::default();
        assert!((params.k1 - 1.5).abs() < 1e-6);
        assert!((params.b - 0.75).abs() < 1e-6);
    }

    #[test]
    fn test_hybrid_result_new() {
        let result = HybridResult::new(DocumentId::from("doc1"), 0.8, 0.6, 0.7, 0);
        assert_eq!(result.document_id.as_str(), "doc1");
        assert!((result.dense_score - 0.8).abs() < 1e-6);
        assert!((result.sparse_score - 0.6).abs() < 1e-6);
        assert!((result.combined_score - 0.7).abs() < 1e-6);
        assert_eq!(result.rank, 0);
    }

    #[test]
    fn test_fusion_strategy_default() {
        let strategy = FusionStrategy::default();
        match strategy {
            FusionStrategy::WeightedSum {
                dense_weight,
                sparse_weight,
            } => {
                assert!((dense_weight - 0.5).abs() < 1e-6);
                assert!((sparse_weight - 0.5).abs() < 1e-6);
            }
            _ => panic!("Expected WeightedSum as default"),
        }
    }

    #[test]
    fn test_fusion_strategy_rrf() {
        let strategy = FusionStrategy::rrf(60);
        match strategy {
            FusionStrategy::ReciprocalRankFusion { k } => {
                assert_eq!(k, 60);
            }
            _ => panic!("Expected ReciprocalRankFusion"),
        }
    }

    #[test]
    fn test_hybrid_config_default() {
        let config = HybridConfig::default();
        assert!((config.dense_weight - 0.5).abs() < 1e-6);
        assert!((config.sparse_weight - 0.5).abs() < 1e-6);
        assert!(config.normalize_scores);
        assert!(config.min_score.is_none());
    }

    #[test]
    fn test_hybrid_config_weighted_sum() {
        let config = HybridConfig::weighted_sum(0.7, 0.3);
        assert!((config.dense_weight - 0.7).abs() < 1e-6);
        assert!((config.sparse_weight - 0.3).abs() < 1e-6);
    }

    #[test]
    fn test_hybrid_config_rrf() {
        let config = HybridConfig::rrf(60);
        match config.fusion_strategy {
            FusionStrategy::ReciprocalRankFusion { k } => {
                assert_eq!(k, 60);
            }
            _ => panic!("Expected RRF fusion strategy"),
        }
    }

    #[tokio::test]
    async fn test_in_memory_sparse_store_insert_and_get() {
        let mut store = InMemorySparseStore::new();
        let id = DocumentId::from("doc1");
        let vector = SparseVector::new(vec![0, 2, 4], vec![1.0, 2.0, 3.0], 10);

        store.insert(id.clone(), vector.clone()).await.unwrap();

        let retrieved = store.get(&id).await.unwrap();
        assert!(retrieved.is_some());
        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.indices, vector.indices);
        assert_eq!(retrieved.values, vector.values);
    }

    #[tokio::test]
    async fn test_in_memory_sparse_store_search() {
        let mut store = InMemorySparseStore::new();

        // Insert documents
        store
            .insert(
                DocumentId::from("doc1"),
                SparseVector::new(vec![0, 1], vec![1.0, 0.5], 10),
            )
            .await
            .unwrap();
        store
            .insert(
                DocumentId::from("doc2"),
                SparseVector::new(vec![0, 2], vec![0.5, 1.0], 10),
            )
            .await
            .unwrap();
        store
            .insert(
                DocumentId::from("doc3"),
                SparseVector::new(vec![3, 4], vec![1.0, 1.0], 10),
            )
            .await
            .unwrap();

        // Search with query that matches doc1 and doc2
        let query = SparseVector::new(vec![0, 1], vec![1.0, 1.0], 10);
        let results = store.search(&query, 10).await.unwrap();

        // doc1 should have highest score: 1.0*1.0 + 0.5*1.0 = 1.5
        // doc2 should have lower score: 0.5*1.0 = 0.5
        // doc3 should not match (no overlapping indices)
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0.as_str(), "doc1");
        assert!((results[0].1 - 1.5).abs() < 1e-6);
    }

    #[tokio::test]
    async fn test_in_memory_sparse_store_delete() {
        let mut store = InMemorySparseStore::new();
        let id = DocumentId::from("doc1");
        let vector = SparseVector::new(vec![0, 2], vec![1.0, 2.0], 10);

        store.insert(id.clone(), vector).await.unwrap();
        assert_eq!(store.count().await, 1);

        let deleted = store.delete(&id).await.unwrap();
        assert!(deleted);
        assert_eq!(store.count().await, 0);

        let deleted = store.delete(&id).await.unwrap();
        assert!(!deleted);
    }

    #[tokio::test]
    async fn test_in_memory_sparse_store_clear() {
        let mut store = InMemorySparseStore::new();

        store
            .insert(
                DocumentId::from("doc1"),
                SparseVector::new(vec![0], vec![1.0], 10),
            )
            .await
            .unwrap();
        store
            .insert(
                DocumentId::from("doc2"),
                SparseVector::new(vec![1], vec![1.0], 10),
            )
            .await
            .unwrap();

        assert_eq!(store.count().await, 2);

        store.clear().await.unwrap();
        assert_eq!(store.count().await, 0);
    }

    #[tokio::test]
    async fn test_hybrid_searcher_weighted_sum() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["quick brown fox", "lazy dog", "brown dog"];
        encoder.fit(&docs);

        let mut store = InMemorySparseStore::new();
        for (i, doc) in docs.iter().enumerate() {
            let id = DocumentId::from(format!("doc{}", i + 1));
            let vector = encoder.encode(doc);
            store.insert(id, vector).await.unwrap();
        }

        let config = HybridConfig::weighted_sum(0.5, 0.5);
        let searcher = HybridSearcher::new(store, encoder, config);

        // Dense results (simulated)
        let dense_results = vec![
            (DocumentId::from("doc1"), 0.9),
            (DocumentId::from("doc2"), 0.5),
            (DocumentId::from("doc3"), 0.7),
        ];

        let results = searcher
            .search("brown fox", &dense_results, 3)
            .await
            .unwrap();

        assert!(!results.is_empty());
        // Results should be sorted by combined score
        for i in 1..results.len() {
            assert!(results[i - 1].combined_score >= results[i].combined_score);
        }
    }

    #[tokio::test]
    async fn test_hybrid_searcher_rrf() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["quick brown fox", "lazy dog", "brown dog"];
        encoder.fit(&docs);

        let mut store = InMemorySparseStore::new();
        for (i, doc) in docs.iter().enumerate() {
            let id = DocumentId::from(format!("doc{}", i + 1));
            let vector = encoder.encode(doc);
            store.insert(id, vector).await.unwrap();
        }

        let config = HybridConfig::rrf(60);
        let searcher = HybridSearcher::new(store, encoder, config);

        let dense_results = vec![
            (DocumentId::from("doc1"), 0.9),
            (DocumentId::from("doc3"), 0.7),
            (DocumentId::from("doc2"), 0.5),
        ];

        let results = searcher.search("brown", &dense_results, 3).await.unwrap();

        assert!(!results.is_empty());
        // Results should be sorted by RRF score
        for i in 1..results.len() {
            assert!(results[i - 1].combined_score >= results[i].combined_score);
        }
    }

    #[tokio::test]
    async fn test_hybrid_searcher_distribution_based() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["quick brown fox", "lazy dog", "brown dog"];
        encoder.fit(&docs);

        let mut store = InMemorySparseStore::new();
        for (i, doc) in docs.iter().enumerate() {
            let id = DocumentId::from(format!("doc{}", i + 1));
            let vector = encoder.encode(doc);
            store.insert(id, vector).await.unwrap();
        }

        let config = HybridConfig {
            fusion_strategy: FusionStrategy::distribution_based(0.5, 0.5),
            dense_weight: 0.5,
            sparse_weight: 0.5,
            normalize_scores: true,
            min_score: None,
        };
        let searcher = HybridSearcher::new(store, encoder, config);

        let dense_results = vec![
            (DocumentId::from("doc1"), 0.9),
            (DocumentId::from("doc2"), 0.5),
            (DocumentId::from("doc3"), 0.7),
        ];

        let results = searcher
            .search("brown fox", &dense_results, 3)
            .await
            .unwrap();

        assert!(!results.is_empty());
    }

    #[tokio::test]
    async fn test_hybrid_searcher_with_min_score() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["quick brown fox", "lazy dog"];
        encoder.fit(&docs);

        let mut store = InMemorySparseStore::new();
        for (i, doc) in docs.iter().enumerate() {
            let id = DocumentId::from(format!("doc{}", i + 1));
            let vector = encoder.encode(doc);
            store.insert(id, vector).await.unwrap();
        }

        let config = HybridConfig::weighted_sum(0.5, 0.5).with_min_score(0.8);
        let searcher = HybridSearcher::new(store, encoder, config);

        let dense_results = vec![
            (DocumentId::from("doc1"), 0.9),
            (DocumentId::from("doc2"), 0.3),
        ];

        let results = searcher
            .search("quick fox", &dense_results, 10)
            .await
            .unwrap();

        // All results should have combined_score >= 0.8
        for result in &results {
            assert!(result.combined_score >= 0.8);
        }
    }

    #[test]
    fn test_normalize_score() {
        // Test normalization
        let score = HybridSearcher::<InMemorySparseStore>::normalize_score(0.5, 0.0, 1.0);
        assert!((score - 0.5).abs() < 1e-6);

        let score = HybridSearcher::<InMemorySparseStore>::normalize_score(5.0, 0.0, 10.0);
        assert!((score - 0.5).abs() < 1e-6);

        // Test edge case: min == max
        let score = HybridSearcher::<InMemorySparseStore>::normalize_score(5.0, 5.0, 5.0);
        assert!((score - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_score_stats() {
        let results = vec![
            (DocumentId::from("a"), 1.0),
            (DocumentId::from("b"), 2.0),
            (DocumentId::from("c"), 3.0),
        ];

        let (mean, std) = HybridSearcher::<InMemorySparseStore>::score_stats(&results);
        assert!((mean - 2.0).abs() < 1e-6);
        // std = sqrt(((1-2)^2 + (2-2)^2 + (3-2)^2) / 3) = sqrt(2/3)
        let expected_std = (2.0_f32 / 3.0).sqrt();
        assert!((std - expected_std).abs() < 1e-6);
    }

    #[test]
    fn test_z_normalize() {
        let normalized = HybridSearcher::<InMemorySparseStore>::z_normalize(5.0, 3.0, 2.0);
        // (5 - 3) / 2 = 1.0
        assert!((normalized - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_bm25_encoder_with_custom_params() {
        let params = BM25Params {
            k1: 2.0,
            b: 0.5,
            delta: 1.0,
        };
        let encoder = BM25Encoder::with_params(params);
        assert!((encoder.params().k1 - 2.0).abs() < 1e-6);
        assert!((encoder.params().b - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_sparse_vector_default() {
        let v = SparseVector::default();
        assert!(v.is_empty());
        assert_eq!(v.dimension, 0);
    }

    #[tokio::test]
    async fn test_hybrid_searcher_empty_results() {
        let encoder = BM25Encoder::new();
        let store = InMemorySparseStore::new();
        let config = HybridConfig::default();
        let searcher = HybridSearcher::new(store, encoder, config);

        let results = searcher.search("query", &[], 10).await.unwrap();
        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn test_hybrid_searcher_search_with_sparse() {
        let mut encoder = BM25Encoder::new();
        let docs = vec!["hello world", "world peace"];
        encoder.fit(&docs);

        let mut store = InMemorySparseStore::new();
        for (i, doc) in docs.iter().enumerate() {
            let id = DocumentId::from(format!("doc{}", i + 1));
            let vector = encoder.encode(doc);
            store.insert(id, vector).await.unwrap();
        }

        let config = HybridConfig::default();
        let searcher = HybridSearcher::new(store, encoder, config);

        let sparse_query = searcher.encoder().encode("world");
        let dense_results = vec![
            (DocumentId::from("doc1"), 0.8),
            (DocumentId::from("doc2"), 0.6),
        ];

        let results = searcher
            .search_with_sparse(&sparse_query, &dense_results, 10)
            .await
            .unwrap();
        assert!(!results.is_empty());
    }
}