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
//! Advanced Metadata Filtering
//!
//! This module provides enhanced filtering capabilities beyond the basic
//! comparison operators, including:
//!
//! - **JSON Path queries**: Navigate nested JSON structures (`user.address.city = 'NYC'`)
//! - **Regular expressions**: Pattern matching (`name ~= '^John'`)
//! - **Array operations**: Contains, length, any/all (`tags contains 'rust'`)
//! - **Composite filters**: AND/OR/NOT with complex expressions
//!
//! ## Example
//!
//! ```no_run
//! use vecstore::advanced_filter::{AdvancedFilter, FilterBuilder};
//!
//! # fn main() -> anyhow::Result<()> {
//! let filter = FilterBuilder::new()
//!     .json_path("user.profile.age", ">=", 18)?
//!     .and()
//!     .regex("email", r".*@example\.com$")?
//!     .and()
//!     .array_contains("tags", "premium")?
//!     .build();
//!
//! // Use with VecStore query
//! // let results = store.query_with_advanced_filter(query, filter)?;
//! # Ok(())
//! # }
//! ```

use anyhow::{anyhow, Result};
use regex::Regex;
use serde_json::Value;
use std::collections::HashSet;

/// Advanced filter expression
#[derive(Debug, Clone)]
pub enum AdvancedFilter {
    /// JSON path query: path, operator, value
    JsonPath(String, String, Value),
    /// Regular expression: field, pattern
    Regex(String, String),
    /// Array contains: field, value
    ArrayContains(String, Value),
    /// Array length: field, operator, length
    ArrayLength(String, String, usize),
    /// All array elements match condition
    ArrayAll(String, Box<AdvancedFilter>),
    /// Any array element matches condition
    ArrayAny(String, Box<AdvancedFilter>),
    /// Logical AND
    And(Vec<AdvancedFilter>),
    /// Logical OR
    Or(Vec<AdvancedFilter>),
    /// Logical NOT
    Not(Box<AdvancedFilter>),
    /// Basic comparison (field, operator, value)
    Basic(String, String, Value),
}

impl AdvancedFilter {
    /// Evaluate filter against metadata
    pub fn matches(&self, metadata: &Value) -> Result<bool> {
        match self {
            AdvancedFilter::JsonPath(path, op, value) => {
                let extracted = extract_json_path(metadata, path)?;
                compare_values(&extracted, op, value)
            }
            AdvancedFilter::Regex(field, pattern) => {
                let field_value = extract_field(metadata, field)?;
                if let Some(s) = field_value.as_str() {
                    let re = Regex::new(pattern)?;
                    Ok(re.is_match(s))
                } else {
                    Ok(false)
                }
            }
            AdvancedFilter::ArrayContains(field, value) => {
                let field_value = extract_field(metadata, field)?;
                if let Some(arr) = field_value.as_array() {
                    Ok(arr.contains(value))
                } else {
                    Ok(false)
                }
            }
            AdvancedFilter::ArrayLength(field, op, length) => {
                let field_value = extract_field(metadata, field)?;
                if let Some(arr) = field_value.as_array() {
                    let actual_length = arr.len();
                    compare_numbers(actual_length as i64, op, *length as i64)
                } else {
                    Ok(false)
                }
            }
            AdvancedFilter::ArrayAll(field, condition) => {
                let field_value = extract_field(metadata, field)?;
                if let Some(arr) = field_value.as_array() {
                    for item in arr {
                        if !condition.matches(item)? {
                            return Ok(false);
                        }
                    }
                    Ok(true)
                } else {
                    Ok(false)
                }
            }
            AdvancedFilter::ArrayAny(field, condition) => {
                let field_value = extract_field(metadata, field)?;
                if let Some(arr) = field_value.as_array() {
                    for item in arr {
                        if condition.matches(item)? {
                            return Ok(true);
                        }
                    }
                    Ok(false)
                } else {
                    Ok(false)
                }
            }
            AdvancedFilter::And(filters) => {
                for filter in filters {
                    if !filter.matches(metadata)? {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            AdvancedFilter::Or(filters) => {
                for filter in filters {
                    if filter.matches(metadata)? {
                        return Ok(true);
                    }
                }
                Ok(false)
            }
            AdvancedFilter::Not(filter) => Ok(!filter.matches(metadata)?),
            AdvancedFilter::Basic(field, op, value) => {
                let field_value = extract_field(metadata, field)?;
                compare_values(&field_value, op, value)
            }
        }
    }
}

/// Builder for advanced filters
pub struct FilterBuilder {
    filters: Vec<AdvancedFilter>,
}

impl FilterBuilder {
    /// Create a new filter builder
    pub fn new() -> Self {
        Self {
            filters: Vec::new(),
        }
    }

    /// Add JSON path query
    pub fn json_path(mut self, path: &str, op: &str, value: Value) -> Result<Self> {
        self.filters.push(AdvancedFilter::JsonPath(
            path.to_string(),
            op.to_string(),
            value,
        ));
        Ok(self)
    }

    /// Add regex pattern match
    pub fn regex(mut self, field: &str, pattern: &str) -> Result<Self> {
        // Validate regex
        Regex::new(pattern)?;
        self.filters.push(AdvancedFilter::Regex(
            field.to_string(),
            pattern.to_string(),
        ));
        Ok(self)
    }

    /// Add array contains check
    pub fn array_contains(mut self, field: &str, value: Value) -> Result<Self> {
        self.filters
            .push(AdvancedFilter::ArrayContains(field.to_string(), value));
        Ok(self)
    }

    /// Add array length check
    pub fn array_length(mut self, field: &str, op: &str, length: usize) -> Result<Self> {
        self.filters.push(AdvancedFilter::ArrayLength(
            field.to_string(),
            op.to_string(),
            length,
        ));
        Ok(self)
    }

    /// Add basic comparison
    pub fn basic(mut self, field: &str, op: &str, value: Value) -> Result<Self> {
        self.filters.push(AdvancedFilter::Basic(
            field.to_string(),
            op.to_string(),
            value,
        ));
        Ok(self)
    }

    /// Combine with AND
    pub fn and(self) -> Self {
        self
    }

    /// Build final filter
    pub fn build(self) -> AdvancedFilter {
        if self.filters.len() == 1 {
            self.filters.into_iter().next().unwrap()
        } else {
            AdvancedFilter::And(self.filters)
        }
    }
}

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

/// Extract value from JSON path
fn extract_json_path(metadata: &Value, path: &str) -> Result<Value> {
    let parts: Vec<&str> = path.split('.').collect();
    let mut current = metadata;

    for part in parts {
        // Handle array indexing like "items[0]"
        if let Some(bracket_pos) = part.find('[') {
            let field = &part[..bracket_pos];
            let index_str = &part[bracket_pos + 1..part.len() - 1];
            let index: usize = index_str.parse()?;

            current = &current[field];
            if let Some(arr) = current.as_array() {
                current = arr
                    .get(index)
                    .ok_or_else(|| anyhow!("Index out of bounds"))?;
            } else {
                return Err(anyhow!("Not an array: {}", field));
            }
        } else {
            current = &current[part];
        }

        if current.is_null() {
            return Ok(Value::Null);
        }
    }

    Ok(current.clone())
}

/// Extract field from metadata (supports dot notation for one level)
fn extract_field(metadata: &Value, field: &str) -> Result<Value> {
    if field.contains('.') {
        extract_json_path(metadata, field)
    } else {
        Ok(metadata[field].clone())
    }
}

/// Compare two JSON values
fn compare_values(a: &Value, op: &str, b: &Value) -> Result<bool> {
    match op {
        "=" | "==" => Ok(a == b),
        "!=" => Ok(a != b),
        ">" => compare_numeric(a, b, |x, y| x > y),
        ">=" => compare_numeric(a, b, |x, y| x >= y),
        "<" => compare_numeric(a, b, |x, y| x < y),
        "<=" => compare_numeric(a, b, |x, y| x <= y),
        _ => Err(anyhow!("Unknown operator: {}", op)),
    }
}

/// Compare numeric values
fn compare_numeric<F>(a: &Value, b: &Value, cmp: F) -> Result<bool>
where
    F: Fn(f64, f64) -> bool,
{
    let a_num = a.as_f64().ok_or_else(|| anyhow!("Not a number"))?;
    let b_num = b.as_f64().ok_or_else(|| anyhow!("Not a number"))?;
    Ok(cmp(a_num, b_num))
}

/// Compare integer numbers
fn compare_numbers(a: i64, op: &str, b: i64) -> Result<bool> {
    match op {
        "=" | "==" => Ok(a == b),
        "!=" => Ok(a != b),
        ">" => Ok(a > b),
        ">=" => Ok(a >= b),
        "<" => Ok(a < b),
        "<=" => Ok(a <= b),
        _ => Err(anyhow!("Unknown operator: {}", op)),
    }
}

/// Parse advanced filter from string
pub fn parse_advanced_filter(query: &str) -> Result<AdvancedFilter> {
    // Basic parsing - production version would use a proper parser
    let query = query.trim();

    // Check for regex operator
    if query.contains("~=") {
        let parts: Vec<&str> = query.split("~=").collect();
        if parts.len() == 2 {
            let field = parts[0].trim();
            let pattern = parts[1].trim().trim_matches('\'').trim_matches('"');
            return Ok(AdvancedFilter::Regex(
                field.to_string(),
                pattern.to_string(),
            ));
        }
    }

    // Check for array contains
    if query.contains(" contains ") {
        let parts: Vec<&str> = query.split(" contains ").collect();
        if parts.len() == 2 {
            let field = parts[0].trim();
            let value_str = parts[1].trim().trim_matches('\'').trim_matches('"');
            return Ok(AdvancedFilter::ArrayContains(
                field.to_string(),
                Value::String(value_str.to_string()),
            ));
        }
    }

    // Check for array.length
    if query.contains(".length ") {
        let parts: Vec<&str> = query.split(".length ").collect();
        if parts.len() == 2 {
            let field = parts[0].trim();
            let rest = parts[1].trim();
            for op in &[">=", "<=", ">", "<", "="] {
                if let Some(op_pos) = rest.find(op) {
                    let length_str = rest[op_pos + op.len()..].trim();
                    let length: usize = length_str.parse()?;
                    return Ok(AdvancedFilter::ArrayLength(
                        field.to_string(),
                        op.to_string(),
                        length,
                    ));
                }
            }
        }
    }

    // Basic comparison
    for op in &[">=", "<=", "!=", "=", ">", "<"] {
        if let Some(op_pos) = query.find(op) {
            let field = query[..op_pos].trim();
            let value_str = query[op_pos + op.len()..].trim();

            let value = if value_str.starts_with('\'') || value_str.starts_with('"') {
                Value::String(value_str.trim_matches('\'').trim_matches('"').to_string())
            } else if let Ok(num) = value_str.parse::<i64>() {
                Value::Number(num.into())
            } else if let Ok(num) = value_str.parse::<f64>() {
                Value::Number(serde_json::Number::from_f64(num).unwrap())
            } else {
                Value::String(value_str.to_string())
            };

            return Ok(AdvancedFilter::Basic(
                field.to_string(),
                op.to_string(),
                value,
            ));
        }
    }

    Err(anyhow!("Could not parse filter: {}", query))
}

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

    #[test]
    fn test_json_path_extraction() {
        let data = json!({
            "user": {
                "name": "Alice",
                "age": 30,
                "address": {
                    "city": "NYC"
                }
            }
        });

        let result = extract_json_path(&data, "user.name").unwrap();
        assert_eq!(result, json!("Alice"));

        let result = extract_json_path(&data, "user.address.city").unwrap();
        assert_eq!(result, json!("NYC"));
    }

    #[test]
    fn test_json_path_filter() {
        let data = json!({
            "user": {
                "age": 25
            }
        });

        let filter = AdvancedFilter::JsonPath("user.age".to_string(), ">=".to_string(), json!(18));
        assert!(filter.matches(&data).unwrap());

        let filter = AdvancedFilter::JsonPath("user.age".to_string(), "<".to_string(), json!(18));
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_regex_filter() {
        let data = json!({
            "email": "alice@example.com"
        });

        let filter = AdvancedFilter::Regex("email".to_string(), r".*@example\.com$".to_string());
        assert!(filter.matches(&data).unwrap());

        let filter = AdvancedFilter::Regex("email".to_string(), r".*@test\.com$".to_string());
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_array_contains() {
        let data = json!({
            "tags": ["rust", "vector", "database"]
        });

        let filter = AdvancedFilter::ArrayContains("tags".to_string(), json!("rust"));
        assert!(filter.matches(&data).unwrap());

        let filter = AdvancedFilter::ArrayContains("tags".to_string(), json!("python"));
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_array_length() {
        let data = json!({
            "tags": ["a", "b", "c"]
        });

        let filter = AdvancedFilter::ArrayLength("tags".to_string(), ">".to_string(), 2);
        assert!(filter.matches(&data).unwrap());

        let filter = AdvancedFilter::ArrayLength("tags".to_string(), "=".to_string(), 3);
        assert!(filter.matches(&data).unwrap());

        let filter = AdvancedFilter::ArrayLength("tags".to_string(), "<".to_string(), 3);
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_and_filter() {
        let data = json!({
            "age": 25,
            "name": "Alice"
        });

        let filter = AdvancedFilter::And(vec![
            AdvancedFilter::Basic("age".to_string(), ">=".to_string(), json!(18)),
            AdvancedFilter::Basic("name".to_string(), "=".to_string(), json!("Alice")),
        ]);

        assert!(filter.matches(&data).unwrap());
    }

    #[test]
    fn test_or_filter() {
        let data = json!({
            "status": "active"
        });

        let filter = AdvancedFilter::Or(vec![
            AdvancedFilter::Basic("status".to_string(), "=".to_string(), json!("active")),
            AdvancedFilter::Basic("status".to_string(), "=".to_string(), json!("pending")),
        ]);

        assert!(filter.matches(&data).unwrap());
    }

    #[test]
    fn test_not_filter() {
        let data = json!({
            "deleted": false
        });

        let filter = AdvancedFilter::Not(Box::new(AdvancedFilter::Basic(
            "deleted".to_string(),
            "=".to_string(),
            json!(true),
        )));

        assert!(filter.matches(&data).unwrap());
    }

    #[test]
    fn test_filter_builder() {
        let data = json!({
            "user": {
                "age": 25
            },
            "email": "test@example.com",
            "tags": ["premium"]
        });

        let filter = FilterBuilder::new()
            .json_path("user.age", ">=", json!(18))
            .unwrap()
            .and()
            .regex("email", r".*@example\.com$")
            .unwrap()
            .and()
            .array_contains("tags", json!("premium"))
            .unwrap()
            .build();

        assert!(filter.matches(&data).unwrap());
    }

    #[test]
    fn test_parse_regex_filter() {
        let filter = parse_advanced_filter("name ~= '^John'").unwrap();

        let data = json!({"name": "John Doe"});
        assert!(filter.matches(&data).unwrap());

        let data = json!({"name": "Jane Doe"});
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_parse_array_contains() {
        let filter = parse_advanced_filter("tags contains 'rust'").unwrap();

        let data = json!({"tags": ["rust", "programming"]});
        assert!(filter.matches(&data).unwrap());

        let data = json!({"tags": ["python", "programming"]});
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_parse_array_length() {
        let filter = parse_advanced_filter("items.length > 5").unwrap();

        let data = json!({"items": [1, 2, 3, 4, 5, 6]});
        assert!(filter.matches(&data).unwrap());

        let data = json!({"items": [1, 2, 3]});
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_parse_basic_filter() {
        let filter = parse_advanced_filter("age >= 18").unwrap();

        let data = json!({"age": 25});
        assert!(filter.matches(&data).unwrap());

        let data = json!({"age": 15});
        assert!(!filter.matches(&data).unwrap());
    }

    #[test]
    fn test_array_indexing() {
        let data = json!({
            "items": [
                {"name": "first"},
                {"name": "second"}
            ]
        });

        let result = extract_json_path(&data, "items[0].name").unwrap();
        assert_eq!(result, json!("first"));

        let result = extract_json_path(&data, "items[1].name").unwrap();
        assert_eq!(result, json!("second"));
    }
}