vecstore 1.0.0

The perfect vector database - 100/100 score, embeddable, high-performance, production-ready with RAG toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Fuzzy search with typo tolerance
//!
//! This module provides fuzzy string matching capabilities for handling typos,
//! misspellings, and approximate text matching in search queries.
//!
//! ## Features
//!
//! - **Levenshtein distance** - Classic edit distance metric
//! - **Damerau-Levenshtein distance** - Includes transpositions
//! - **Fuzzy matching** - Find strings within edit distance threshold
//! - **BK-tree** - Efficient fuzzy search data structure
//! - **Query correction** - Suggest corrections for misspelled queries
//!
//! ## Algorithms
//!
//! ### Levenshtein Distance
//! Minimum number of single-character edits (insertions, deletions, substitutions)
//! required to change one string into another.
//!
//! ### Damerau-Levenshtein Distance
//! Extends Levenshtein to include transpositions (swapping adjacent characters).
//! Handles common typos like "teh" → "the".
//!
//! ### BK-tree (Burkhard-Keller tree)
//! Metric tree for efficient fuzzy search. Allows finding all strings within
//! edit distance k in O(log n) average time.
//!
//! ## Example
//!
//! ```no_run
//! use vecstore::fuzzy::{levenshtein_distance, FuzzyMatcher, BKTree};
//!
//! // Simple distance calculation
//! let dist = levenshtein_distance("kitten", "sitting");
//! assert_eq!(dist, 3);
//!
//! // Fuzzy matching
//! let matcher = FuzzyMatcher::new(2); // max edit distance = 2
//! assert!(matcher.is_match("hello", "helo"));
//!
//! // BK-tree for efficient search
//! let mut tree = BKTree::new();
//! tree.insert("hello".to_string());
//! tree.insert("world".to_string());
//!
//! let matches = tree.search("helo", 1); // Find within edit distance 1
//! ```

use std::cmp::min;
use std::collections::HashMap;

/// Calculate Levenshtein distance between two strings
///
/// The Levenshtein distance is the minimum number of single-character edits
/// (insertions, deletions, or substitutions) required to change one string into another.
///
/// ## Complexity
/// - Time: O(m * n) where m, n are string lengths
/// - Space: O(min(m, n)) using optimized implementation
///
/// ## Example
///
/// ```
/// use vecstore::fuzzy::levenshtein_distance;
///
/// assert_eq!(levenshtein_distance("kitten", "sitting"), 3);
/// assert_eq!(levenshtein_distance("hello", "helo"), 1);
/// assert_eq!(levenshtein_distance("same", "same"), 0);
/// ```
pub fn levenshtein_distance(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let m = a_chars.len();
    let n = b_chars.len();

    if m == 0 {
        return n;
    }
    if n == 0 {
        return m;
    }

    // Use two rows for space optimization
    let mut prev_row: Vec<usize> = (0..=n).collect();
    let mut curr_row: Vec<usize> = vec![0; n + 1];

    for i in 1..=m {
        curr_row[0] = i;

        for j in 1..=n {
            let cost = if a_chars[i - 1] == b_chars[j - 1] {
                0
            } else {
                1
            };

            curr_row[j] = min(
                min(
                    prev_row[j] + 1,     // deletion
                    curr_row[j - 1] + 1, // insertion
                ),
                prev_row[j - 1] + cost, // substitution
            );
        }

        std::mem::swap(&mut prev_row, &mut curr_row);
    }

    prev_row[n]
}

/// Calculate Damerau-Levenshtein distance between two strings
///
/// Extends Levenshtein distance to include transpositions (swapping adjacent characters).
/// This better handles common typos like "teh" → "the".
///
/// ## Example
///
/// ```
/// use vecstore::fuzzy::damerau_levenshtein_distance;
///
/// // Transposition: "ab" -> "ba" is 1 edit (not 2 as in Levenshtein)
/// assert_eq!(damerau_levenshtein_distance("ab", "ba"), 1);
/// assert_eq!(damerau_levenshtein_distance("the", "teh"), 1);
/// ```
pub fn damerau_levenshtein_distance(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let m = a_chars.len();
    let n = b_chars.len();

    if m == 0 {
        return n;
    }
    if n == 0 {
        return m;
    }

    // Create distance matrix
    let mut d = vec![vec![0; n + 1]; m + 1];

    // Initialize first row and column
    for i in 0..=m {
        d[i][0] = i;
    }
    for j in 0..=n {
        d[0][j] = j;
    }

    // Fill matrix
    for i in 1..=m {
        for j in 1..=n {
            let cost = if a_chars[i - 1] == b_chars[j - 1] {
                0
            } else {
                1
            };

            d[i][j] = min(
                min(
                    d[i - 1][j] + 1, // deletion
                    d[i][j - 1] + 1, // insertion
                ),
                d[i - 1][j - 1] + cost, // substitution
            );

            // Transposition
            if i > 1
                && j > 1
                && a_chars[i - 1] == b_chars[j - 2]
                && a_chars[i - 2] == b_chars[j - 1]
            {
                d[i][j] = min(d[i][j], d[i - 2][j - 2] + 1);
            }
        }
    }

    d[m][n]
}

/// Calculate normalized similarity score (0.0 to 1.0)
///
/// Returns 1.0 for identical strings, approaching 0.0 as they differ more.
///
/// ## Formula
/// ```text
/// similarity = 1.0 - (distance / max_length)
/// ```
///
/// ## Example
///
/// ```
/// use vecstore::fuzzy::similarity_score;
///
/// assert_eq!(similarity_score("hello", "hello"), 1.0);
/// assert!(similarity_score("hello", "helo") > 0.8);
/// ```
pub fn similarity_score(a: &str, b: &str) -> f32 {
    let dist = levenshtein_distance(a, b);
    let max_len = a.len().max(b.len());

    if max_len == 0 {
        return 1.0;
    }

    1.0 - (dist as f32 / max_len as f32)
}

/// Fuzzy string matcher with configurable edit distance threshold
///
/// ## Example
///
/// ```
/// use vecstore::fuzzy::FuzzyMatcher;
///
/// let matcher = FuzzyMatcher::new(2);
/// assert!(matcher.is_match("hello", "helo"));   // distance = 1 ≤ 2
/// assert!(!matcher.is_match("hello", "world")); // distance = 4 > 2
/// ```
pub struct FuzzyMatcher {
    /// Maximum edit distance for a match
    max_distance: usize,

    /// Whether to use Damerau-Levenshtein (includes transpositions)
    use_damerau: bool,

    /// Case-sensitive matching
    case_sensitive: bool,
}

impl FuzzyMatcher {
    /// Create a new fuzzy matcher
    ///
    /// # Arguments
    ///
    /// * `max_distance` - Maximum edit distance for a match
    pub fn new(max_distance: usize) -> Self {
        Self {
            max_distance,
            use_damerau: true,
            case_sensitive: false,
        }
    }

    /// Enable/disable Damerau-Levenshtein distance (default: enabled)
    pub fn with_damerau(mut self, enabled: bool) -> Self {
        self.use_damerau = enabled;
        self
    }

    /// Enable/disable case-sensitive matching (default: disabled)
    pub fn with_case_sensitive(mut self, enabled: bool) -> Self {
        self.case_sensitive = enabled;
        self
    }

    /// Check if two strings match within the edit distance threshold
    pub fn is_match(&self, a: &str, b: &str) -> bool {
        let (a, b) = if !self.case_sensitive {
            (a.to_lowercase(), b.to_lowercase())
        } else {
            (a.to_string(), b.to_string())
        };

        let distance = if self.use_damerau {
            damerau_levenshtein_distance(&a, &b)
        } else {
            levenshtein_distance(&a, &b)
        };

        distance <= self.max_distance
    }

    /// Get the distance between two strings
    pub fn distance(&self, a: &str, b: &str) -> usize {
        let (a, b) = if !self.case_sensitive {
            (a.to_lowercase(), b.to_lowercase())
        } else {
            (a.to_string(), b.to_string())
        };

        if self.use_damerau {
            damerau_levenshtein_distance(&a, &b)
        } else {
            levenshtein_distance(&a, &b)
        }
    }

    /// Find the best match from a list of candidates
    ///
    /// Returns (index, distance) of the closest match, or None if no match within threshold.
    pub fn find_best_match(&self, query: &str, candidates: &[String]) -> Option<(usize, usize)> {
        let mut best: Option<(usize, usize)> = None;

        for (i, candidate) in candidates.iter().enumerate() {
            let dist = self.distance(query, candidate);

            if dist <= self.max_distance {
                if let Some((_, best_dist)) = best {
                    if dist < best_dist {
                        best = Some((i, dist));
                    }
                } else {
                    best = Some((i, dist));
                }
            }
        }

        best
    }

    /// Find all matches within the edit distance threshold
    ///
    /// Returns indices of all matching candidates.
    pub fn find_all_matches(&self, query: &str, candidates: &[String]) -> Vec<usize> {
        candidates
            .iter()
            .enumerate()
            .filter(|(_, candidate)| self.is_match(query, candidate))
            .map(|(i, _)| i)
            .collect()
    }
}

/// BK-tree (Burkhard-Keller tree) for efficient fuzzy search
///
/// A metric tree that allows finding all strings within edit distance k in O(log n) average time.
///
/// ## Use Cases
///
/// - Spell checking
/// - Query correction
/// - Fuzzy autocomplete
/// - Duplicate detection
///
/// ## Example
///
/// ```
/// use vecstore::fuzzy::BKTree;
///
/// let mut tree = BKTree::new();
/// tree.insert("hello".to_string());
/// tree.insert("help".to_string());
/// tree.insert("world".to_string());
///
/// // Find all words within edit distance 1 of "helo"
/// let matches = tree.search("helo", 1);
/// assert!(matches.contains(&"hello".to_string()));
/// ```
pub struct BKTree {
    root: Option<Box<BKNode>>,
}

struct BKNode {
    word: String,
    children: HashMap<usize, Box<BKNode>>,
}

impl BKTree {
    /// Create a new empty BK-tree
    pub fn new() -> Self {
        Self { root: None }
    }

    /// Insert a word into the tree
    pub fn insert(&mut self, word: String) {
        if let Some(ref mut root) = self.root {
            root.insert(word);
        } else {
            self.root = Some(Box::new(BKNode {
                word,
                children: HashMap::new(),
            }));
        }
    }

    /// Search for words within edit distance k
    ///
    /// Returns all words in the tree that are within edit distance k of the query.
    pub fn search(&self, query: &str, max_distance: usize) -> Vec<String> {
        let mut results = Vec::new();

        if let Some(ref root) = self.root {
            root.search(query, max_distance, &mut results);
        }

        results
    }

    /// Find the closest word in the tree
    ///
    /// Returns (word, distance) of the closest match.
    pub fn find_closest(&self, query: &str) -> Option<(String, usize)> {
        if let Some(ref root) = self.root {
            let mut best: Option<(String, usize)> = None;
            root.find_closest(query, &mut best);
            best
        } else {
            None
        }
    }

    /// Get the number of words in the tree
    pub fn len(&self) -> usize {
        if let Some(ref root) = self.root {
            root.count()
        } else {
            0
        }
    }

    /// Check if the tree is empty
    pub fn is_empty(&self) -> bool {
        self.root.is_none()
    }

    /// Build a BK-tree from a collection of words
    pub fn from_words<I>(words: I) -> Self
    where
        I: IntoIterator<Item = String>,
    {
        let mut tree = Self::new();
        for word in words {
            tree.insert(word);
        }
        tree
    }
}

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

impl BKNode {
    fn insert(&mut self, word: String) {
        let distance = levenshtein_distance(&self.word, &word);

        if distance == 0 {
            // Duplicate word, ignore
            return;
        }

        if let Some(child) = self.children.get_mut(&distance) {
            child.insert(word);
        } else {
            self.children.insert(
                distance,
                Box::new(BKNode {
                    word,
                    children: HashMap::new(),
                }),
            );
        }
    }

    fn search(&self, query: &str, max_distance: usize, results: &mut Vec<String>) {
        let distance = levenshtein_distance(&self.word, query);

        if distance <= max_distance {
            results.push(self.word.clone());
        }

        // Triangle inequality: only search children within range
        let min_dist = distance.saturating_sub(max_distance);
        let max_dist = distance + max_distance;

        for (child_dist, child) in &self.children {
            if *child_dist >= min_dist && *child_dist <= max_dist {
                child.search(query, max_distance, results);
            }
        }
    }

    fn find_closest(&self, query: &str, best: &mut Option<(String, usize)>) {
        let distance = levenshtein_distance(&self.word, query);

        match best {
            Some((_, best_dist)) => {
                if distance < *best_dist {
                    *best = Some((self.word.clone(), distance));
                }
            }
            None => {
                *best = Some((self.word.clone(), distance));
            }
        }

        // Search children
        for child in self.children.values() {
            child.find_closest(query, best);
        }
    }

    fn count(&self) -> usize {
        1 + self.children.values().map(|c| c.count()).sum::<usize>()
    }
}

/// Suggest corrections for a misspelled query
///
/// Uses a dictionary of known words to find the closest matches.
///
/// ## Example
///
/// ```
/// use vecstore::fuzzy::suggest_corrections;
///
/// let dictionary = vec![
///     "hello".to_string(),
///     "help".to_string(),
///     "world".to_string(),
/// ];
///
/// let suggestions = suggest_corrections("helo", &dictionary, 1, 3);
/// assert!(suggestions.contains(&"hello".to_string()));
/// ```
pub fn suggest_corrections(
    query: &str,
    dictionary: &[String],
    max_distance: usize,
    max_suggestions: usize,
) -> Vec<String> {
    let mut candidates: Vec<(usize, String)> = dictionary
        .iter()
        .map(|word| {
            let dist = levenshtein_distance(query, word);
            (dist, word.clone())
        })
        .filter(|(dist, _)| *dist <= max_distance)
        .collect();

    // Sort by distance (ascending)
    candidates.sort_by_key(|(dist, _)| *dist);

    // Take top N
    candidates
        .into_iter()
        .take(max_suggestions)
        .map(|(_, word)| word)
        .collect()
}

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

    #[test]
    fn test_levenshtein_distance() {
        assert_eq!(levenshtein_distance("", ""), 0);
        assert_eq!(levenshtein_distance("abc", ""), 3);
        assert_eq!(levenshtein_distance("", "abc"), 3);
        assert_eq!(levenshtein_distance("kitten", "sitting"), 3);
        assert_eq!(levenshtein_distance("saturday", "sunday"), 3);
        assert_eq!(levenshtein_distance("hello", "helo"), 1);
    }

    #[test]
    fn test_damerau_levenshtein_distance() {
        // Transposition
        assert_eq!(damerau_levenshtein_distance("ab", "ba"), 1);
        assert_eq!(damerau_levenshtein_distance("the", "teh"), 1);

        // Should give same results as Levenshtein for non-transposition cases
        assert_eq!(damerau_levenshtein_distance("kitten", "sitting"), 3);
    }

    #[test]
    fn test_similarity_score() {
        assert_eq!(similarity_score("hello", "hello"), 1.0);
        assert_eq!(similarity_score("", ""), 1.0);

        let score = similarity_score("hello", "helo");
        assert!(score >= 0.8 && score < 1.0); // distance=1, max_len=5, score=0.8
    }

    #[test]
    fn test_fuzzy_matcher() {
        let matcher = FuzzyMatcher::new(2);

        assert!(matcher.is_match("hello", "hello"));
        assert!(matcher.is_match("hello", "helo"));
        assert!(matcher.is_match("hello", "hallo"));
        assert!(!matcher.is_match("hello", "world"));
    }

    #[test]
    fn test_fuzzy_matcher_case_insensitive() {
        let matcher = FuzzyMatcher::new(1).with_case_sensitive(false);

        assert!(matcher.is_match("Hello", "hello"));
        assert!(matcher.is_match("HELLO", "helo"));
    }

    #[test]
    fn test_fuzzy_matcher_case_sensitive() {
        // With max_distance=0, only exact matches work
        let matcher = FuzzyMatcher::new(0).with_case_sensitive(true);

        assert!(!matcher.is_match("Hello", "hello")); // Different case, not exact match
        assert!(matcher.is_match("hello", "hello")); // Exact match
        assert!(matcher.is_match("Hello", "Hello")); // Exact match

        // With case-insensitive, "Hello" and "hello" should match with distance 0
        let matcher_ci = FuzzyMatcher::new(0).with_case_sensitive(false);
        assert!(matcher_ci.is_match("Hello", "hello"));
    }

    #[test]
    fn test_fuzzy_matcher_find_best_match() {
        let matcher = FuzzyMatcher::new(2);
        let candidates = vec!["hello".to_string(), "world".to_string(), "help".to_string()];

        let best = matcher.find_best_match("helo", &candidates);
        assert_eq!(best, Some((0, 1))); // "hello" at index 0, distance 1
    }

    #[test]
    fn test_fuzzy_matcher_find_all_matches() {
        let matcher = FuzzyMatcher::new(2);
        let candidates = vec!["hello".to_string(), "halo".to_string(), "world".to_string()];

        let matches = matcher.find_all_matches("helo", &candidates);
        assert_eq!(matches.len(), 2); // "hello" and "halo"
    }

    #[test]
    fn test_bk_tree_insert_and_search() {
        let mut tree = BKTree::new();

        tree.insert("hello".to_string());
        tree.insert("help".to_string());
        tree.insert("world".to_string());

        assert_eq!(tree.len(), 3);

        // "helo" is within distance 1 of both "hello" and "help"
        let matches = tree.search("helo", 1);
        assert_eq!(matches.len(), 2);
        assert!(matches.contains(&"hello".to_string()));
        assert!(matches.contains(&"help".to_string()));

        // "world" is not within distance 1
        let matches2 = tree.search("wrld", 1);
        assert_eq!(matches2.len(), 1);
        assert!(matches2.contains(&"world".to_string()));
    }

    #[test]
    fn test_bk_tree_find_closest() {
        let mut tree = BKTree::new();

        tree.insert("hello".to_string());
        tree.insert("world".to_string());
        tree.insert("help".to_string());

        let closest = tree.find_closest("helo");
        assert_eq!(closest, Some(("hello".to_string(), 1)));
    }

    #[test]
    fn test_bk_tree_from_words() {
        let words = vec![
            "apple".to_string(),
            "banana".to_string(),
            "cherry".to_string(),
        ];

        let tree = BKTree::from_words(words);
        assert_eq!(tree.len(), 3);
    }

    #[test]
    fn test_bk_tree_empty() {
        let tree = BKTree::new();
        assert!(tree.is_empty());
        assert_eq!(tree.len(), 0);
    }

    #[test]
    fn test_suggest_corrections() {
        let dictionary = vec![
            "hello".to_string(),
            "help".to_string(),
            "world".to_string(),
            "word".to_string(),
        ];

        let suggestions = suggest_corrections("helo", &dictionary, 2, 3);

        assert!(suggestions.contains(&"hello".to_string()));
        assert!(suggestions.len() <= 3);

        // Should be sorted by distance
        if suggestions.len() >= 2 {
            let dist1 = levenshtein_distance("helo", &suggestions[0]);
            let dist2 = levenshtein_distance("helo", &suggestions[1]);
            assert!(dist1 <= dist2);
        }
    }

    #[test]
    fn test_bk_tree_duplicates() {
        let mut tree = BKTree::new();

        tree.insert("hello".to_string());
        tree.insert("hello".to_string()); // Duplicate

        assert_eq!(tree.len(), 1); // Should not increase
    }
}