ipfrs-semantic 0.2.0

Semantic search with HNSW vector indexing for content-addressed data
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
//! Multi-source search result aggregation and ranking.
//!
//! This module provides facilities for combining search results from multiple
//! sources (e.g., HNSW, DiskANN, federated peers) into a single ranked list
//! using various aggregation strategies including Reciprocal Rank Fusion,
//! score summation, weighted combination, and more.

use std::collections::HashMap;

/// Strategy for aggregating search results from multiple sources.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AggregationStrategy {
    /// Sum scores across all sources for each document.
    ScoreSum,
    /// Take the maximum score across all sources for each document.
    ScoreMax,
    /// Average scores across all sources for each document.
    ScoreAverage,
    /// Reciprocal Rank Fusion: score = sum(1/(k+rank)).
    RankFusion,
    /// Weighted combination using per-source weights.
    WeightedCombination,
}

/// A single search result from one source.
#[derive(Debug, Clone)]
pub struct SearchResult {
    /// Unique document identifier.
    pub doc_id: String,
    /// Relevance score (higher is better).
    pub score: f64,
    /// Name of the source that produced this result.
    pub source: String,
    /// Arbitrary metadata associated with the result.
    pub metadata: HashMap<String, String>,
}

/// An aggregated result combining information from multiple sources.
#[derive(Debug, Clone)]
pub struct AggregatedResult {
    /// Unique document identifier.
    pub doc_id: String,
    /// Final aggregated score.
    pub final_score: f64,
    /// List of sources that contributed to this result.
    pub sources: Vec<String>,
    /// Per-source scores as (source_name, score) pairs.
    pub source_scores: Vec<(String, f64)>,
    /// Rank in the final result list (1-based).
    pub rank: usize,
}

/// Configuration for the result aggregator.
#[derive(Debug, Clone)]
pub struct AggregatorConfig {
    /// Strategy used to combine scores.
    pub strategy: AggregationStrategy,
    /// Maximum number of results to return.
    pub max_results: usize,
    /// Minimum score threshold; results below this are filtered out.
    pub min_score_threshold: f64,
    /// Per-source weights for `WeightedCombination` strategy.
    pub source_weights: HashMap<String, f64>,
    /// RRF constant k (default 60.0). Higher values reduce the impact of rank differences.
    pub rrf_k: f64,
}

impl Default for AggregatorConfig {
    fn default() -> Self {
        Self {
            strategy: AggregationStrategy::RankFusion,
            max_results: 100,
            min_score_threshold: 0.0,
            source_weights: HashMap::new(),
            rrf_k: 60.0,
        }
    }
}

/// Tracks statistics about aggregation operations.
#[derive(Debug, Clone, Default)]
pub struct AggregatorStats {
    /// Total number of aggregation operations performed.
    pub aggregations_performed: u64,
    /// Total number of input results across all aggregations.
    pub total_input_results: u64,
    /// Total number of output results across all aggregations.
    pub total_output_results: u64,
    /// Running average compression ratio (input/output).
    pub avg_compression_ratio: f64,
}

/// Multi-source search result aggregator.
///
/// Collects search results from multiple sources and combines them using
/// a configurable aggregation strategy.
pub struct ResultAggregator {
    config: AggregatorConfig,
    result_sets: HashMap<String, Vec<SearchResult>>,
    stats: AggregatorStats,
}

impl ResultAggregator {
    /// Create a new `ResultAggregator` with the given configuration.
    pub fn new(config: AggregatorConfig) -> Self {
        Self {
            config,
            result_sets: HashMap::new(),
            stats: AggregatorStats::default(),
        }
    }

    /// Add a batch of results from a named source.
    pub fn add_results(&mut self, source: &str, results: Vec<SearchResult>) {
        self.result_sets
            .entry(source.to_string())
            .or_default()
            .extend(results);
    }

    /// Aggregate all added result sets using the configured strategy.
    ///
    /// Returns a sorted, deduplicated, ranked list of aggregated results.
    pub fn aggregate(&mut self) -> Vec<AggregatedResult> {
        let input_count: u64 = self.result_sets.values().map(|v| v.len() as u64).sum();

        let mut results = match self.config.strategy {
            AggregationStrategy::ScoreSum => Self::aggregate_score_sum(&self.result_sets),
            AggregationStrategy::ScoreMax => Self::aggregate_score_max(&self.result_sets),
            AggregationStrategy::ScoreAverage => Self::aggregate_score_avg(&self.result_sets),
            AggregationStrategy::RankFusion => {
                Self::aggregate_rrf(&self.result_sets, self.config.rrf_k)
            }
            AggregationStrategy::WeightedCombination => {
                Self::aggregate_weighted(&self.result_sets, &self.config.source_weights)
            }
        };

        self.apply_threshold(&mut results);

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

        if results.len() > self.config.max_results {
            results.truncate(self.config.max_results);
        }

        // Assign ranks (1-based)
        for (i, r) in results.iter_mut().enumerate() {
            r.rank = i + 1;
        }

        // Update stats
        let output_count = results.len() as u64;
        self.stats.aggregations_performed += 1;
        self.stats.total_input_results += input_count;
        self.stats.total_output_results += output_count;

        let n = self.stats.aggregations_performed as f64;
        let ratio = if output_count > 0 {
            input_count as f64 / output_count as f64
        } else if input_count > 0 {
            input_count as f64
        } else {
            1.0
        };
        // Running average
        self.stats.avg_compression_ratio =
            self.stats.avg_compression_ratio * ((n - 1.0) / n) + ratio / n;

        results
    }

    /// Reciprocal Rank Fusion aggregation.
    ///
    /// For each source, results are ranked by descending score. The RRF score
    /// for a document is `sum(1 / (k + rank))` across all sources.
    pub fn aggregate_rrf(
        result_sets: &HashMap<String, Vec<SearchResult>>,
        k: f64,
    ) -> Vec<AggregatedResult> {
        // Build per-source rankings (sorted descending by score)
        let mut doc_rrf_scores: HashMap<String, f64> = HashMap::new();
        let mut doc_sources: HashMap<String, Vec<String>> = HashMap::new();
        let mut doc_source_scores: HashMap<String, Vec<(String, f64)>> = HashMap::new();

        for (source, results) in result_sets {
            // Sort by descending score to determine rank
            let mut sorted: Vec<&SearchResult> = results.iter().collect();
            sorted.sort_by(|a, b| {
                b.score
                    .partial_cmp(&a.score)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

            // Deduplicate within source (keep highest score)
            let mut seen_in_source: HashMap<&str, bool> = HashMap::new();
            let mut rank: usize = 0;

            for res in &sorted {
                if seen_in_source.contains_key(res.doc_id.as_str()) {
                    continue;
                }
                seen_in_source.insert(&res.doc_id, true);
                rank += 1;

                let rrf_score = 1.0 / (k + rank as f64);
                *doc_rrf_scores.entry(res.doc_id.clone()).or_default() += rrf_score;

                doc_sources
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push(source.clone());

                doc_source_scores
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push((source.clone(), res.score));
            }
        }

        Self::build_aggregated_results(doc_rrf_scores, doc_sources, doc_source_scores)
    }

    /// Sum scores across all sources for each document.
    pub fn aggregate_score_sum(
        result_sets: &HashMap<String, Vec<SearchResult>>,
    ) -> Vec<AggregatedResult> {
        let mut doc_scores: HashMap<String, f64> = HashMap::new();
        let mut doc_sources: HashMap<String, Vec<String>> = HashMap::new();
        let mut doc_source_scores: HashMap<String, Vec<(String, f64)>> = HashMap::new();

        for (source, results) in result_sets {
            let mut seen: HashMap<&str, bool> = HashMap::new();
            for res in results {
                if seen.contains_key(res.doc_id.as_str()) {
                    continue;
                }
                seen.insert(&res.doc_id, true);

                *doc_scores.entry(res.doc_id.clone()).or_default() += res.score;

                doc_sources
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push(source.clone());

                doc_source_scores
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push((source.clone(), res.score));
            }
        }

        Self::build_aggregated_results(doc_scores, doc_sources, doc_source_scores)
    }

    /// Take the maximum score across all sources for each document.
    pub fn aggregate_score_max(
        result_sets: &HashMap<String, Vec<SearchResult>>,
    ) -> Vec<AggregatedResult> {
        let mut doc_scores: HashMap<String, f64> = HashMap::new();
        let mut doc_sources: HashMap<String, Vec<String>> = HashMap::new();
        let mut doc_source_scores: HashMap<String, Vec<(String, f64)>> = HashMap::new();

        for (source, results) in result_sets {
            let mut seen: HashMap<&str, bool> = HashMap::new();
            for res in results {
                if seen.contains_key(res.doc_id.as_str()) {
                    continue;
                }
                seen.insert(&res.doc_id, true);

                let entry = doc_scores
                    .entry(res.doc_id.clone())
                    .or_insert(f64::NEG_INFINITY);
                if res.score > *entry {
                    *entry = res.score;
                }

                doc_sources
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push(source.clone());

                doc_source_scores
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push((source.clone(), res.score));
            }
        }

        Self::build_aggregated_results(doc_scores, doc_sources, doc_source_scores)
    }

    /// Average scores across all sources for each document.
    pub fn aggregate_score_avg(
        result_sets: &HashMap<String, Vec<SearchResult>>,
    ) -> Vec<AggregatedResult> {
        let mut doc_score_sums: HashMap<String, f64> = HashMap::new();
        let mut doc_score_counts: HashMap<String, usize> = HashMap::new();
        let mut doc_sources: HashMap<String, Vec<String>> = HashMap::new();
        let mut doc_source_scores: HashMap<String, Vec<(String, f64)>> = HashMap::new();

        for (source, results) in result_sets {
            let mut seen: HashMap<&str, bool> = HashMap::new();
            for res in results {
                if seen.contains_key(res.doc_id.as_str()) {
                    continue;
                }
                seen.insert(&res.doc_id, true);

                *doc_score_sums.entry(res.doc_id.clone()).or_default() += res.score;
                *doc_score_counts.entry(res.doc_id.clone()).or_default() += 1;

                doc_sources
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push(source.clone());

                doc_source_scores
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push((source.clone(), res.score));
            }
        }

        let doc_scores: HashMap<String, f64> = doc_score_sums
            .into_iter()
            .map(|(doc_id, sum)| {
                let count = doc_score_counts.get(&doc_id).copied().unwrap_or(1);
                (doc_id, sum / count as f64)
            })
            .collect();

        Self::build_aggregated_results(doc_scores, doc_sources, doc_source_scores)
    }

    /// Weighted combination of scores using per-source weights.
    ///
    /// If a source has no configured weight, it defaults to 1.0.
    pub fn aggregate_weighted(
        result_sets: &HashMap<String, Vec<SearchResult>>,
        weights: &HashMap<String, f64>,
    ) -> Vec<AggregatedResult> {
        let mut doc_scores: HashMap<String, f64> = HashMap::new();
        let mut doc_sources: HashMap<String, Vec<String>> = HashMap::new();
        let mut doc_source_scores: HashMap<String, Vec<(String, f64)>> = HashMap::new();

        for (source, results) in result_sets {
            let weight = weights.get(source).copied().unwrap_or(1.0);
            let mut seen: HashMap<&str, bool> = HashMap::new();

            for res in results {
                if seen.contains_key(res.doc_id.as_str()) {
                    continue;
                }
                seen.insert(&res.doc_id, true);

                *doc_scores.entry(res.doc_id.clone()).or_default() += res.score * weight;

                doc_sources
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push(source.clone());

                doc_source_scores
                    .entry(res.doc_id.clone())
                    .or_default()
                    .push((source.clone(), res.score));
            }
        }

        Self::build_aggregated_results(doc_scores, doc_sources, doc_source_scores)
    }

    /// Remove all result sets.
    pub fn clear(&mut self) {
        self.result_sets.clear();
    }

    /// Return the number of distinct sources currently held.
    pub fn source_count(&self) -> usize {
        self.result_sets.len()
    }

    /// Return the total number of individual results across all sources.
    pub fn total_results(&self) -> usize {
        self.result_sets.values().map(|v| v.len()).sum()
    }

    /// Return a reference to the current aggregation statistics.
    pub fn stats(&self) -> &AggregatorStats {
        &self.stats
    }

    /// Filter out results whose `final_score` is below the configured threshold.
    pub fn apply_threshold(&self, results: &mut Vec<AggregatedResult>) {
        results.retain(|r| r.final_score >= self.config.min_score_threshold);
    }

    // ---- internal helpers ----

    /// Build the final `AggregatedResult` list from the intermediate maps.
    fn build_aggregated_results(
        doc_scores: HashMap<String, f64>,
        doc_sources: HashMap<String, Vec<String>>,
        doc_source_scores: HashMap<String, Vec<(String, f64)>>,
    ) -> Vec<AggregatedResult> {
        let mut results: Vec<AggregatedResult> = doc_scores
            .into_iter()
            .map(|(doc_id, final_score)| {
                let sources = doc_sources.get(&doc_id).cloned().unwrap_or_default();
                let source_scores = doc_source_scores.get(&doc_id).cloned().unwrap_or_default();
                AggregatedResult {
                    doc_id,
                    final_score,
                    sources,
                    source_scores,
                    rank: 0, // assigned later
                }
            })
            .collect();

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

        results
    }
}

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

    fn make_result(doc_id: &str, score: f64, source: &str) -> SearchResult {
        SearchResult {
            doc_id: doc_id.to_string(),
            score,
            source: source.to_string(),
            metadata: HashMap::new(),
        }
    }

    fn make_result_with_meta(
        doc_id: &str,
        score: f64,
        source: &str,
        meta: Vec<(&str, &str)>,
    ) -> SearchResult {
        let metadata: HashMap<String, String> = meta
            .into_iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        SearchResult {
            doc_id: doc_id.to_string(),
            score,
            source: source.to_string(),
            metadata,
        }
    }

    // ---- ScoreSum strategy ----

    #[test]
    fn test_score_sum_basic() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results(
            "a",
            vec![make_result("d1", 0.5, "a"), make_result("d2", 0.3, "a")],
        );
        agg.add_results(
            "b",
            vec![make_result("d1", 0.4, "b"), make_result("d3", 0.6, "b")],
        );

        let results = agg.aggregate();
        let d1 = results
            .iter()
            .find(|r| r.doc_id == "d1")
            .expect("d1 present");
        assert!(
            (d1.final_score - 0.9).abs() < 1e-9,
            "d1 sum = 0.5+0.4 = 0.9"
        );
    }

    #[test]
    fn test_score_sum_single_source() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.7, "a")]);
        let results = agg.aggregate();
        assert_eq!(results.len(), 1);
        assert!((results[0].final_score - 0.7).abs() < 1e-9);
    }

    // ---- ScoreMax strategy ----

    #[test]
    fn test_score_max_basic() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreMax,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.9, "b")]);

        let results = agg.aggregate();
        let d1 = results
            .iter()
            .find(|r| r.doc_id == "d1")
            .expect("d1 present");
        assert!((d1.final_score - 0.9).abs() < 1e-9);
    }

    #[test]
    fn test_score_max_picks_highest() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreMax,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.1, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.3, "b")]);
        agg.add_results("c", vec![make_result("d1", 0.2, "c")]);

        let results = agg.aggregate();
        assert!((results[0].final_score - 0.3).abs() < 1e-9);
    }

    // ---- ScoreAverage strategy ----

    #[test]
    fn test_score_avg_basic() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreAverage,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.6, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.4, "b")]);

        let results = agg.aggregate();
        let d1 = results
            .iter()
            .find(|r| r.doc_id == "d1")
            .expect("d1 present");
        assert!((d1.final_score - 0.5).abs() < 1e-9, "avg(0.6, 0.4) = 0.5");
    }

    #[test]
    fn test_score_avg_three_sources() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreAverage,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.3, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.6, "b")]);
        agg.add_results("c", vec![make_result("d1", 0.9, "c")]);

        let results = agg.aggregate();
        assert!((results[0].final_score - 0.6).abs() < 1e-9);
    }

    // ---- RRF strategy ----

    #[test]
    fn test_rrf_formula() {
        // With k=60, rank 1 gives 1/(60+1) = 1/61
        let mut sets: HashMap<String, Vec<SearchResult>> = HashMap::new();
        sets.insert("a".to_string(), vec![make_result("d1", 1.0, "a")]);

        let results = ResultAggregator::aggregate_rrf(&sets, 60.0);
        let expected = 1.0 / 61.0;
        assert!(
            (results[0].final_score - expected).abs() < 1e-9,
            "RRF score for rank-1 doc with k=60 should be 1/61"
        );
    }

    #[test]
    fn test_rrf_multi_source() {
        let mut sets: HashMap<String, Vec<SearchResult>> = HashMap::new();
        // d1 is rank 1 in both sources
        sets.insert(
            "a".to_string(),
            vec![make_result("d1", 1.0, "a"), make_result("d2", 0.5, "a")],
        );
        sets.insert(
            "b".to_string(),
            vec![make_result("d1", 0.9, "b"), make_result("d3", 0.8, "b")],
        );

        let results = ResultAggregator::aggregate_rrf(&sets, 60.0);
        let d1 = results
            .iter()
            .find(|r| r.doc_id == "d1")
            .expect("d1 present");
        let expected = 2.0 / 61.0; // rank 1 in both sources
        assert!((d1.final_score - expected).abs() < 1e-9);
    }

    #[test]
    fn test_rrf_respects_k_parameter() {
        let mut sets: HashMap<String, Vec<SearchResult>> = HashMap::new();
        sets.insert("a".to_string(), vec![make_result("d1", 1.0, "a")]);

        let results_low_k = ResultAggregator::aggregate_rrf(&sets, 10.0);
        let results_high_k = ResultAggregator::aggregate_rrf(&sets, 100.0);

        // Lower k means higher score for the same rank
        assert!(results_low_k[0].final_score > results_high_k[0].final_score);
    }

    #[test]
    fn test_rrf_rank_ordering() {
        // d1 has score 1.0 (rank 1), d2 has score 0.5 (rank 2)
        let mut sets: HashMap<String, Vec<SearchResult>> = HashMap::new();
        sets.insert(
            "a".to_string(),
            vec![make_result("d1", 1.0, "a"), make_result("d2", 0.5, "a")],
        );

        let results = ResultAggregator::aggregate_rrf(&sets, 60.0);
        assert!(results[0].final_score > results[1].final_score);
        // rank 1: 1/61, rank 2: 1/62
        let expected_d1 = 1.0 / 61.0;
        let expected_d2 = 1.0 / 62.0;
        assert!((results[0].final_score - expected_d1).abs() < 1e-9);
        assert!((results[1].final_score - expected_d2).abs() < 1e-9);
    }

    // ---- WeightedCombination strategy ----

    #[test]
    fn test_weighted_basic() {
        let mut weights = HashMap::new();
        weights.insert("a".to_string(), 2.0);
        weights.insert("b".to_string(), 1.0);

        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::WeightedCombination,
            source_weights: weights,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.5, "b")]);

        let results = agg.aggregate();
        let d1 = results
            .iter()
            .find(|r| r.doc_id == "d1")
            .expect("d1 present");
        // 0.5*2.0 + 0.5*1.0 = 1.5
        assert!((d1.final_score - 1.5).abs() < 1e-9);
    }

    #[test]
    fn test_weighted_default_weight() {
        // Source not in weights map should default to 1.0
        let weights = HashMap::new();

        let mut sets: HashMap<String, Vec<SearchResult>> = HashMap::new();
        sets.insert(
            "unknown".to_string(),
            vec![make_result("d1", 0.7, "unknown")],
        );

        let results = ResultAggregator::aggregate_weighted(&sets, &weights);
        assert!((results[0].final_score - 0.7).abs() < 1e-9);
    }

    #[test]
    fn test_weighted_zero_weight() {
        let mut weights = HashMap::new();
        weights.insert("a".to_string(), 0.0);

        let mut sets: HashMap<String, Vec<SearchResult>> = HashMap::new();
        sets.insert("a".to_string(), vec![make_result("d1", 0.9, "a")]);

        let results = ResultAggregator::aggregate_weighted(&sets, &weights);
        assert!(
            (results[0].final_score).abs() < 1e-9,
            "zero weight => zero score"
        );
    }

    // ---- Threshold filtering ----

    #[test]
    fn test_threshold_filters_low_scores() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            min_score_threshold: 0.5,
            ..Default::default()
        });
        agg.add_results(
            "a",
            vec![
                make_result("d1", 0.8, "a"),
                make_result("d2", 0.3, "a"),
                make_result("d3", 0.5, "a"),
            ],
        );

        let results = agg.aggregate();
        assert_eq!(results.len(), 2); // d2 filtered out
        assert!(results.iter().all(|r| r.final_score >= 0.5));
    }

    #[test]
    fn test_threshold_zero_passes_all() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            min_score_threshold: 0.0,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.001, "a")]);
        let results = agg.aggregate();
        assert_eq!(results.len(), 1);
    }

    // ---- max_results limit ----

    #[test]
    fn test_max_results_truncation() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            max_results: 2,
            ..Default::default()
        });
        agg.add_results(
            "a",
            vec![
                make_result("d1", 0.9, "a"),
                make_result("d2", 0.8, "a"),
                make_result("d3", 0.7, "a"),
            ],
        );

        let results = agg.aggregate();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].doc_id, "d1");
        assert_eq!(results[1].doc_id, "d2");
    }

    // ---- Deduplication by doc_id ----

    #[test]
    fn test_dedup_within_source() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        // Same doc_id twice in one source (first occurrence wins for that source)
        agg.add_results(
            "a",
            vec![make_result("d1", 0.5, "a"), make_result("d1", 0.3, "a")],
        );
        let results = agg.aggregate();
        assert_eq!(results.len(), 1);
        // Only the first d1 from source "a" is counted
        assert!((results[0].final_score - 0.5).abs() < 1e-9);
    }

    #[test]
    fn test_dedup_across_sources_merges() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.4, "b")]);

        let results = agg.aggregate();
        assert_eq!(results.len(), 1); // merged into one
        assert!(results[0].sources.len() >= 2);
    }

    // ---- Empty sources ----

    #[test]
    fn test_empty_no_sources() {
        let mut agg = ResultAggregator::new(AggregatorConfig::default());
        let results = agg.aggregate();
        assert!(results.is_empty());
    }

    #[test]
    fn test_empty_source_list() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results("a", vec![]);
        let results = agg.aggregate();
        assert!(results.is_empty());
    }

    // ---- Single source passthrough ----

    #[test]
    fn test_single_source_passthrough() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results(
            "only",
            vec![
                make_result("d1", 0.9, "only"),
                make_result("d2", 0.7, "only"),
            ],
        );
        let results = agg.aggregate();
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].doc_id, "d1");
        assert_eq!(results[1].doc_id, "d2");
        assert_eq!(results[0].sources, vec!["only"]);
    }

    // ---- Stats tracking ----

    #[test]
    fn test_stats_initial() {
        let agg = ResultAggregator::new(AggregatorConfig::default());
        assert_eq!(agg.stats().aggregations_performed, 0);
        assert_eq!(agg.stats().total_input_results, 0);
        assert_eq!(agg.stats().total_output_results, 0);
    }

    #[test]
    fn test_stats_after_aggregate() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results(
            "a",
            vec![make_result("d1", 0.5, "a"), make_result("d2", 0.3, "a")],
        );
        agg.add_results("b", vec![make_result("d1", 0.4, "b")]);

        let _results = agg.aggregate();
        assert_eq!(agg.stats().aggregations_performed, 1);
        assert_eq!(agg.stats().total_input_results, 3);
        assert_eq!(agg.stats().total_output_results, 2);
    }

    #[test]
    fn test_stats_multiple_aggregations() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });

        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        let _r1 = agg.aggregate();

        agg.clear();
        agg.add_results(
            "b",
            vec![make_result("d2", 0.8, "b"), make_result("d3", 0.3, "b")],
        );
        let _r2 = agg.aggregate();

        assert_eq!(agg.stats().aggregations_performed, 2);
        assert_eq!(agg.stats().total_input_results, 3); // 1 + 2
        assert_eq!(agg.stats().total_output_results, 3); // 1 + 2
    }

    // ---- Source weights ----

    #[test]
    fn test_source_weights_high_boost() {
        let mut weights = HashMap::new();
        weights.insert("premium".to_string(), 10.0);
        weights.insert("basic".to_string(), 1.0);

        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::WeightedCombination,
            source_weights: weights,
            ..Default::default()
        });
        agg.add_results("premium", vec![make_result("d1", 0.3, "premium")]);
        agg.add_results("basic", vec![make_result("d2", 0.9, "basic")]);

        let results = agg.aggregate();
        // d1: 0.3*10 = 3.0, d2: 0.9*1 = 0.9
        assert_eq!(results[0].doc_id, "d1");
        assert!((results[0].final_score - 3.0).abs() < 1e-9);
    }

    // ---- Clear and re-aggregate ----

    #[test]
    fn test_clear_resets_results() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        assert_eq!(agg.source_count(), 1);
        assert_eq!(agg.total_results(), 1);

        agg.clear();
        assert_eq!(agg.source_count(), 0);
        assert_eq!(agg.total_results(), 0);

        let results = agg.aggregate();
        assert!(results.is_empty());
    }

    #[test]
    fn test_clear_and_reaggregate() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });

        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        let r1 = agg.aggregate();
        assert_eq!(r1.len(), 1);

        agg.clear();
        agg.add_results("b", vec![make_result("d2", 0.8, "b")]);
        let r2 = agg.aggregate();
        assert_eq!(r2.len(), 1);
        assert_eq!(r2[0].doc_id, "d2");
    }

    // ---- Rank assignment ----

    #[test]
    fn test_rank_assignment() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results(
            "a",
            vec![
                make_result("d1", 0.9, "a"),
                make_result("d2", 0.7, "a"),
                make_result("d3", 0.5, "a"),
            ],
        );

        let results = agg.aggregate();
        assert_eq!(results[0].rank, 1);
        assert_eq!(results[1].rank, 2);
        assert_eq!(results[2].rank, 3);
    }

    // ---- Multi-source merge ----

    #[test]
    fn test_multi_source_merge_three() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.3, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.3, "b")]);
        agg.add_results("c", vec![make_result("d1", 0.3, "c")]);

        let results = agg.aggregate();
        assert_eq!(results.len(), 1);
        assert!((results[0].final_score - 0.9).abs() < 1e-9);
        assert_eq!(results[0].sources.len(), 3);
    }

    // ---- source_count and total_results ----

    #[test]
    fn test_source_count() {
        let mut agg = ResultAggregator::new(AggregatorConfig::default());
        assert_eq!(agg.source_count(), 0);
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        assert_eq!(agg.source_count(), 1);
        agg.add_results("b", vec![make_result("d2", 0.3, "b")]);
        assert_eq!(agg.source_count(), 2);
    }

    #[test]
    fn test_total_results() {
        let mut agg = ResultAggregator::new(AggregatorConfig::default());
        assert_eq!(agg.total_results(), 0);
        agg.add_results(
            "a",
            vec![make_result("d1", 0.5, "a"), make_result("d2", 0.3, "a")],
        );
        assert_eq!(agg.total_results(), 2);
        agg.add_results("b", vec![make_result("d3", 0.4, "b")]);
        assert_eq!(agg.total_results(), 3);
    }

    // ---- Metadata preservation ----

    #[test]
    fn test_metadata_preserved() {
        let r = make_result_with_meta("d1", 0.5, "a", vec![("key", "value")]);
        assert_eq!(r.metadata.get("key").map(|s| s.as_str()), Some("value"));
    }

    // ---- source_scores tracking ----

    #[test]
    fn test_source_scores_tracked() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        agg.add_results("b", vec![make_result("d1", 0.3, "b")]);

        let results = agg.aggregate();
        let d1 = results
            .iter()
            .find(|r| r.doc_id == "d1")
            .expect("d1 present");
        assert_eq!(d1.source_scores.len(), 2);
    }

    // ---- Default config ----

    #[test]
    fn test_default_config() {
        let config = AggregatorConfig::default();
        assert_eq!(config.strategy, AggregationStrategy::RankFusion);
        assert_eq!(config.max_results, 100);
        assert!((config.min_score_threshold).abs() < 1e-9);
        assert!((config.rrf_k - 60.0).abs() < 1e-9);
    }

    // ---- Aggregation strategy via aggregate() dispatch ----

    #[test]
    fn test_aggregate_dispatches_rrf() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::RankFusion,
            rrf_k: 60.0,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 1.0, "a")]);
        let results = agg.aggregate();
        let expected = 1.0 / 61.0;
        assert!((results[0].final_score - expected).abs() < 1e-9);
    }

    #[test]
    fn test_aggregate_dispatches_weighted() {
        let mut weights = HashMap::new();
        weights.insert("a".to_string(), 3.0);

        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::WeightedCombination,
            source_weights: weights,
            ..Default::default()
        });
        agg.add_results("a", vec![make_result("d1", 0.5, "a")]);
        let results = agg.aggregate();
        assert!((results[0].final_score - 1.5).abs() < 1e-9);
    }

    // ---- Compression ratio ----

    #[test]
    fn test_compression_ratio() {
        let mut agg = ResultAggregator::new(AggregatorConfig {
            strategy: AggregationStrategy::ScoreSum,
            ..Default::default()
        });
        // 4 input results, 2 unique doc_ids => output 2 => ratio 4/2 = 2.0
        agg.add_results(
            "a",
            vec![make_result("d1", 0.5, "a"), make_result("d2", 0.3, "a")],
        );
        agg.add_results(
            "b",
            vec![make_result("d1", 0.4, "b"), make_result("d2", 0.2, "b")],
        );
        let _results = agg.aggregate();
        assert!((agg.stats().avg_compression_ratio - 2.0).abs() < 1e-9);
    }
}