Skip to main content

mempal_search_core/
lib.rs

1#![warn(clippy::all)]
2
3use std::collections::HashMap;
4
5pub const DEFAULT_RRF_K: f64 = 60.0;
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct RankedHit<T> {
9    pub key: String,
10    pub item: T,
11    pub score: f64,
12}
13
14pub fn reciprocal_rank_fusion<T>(
15    ranked_lists: Vec<Vec<(String, T)>>,
16    limit: usize,
17    k: f64,
18) -> Vec<RankedHit<T>> {
19    if limit == 0 {
20        return Vec::new();
21    }
22
23    let mut scores: HashMap<String, f64> = HashMap::new();
24    let mut items: HashMap<String, T> = HashMap::new();
25
26    for ranked_list in ranked_lists {
27        for (rank, (key, item)) in ranked_list.into_iter().enumerate() {
28            let score = 1.0 / (k + rank as f64 + 1.0);
29            *scores.entry(key.clone()).or_default() += score;
30            items.entry(key).or_insert(item);
31        }
32    }
33
34    let mut merged = scores
35        .into_iter()
36        .filter_map(|(key, score)| {
37            let item = items.remove(&key)?;
38            Some(RankedHit { key, item, score })
39        })
40        .collect::<Vec<_>>();
41
42    merged.sort_by(|a, b| {
43        b.score
44            .partial_cmp(&a.score)
45            .unwrap_or(std::cmp::Ordering::Equal)
46            .then_with(|| a.key.cmp(&b.key))
47    });
48    merged.truncate(limit);
49    merged
50}
51
52pub fn build_fts_match_query(query: &str) -> Option<String> {
53    let terms = query
54        .split_whitespace()
55        .map(str::trim)
56        .filter(|term| !term.is_empty())
57        .map(|term| format!("\"{}\"", term.replace('"', "\"\"")))
58        .collect::<Vec<_>>();
59
60    if terms.is_empty() {
61        None
62    } else {
63        Some(terms.join(" AND "))
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::{DEFAULT_RRF_K, build_fts_match_query, reciprocal_rank_fusion};
70
71    #[test]
72    fn rrf_merges_ranked_lists_by_shared_keys() {
73        let fused = reciprocal_rank_fusion(
74            vec![
75                vec![("a".to_string(), "vector-a"), ("b".to_string(), "vector-b")],
76                vec![("b".to_string(), "fts-b"), ("c".to_string(), "fts-c")],
77            ],
78            3,
79            DEFAULT_RRF_K,
80        );
81
82        assert_eq!(
83            fused.iter().map(|hit| hit.key.as_str()).collect::<Vec<_>>(),
84            vec!["b", "a", "c"]
85        );
86        assert_eq!(fused[0].item, "vector-b");
87    }
88
89    #[test]
90    fn fts_query_quotes_terms_and_escapes_quotes() {
91        assert_eq!(
92            build_fts_match_query(r#"alpha "beta""#).as_deref(),
93            Some(r#""alpha" AND """beta""""#)
94        );
95        assert_eq!(build_fts_match_query("  ").as_deref(), None);
96    }
97}