oxify-vector 0.1.0

In-memory vector search and similarity operations for OxiFY (ported from OxiRS)
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
//! Metadata filtering for vector search
//!
//! Provides pre-filtering and post-filtering capabilities to constrain
//! search results based on metadata attributes.
//!
//! ## Example
//!
//! ```rust
//! use oxify_vector::filter::{Filter, FilterCondition, FilterValue};
//!
//! // Filter by document type
//! let filter = Filter::new()
//!     .eq("type", "article")
//!     .gte("year", 2020);
//!
//! // Filter with OR conditions
//! let filter = Filter::any(vec![
//!     Filter::new().eq("category", "tech"),
//!     Filter::new().eq("category", "science"),
//! ]);
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Value types for metadata filtering
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum FilterValue {
    /// String value
    String(String),
    /// Integer value
    Int(i64),
    /// Float value
    Float(f64),
    /// Boolean value
    Bool(bool),
    /// List of strings (for IN operator)
    StringList(Vec<String>),
    /// List of integers (for IN operator)
    IntList(Vec<i64>),
}

impl From<&str> for FilterValue {
    fn from(s: &str) -> Self {
        FilterValue::String(s.to_string())
    }
}

impl From<String> for FilterValue {
    fn from(s: String) -> Self {
        FilterValue::String(s)
    }
}

impl From<i64> for FilterValue {
    fn from(v: i64) -> Self {
        FilterValue::Int(v)
    }
}

impl From<i32> for FilterValue {
    fn from(v: i32) -> Self {
        FilterValue::Int(v as i64)
    }
}

impl From<f64> for FilterValue {
    fn from(v: f64) -> Self {
        FilterValue::Float(v)
    }
}

impl From<bool> for FilterValue {
    fn from(v: bool) -> Self {
        FilterValue::Bool(v)
    }
}

impl From<Vec<String>> for FilterValue {
    fn from(v: Vec<String>) -> Self {
        FilterValue::StringList(v)
    }
}

impl From<Vec<&str>> for FilterValue {
    fn from(v: Vec<&str>) -> Self {
        FilterValue::StringList(v.into_iter().map(|s| s.to_string()).collect())
    }
}

impl From<Vec<i64>> for FilterValue {
    fn from(v: Vec<i64>) -> Self {
        FilterValue::IntList(v)
    }
}

/// Filter condition operators
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum FilterCondition {
    /// Equal to
    Eq(String, FilterValue),
    /// Not equal to
    Ne(String, FilterValue),
    /// Greater than
    Gt(String, FilterValue),
    /// Greater than or equal to
    Gte(String, FilterValue),
    /// Less than
    Lt(String, FilterValue),
    /// Less than or equal to
    Lte(String, FilterValue),
    /// Value is in list
    In(String, FilterValue),
    /// Value is not in list
    NotIn(String, FilterValue),
    /// Field contains substring
    Contains(String, String),
    /// Field starts with prefix
    StartsWith(String, String),
    /// All conditions must match (AND)
    All(Vec<FilterCondition>),
    /// Any condition must match (OR)
    Any(Vec<FilterCondition>),
    /// Negate condition (NOT)
    Not(Box<FilterCondition>),
}

/// Metadata filter builder
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Filter {
    conditions: Vec<FilterCondition>,
}

impl Filter {
    /// Create a new empty filter
    pub fn new() -> Self {
        Self {
            conditions: Vec::new(),
        }
    }

    /// Create a filter that matches ALL conditions (AND)
    pub fn all(filters: Vec<Filter>) -> Self {
        let conditions: Vec<FilterCondition> =
            filters.into_iter().flat_map(|f| f.conditions).collect();
        Self { conditions }
    }

    /// Create a filter that matches ANY condition (OR)
    pub fn any(filters: Vec<Filter>) -> Self {
        let inner: Vec<FilterCondition> = filters
            .into_iter()
            .map(|f| {
                if f.conditions.len() == 1 {
                    f.conditions.into_iter().next().unwrap()
                } else {
                    FilterCondition::All(f.conditions)
                }
            })
            .collect();
        Self {
            conditions: vec![FilterCondition::Any(inner)],
        }
    }

    /// Add equality condition
    pub fn eq<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, value: V) -> Self {
        self.conditions
            .push(FilterCondition::Eq(key.into(), value.into()));
        self
    }

    /// Add not-equal condition
    pub fn ne<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, value: V) -> Self {
        self.conditions
            .push(FilterCondition::Ne(key.into(), value.into()));
        self
    }

    /// Add greater-than condition
    pub fn gt<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, value: V) -> Self {
        self.conditions
            .push(FilterCondition::Gt(key.into(), value.into()));
        self
    }

    /// Add greater-than-or-equal condition
    pub fn gte<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, value: V) -> Self {
        self.conditions
            .push(FilterCondition::Gte(key.into(), value.into()));
        self
    }

    /// Add less-than condition
    pub fn lt<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, value: V) -> Self {
        self.conditions
            .push(FilterCondition::Lt(key.into(), value.into()));
        self
    }

    /// Add less-than-or-equal condition
    pub fn lte<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, value: V) -> Self {
        self.conditions
            .push(FilterCondition::Lte(key.into(), value.into()));
        self
    }

    /// Add IN condition (value must be in list)
    pub fn in_list<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, values: V) -> Self {
        self.conditions
            .push(FilterCondition::In(key.into(), values.into()));
        self
    }

    /// Add NOT IN condition (value must not be in list)
    pub fn not_in<K: Into<String>, V: Into<FilterValue>>(mut self, key: K, values: V) -> Self {
        self.conditions
            .push(FilterCondition::NotIn(key.into(), values.into()));
        self
    }

    /// Add contains condition (string contains substring)
    pub fn contains<K: Into<String>, V: Into<String>>(mut self, key: K, substring: V) -> Self {
        self.conditions
            .push(FilterCondition::Contains(key.into(), substring.into()));
        self
    }

    /// Add starts_with condition
    pub fn starts_with<K: Into<String>, V: Into<String>>(mut self, key: K, prefix: V) -> Self {
        self.conditions
            .push(FilterCondition::StartsWith(key.into(), prefix.into()));
        self
    }

    /// Get the conditions
    pub fn conditions(&self) -> &[FilterCondition] {
        &self.conditions
    }

    /// Check if filter is empty
    pub fn is_empty(&self) -> bool {
        self.conditions.is_empty()
    }

    /// Evaluate filter against metadata
    pub fn matches(&self, metadata: &Metadata) -> bool {
        self.conditions
            .iter()
            .all(|c| evaluate_condition(c, metadata))
    }
}

/// Metadata storage for a single entity
pub type Metadata = HashMap<String, FilterValue>;

/// Evaluate a single condition against metadata
fn evaluate_condition(condition: &FilterCondition, metadata: &Metadata) -> bool {
    match condition {
        FilterCondition::Eq(key, expected) => metadata.get(key) == Some(expected),
        FilterCondition::Ne(key, expected) => metadata.get(key) != Some(expected),
        FilterCondition::Gt(key, expected) => metadata
            .get(key)
            .is_some_and(|v| compare_values(v, expected) == Some(std::cmp::Ordering::Greater)),
        FilterCondition::Gte(key, expected) => metadata.get(key).is_some_and(|v| {
            matches!(
                compare_values(v, expected),
                Some(std::cmp::Ordering::Greater | std::cmp::Ordering::Equal)
            )
        }),
        FilterCondition::Lt(key, expected) => metadata
            .get(key)
            .is_some_and(|v| compare_values(v, expected) == Some(std::cmp::Ordering::Less)),
        FilterCondition::Lte(key, expected) => metadata.get(key).is_some_and(|v| {
            matches!(
                compare_values(v, expected),
                Some(std::cmp::Ordering::Less | std::cmp::Ordering::Equal)
            )
        }),
        FilterCondition::In(key, values) => {
            metadata.get(key).is_some_and(|v| value_in_list(v, values))
        }
        FilterCondition::NotIn(key, values) => {
            metadata.get(key).is_none_or(|v| !value_in_list(v, values))
        }
        FilterCondition::Contains(key, substring) => metadata.get(key).is_some_and(|v| {
            if let FilterValue::String(s) = v {
                s.contains(substring)
            } else {
                false
            }
        }),
        FilterCondition::StartsWith(key, prefix) => metadata.get(key).is_some_and(|v| {
            if let FilterValue::String(s) = v {
                s.starts_with(prefix)
            } else {
                false
            }
        }),
        FilterCondition::All(conditions) => {
            conditions.iter().all(|c| evaluate_condition(c, metadata))
        }
        FilterCondition::Any(conditions) => {
            conditions.iter().any(|c| evaluate_condition(c, metadata))
        }
        FilterCondition::Not(condition) => !evaluate_condition(condition, metadata),
    }
}

/// Compare two filter values
fn compare_values(a: &FilterValue, b: &FilterValue) -> Option<std::cmp::Ordering> {
    match (a, b) {
        (FilterValue::Int(a), FilterValue::Int(b)) => Some(a.cmp(b)),
        (FilterValue::Float(a), FilterValue::Float(b)) => a.partial_cmp(b),
        (FilterValue::Int(a), FilterValue::Float(b)) => (*a as f64).partial_cmp(b),
        (FilterValue::Float(a), FilterValue::Int(b)) => a.partial_cmp(&(*b as f64)),
        (FilterValue::String(a), FilterValue::String(b)) => Some(a.cmp(b)),
        _ => None,
    }
}

/// Check if value is in list
fn value_in_list(value: &FilterValue, list: &FilterValue) -> bool {
    match (value, list) {
        (FilterValue::String(v), FilterValue::StringList(l)) => l.contains(v),
        (FilterValue::Int(v), FilterValue::IntList(l)) => l.contains(v),
        _ => false,
    }
}

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

    fn test_metadata() -> Metadata {
        let mut m = HashMap::new();
        m.insert(
            "type".to_string(),
            FilterValue::String("article".to_string()),
        );
        m.insert("year".to_string(), FilterValue::Int(2023));
        m.insert("score".to_string(), FilterValue::Float(0.95));
        m.insert("published".to_string(), FilterValue::Bool(true));
        m.insert(
            "category".to_string(),
            FilterValue::String("tech".to_string()),
        );
        m
    }

    #[test]
    fn test_eq_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().eq("type", "article");
        assert!(filter.matches(&metadata));

        let filter = Filter::new().eq("type", "book");
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_ne_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().ne("type", "book");
        assert!(filter.matches(&metadata));

        let filter = Filter::new().ne("type", "article");
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_gt_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().gt("year", 2020i64);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().gt("year", 2023i64);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_gte_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().gte("year", 2023i64);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().gte("year", 2024i64);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_lt_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().lt("year", 2026i64);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().lt("year", 2023i64);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_lte_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().lte("year", 2023i64);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().lte("year", 2022i64);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_in_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().in_list("type", vec!["article", "book"]);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().in_list("type", vec!["book", "journal"]);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_not_in_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().not_in("type", vec!["book", "journal"]);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().not_in("type", vec!["article", "journal"]);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_contains_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().contains("type", "art");
        assert!(filter.matches(&metadata));

        let filter = Filter::new().contains("type", "xyz");
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_starts_with_filter() {
        let metadata = test_metadata();

        let filter = Filter::new().starts_with("type", "art");
        assert!(filter.matches(&metadata));

        let filter = Filter::new().starts_with("type", "icle");
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_combined_filters() {
        let metadata = test_metadata();

        // All conditions must match (AND)
        let filter = Filter::new().eq("type", "article").gte("year", 2020i64);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().eq("type", "article").gt("year", 2026i64);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_or_filters() {
        let metadata = test_metadata();

        // Any condition can match (OR)
        let filter = Filter::any(vec![
            Filter::new().eq("category", "tech"),
            Filter::new().eq("category", "science"),
        ]);
        assert!(filter.matches(&metadata));

        let filter = Filter::any(vec![
            Filter::new().eq("category", "sports"),
            Filter::new().eq("category", "music"),
        ]);
        assert!(!filter.matches(&metadata));
    }

    #[test]
    fn test_missing_field() {
        let metadata = test_metadata();

        // Missing field returns false for eq
        let filter = Filter::new().eq("nonexistent", "value");
        assert!(!filter.matches(&metadata));

        // Missing field returns true for ne
        let filter = Filter::new().ne("nonexistent", "value");
        assert!(filter.matches(&metadata));
    }

    #[test]
    fn test_float_comparison() {
        let metadata = test_metadata();

        let filter = Filter::new().gt("score", 0.9);
        assert!(filter.matches(&metadata));

        let filter = Filter::new().lt("score", 1.0);
        assert!(filter.matches(&metadata));
    }

    #[test]
    fn test_filter_is_empty() {
        let filter = Filter::new();
        assert!(filter.is_empty());

        let filter = Filter::new().eq("key", "value");
        assert!(!filter.is_empty());
    }
}