oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
//! SPARQL function bindings for Tantivy text search
//!
//! This module provides SPARQL functions that integrate Tantivy full-text search
//! capabilities into SPARQL queries.
//!
//! ## Available Functions
//!
//! - `vec:text_search(?lit, "query", limit, threshold)` - Basic text search with BM25
//! - `vec:phrase_search(?lit, "exact phrase", limit)` - Exact phrase matching
//! - `vec:fuzzy_search(?lit, "misspeled", distance, limit)` - Fuzzy matching with edit distance
//! - `vec:field_search(?lit, "title:AI description:neural", limit)` - Field-specific search
//!
//! ## Example SPARQL Query
//!
//! ```sparql
//! PREFIX vec: <http://oxirs.org/vec#>
//! PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
//!
//! SELECT ?doc ?label WHERE {
//!   ?doc rdfs:label ?label .
//!   FILTER(vec:text_search(?label, "machine learning", 10, 0.7))
//! }
//! ```

#[cfg(feature = "tantivy-search")]
use super::super::hybrid_search::tantivy_searcher::{RdfDocument, SearchResult, TantivySearcher};

use anyhow::{anyhow, Context, Result};
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;

/// SPARQL text search function executor
#[cfg(feature = "tantivy-search")]
pub struct SparqlTextFunctions {
    searcher: Arc<RwLock<TantivySearcher>>,
}

#[cfg(feature = "tantivy-search")]
impl SparqlTextFunctions {
    /// Create a new SPARQL text functions executor
    pub fn new(searcher: TantivySearcher) -> Self {
        Self {
            searcher: Arc::new(RwLock::new(searcher)),
        }
    }

    /// Execute vec:text_search function
    ///
    /// Arguments:
    /// - literal: The RDF literal to search in
    /// - query: Text query string
    /// - limit: Maximum number of results (default: 10)
    /// - threshold: Minimum score threshold (default: 0.0)
    pub fn text_search(
        &self,
        _literal: &str,
        query: &str,
        limit: Option<usize>,
        threshold: Option<f32>,
    ) -> Result<Vec<SparqlSearchResult>> {
        let searcher = self.searcher.read();
        let limit = limit.unwrap_or(10);
        let threshold = threshold.unwrap_or(0.0);

        let results = searcher
            .text_search(query, limit, threshold)
            .context("Failed to execute text search")?;

        Ok(results.into_iter().map(SparqlSearchResult::from).collect())
    }

    /// Execute vec:phrase_search function
    ///
    /// Arguments:
    /// - literal: The RDF literal to search in
    /// - phrase: Exact phrase to match
    /// - limit: Maximum number of results (default: 10)
    pub fn phrase_search(
        &self,
        _literal: &str,
        phrase: &str,
        limit: Option<usize>,
    ) -> Result<Vec<SparqlSearchResult>> {
        let searcher = self.searcher.read();
        let limit = limit.unwrap_or(10);

        let results = searcher
            .phrase_search(phrase, limit)
            .context("Failed to execute phrase search")?;

        Ok(results.into_iter().map(SparqlSearchResult::from).collect())
    }

    /// Execute vec:fuzzy_search function
    ///
    /// Arguments:
    /// - literal: The RDF literal to search in
    /// - query: Query string (possibly misspelled)
    /// - distance: Maximum edit distance (default: 2)
    /// - limit: Maximum number of results (default: 10)
    pub fn fuzzy_search(
        &self,
        _literal: &str,
        query: &str,
        distance: Option<u8>,
        limit: Option<usize>,
    ) -> Result<Vec<SparqlSearchResult>> {
        let searcher = self.searcher.read();
        let distance = distance.unwrap_or(2);
        let limit = limit.unwrap_or(10);

        let results = searcher
            .fuzzy_search(query, distance, limit)
            .context("Failed to execute fuzzy search")?;

        Ok(results.into_iter().map(SparqlSearchResult::from).collect())
    }

    /// Execute vec:field_search function
    ///
    /// Arguments:
    /// - literal: The RDF literal to search in
    /// - field_query: Field-specific query string (e.g., "title:AI description:neural")
    /// - limit: Maximum number of results (default: 10)
    pub fn field_search(
        &self,
        _literal: &str,
        field_query: &str,
        limit: Option<usize>,
    ) -> Result<Vec<SparqlSearchResult>> {
        let searcher = self.searcher.read();
        let limit = limit.unwrap_or(10);

        // Parse field query string
        let field_queries = Self::parse_field_query(field_query)?;

        let results = searcher
            .field_search(&field_queries, limit)
            .context("Failed to execute field search")?;

        Ok(results.into_iter().map(SparqlSearchResult::from).collect())
    }

    /// Parse field query string into field-value map
    ///
    /// Example: "title:AI description:neural" -> {"title": "AI", "description": "neural"}
    fn parse_field_query(query: &str) -> Result<HashMap<String, String>> {
        let mut field_queries = HashMap::new();

        // Split by whitespace, then by colon
        let parts: Vec<&str> = query.split_whitespace().collect();

        for part in parts {
            if let Some(colon_pos) = part.find(':') {
                let field = part[..colon_pos].to_string();
                let value = part[colon_pos + 1..].to_string();

                if !field.is_empty() && !value.is_empty() {
                    field_queries.insert(field, value);
                }
            }
        }

        if field_queries.is_empty() {
            return Err(anyhow!("Invalid field query format"));
        }

        Ok(field_queries)
    }

    /// Index RDF literals for searching
    pub fn index_literals(&mut self, literals: Vec<RdfLiteral>) -> Result<()> {
        let mut searcher = self.searcher.write();

        let docs: Vec<RdfDocument> = literals
            .into_iter()
            .map(|lit| RdfDocument {
                uri: lit.subject_uri,
                content: lit.value,
                language: lit.language,
                datatype: lit.datatype,
            })
            .collect();

        searcher
            .index_documents(&docs)
            .context("Failed to index RDF literals")?;

        Ok(())
    }

    /// Get search statistics
    pub fn get_stats(&self) -> SearchStats {
        let searcher = self.searcher.read();
        let index_stats = searcher.get_stats();

        SearchStats {
            total_indexed: index_stats.total_documents,
            heap_size_mb: index_stats.heap_size_mb,
        }
    }
}

#[cfg(not(feature = "tantivy-search"))]
/// Stub implementation when tantivy-search feature is disabled
pub struct SparqlTextFunctions;

#[cfg(not(feature = "tantivy-search"))]
impl SparqlTextFunctions {
    pub fn new(_searcher: ()) -> Self {
        Self
    }

    pub fn text_search(
        &self,
        _literal: &str,
        _query: &str,
        _limit: Option<usize>,
        _threshold: Option<f32>,
    ) -> Result<Vec<SparqlSearchResult>> {
        Err(anyhow!("Tantivy search feature is not enabled"))
    }

    pub fn phrase_search(
        &self,
        _literal: &str,
        _phrase: &str,
        _limit: Option<usize>,
    ) -> Result<Vec<SparqlSearchResult>> {
        Err(anyhow!("Tantivy search feature is not enabled"))
    }

    pub fn fuzzy_search(
        &self,
        _literal: &str,
        _query: &str,
        _distance: Option<u8>,
        _limit: Option<usize>,
    ) -> Result<Vec<SparqlSearchResult>> {
        Err(anyhow!("Tantivy search feature is not enabled"))
    }

    pub fn field_search(
        &self,
        _literal: &str,
        _field_query: &str,
        _limit: Option<usize>,
    ) -> Result<Vec<SparqlSearchResult>> {
        Err(anyhow!("Tantivy search feature is not enabled"))
    }
}

/// RDF literal for indexing
#[derive(Debug, Clone)]
pub struct RdfLiteral {
    /// Subject URI that has this literal
    pub subject_uri: String,
    /// Literal value
    pub value: String,
    /// Language tag (@en, @de, etc.)
    pub language: Option<String>,
    /// Datatype IRI (xsd:string, etc.)
    pub datatype: Option<String>,
}

/// Search result for SPARQL queries
#[derive(Debug, Clone)]
pub struct SparqlSearchResult {
    /// Resource URI
    pub uri: String,
    /// Relevance score (0.0 to 1.0)
    pub score: f32,
    /// Matched snippet
    pub snippet: Option<String>,
    /// Language tag
    pub language: Option<String>,
}

#[cfg(feature = "tantivy-search")]
impl From<SearchResult> for SparqlSearchResult {
    fn from(result: SearchResult) -> Self {
        Self {
            uri: result.uri,
            score: result.score,
            snippet: result.snippet,
            language: result.language,
        }
    }
}

/// Search statistics
#[derive(Debug, Clone)]
pub struct SearchStats {
    /// Total documents indexed
    pub total_indexed: u64,
    /// Heap size in MB
    pub heap_size_mb: usize,
}

/// Type alias for the SPARQL text function registry
#[cfg(feature = "tantivy-search")]
pub type TextFunctionRegistry = HashMap<String, Box<dyn Fn(&[String]) -> Result<String>>>;

/// Helper function to register text search functions in SPARQL engine
///
/// This should be called during SPARQL engine initialization to make
/// text search functions available in queries.
#[cfg(feature = "tantivy-search")]
pub fn register_text_functions(
    function_registry: &mut TextFunctionRegistry,
    text_functions: Arc<SparqlTextFunctions>,
) {
    // Register vec:text_search
    let text_funcs = text_functions.clone();
    function_registry.insert(
        "http://oxirs.org/vec#text_search".to_string(),
        Box::new(move |args: &[String]| -> Result<String> {
            if args.len() < 2 {
                return Err(anyhow!("text_search requires at least 2 arguments"));
            }

            let literal = &args[0];
            let query = &args[1];
            let limit = args.get(2).and_then(|s| s.parse().ok());
            let threshold = args.get(3).and_then(|s| s.parse().ok());

            let results = text_funcs.text_search(literal, query, limit, threshold)?;

            // Convert results to SPARQL result format
            let uris: Vec<String> = results.iter().map(|r| r.uri.clone()).collect();
            Ok(uris.join(" "))
        }),
    );

    // Register vec:phrase_search
    let text_funcs = text_functions.clone();
    function_registry.insert(
        "http://oxirs.org/vec#phrase_search".to_string(),
        Box::new(move |args: &[String]| -> Result<String> {
            if args.len() < 2 {
                return Err(anyhow!("phrase_search requires at least 2 arguments"));
            }

            let literal = &args[0];
            let phrase = &args[1];
            let limit = args.get(2).and_then(|s| s.parse().ok());

            let results = text_funcs.phrase_search(literal, phrase, limit)?;

            let uris: Vec<String> = results.iter().map(|r| r.uri.clone()).collect();
            Ok(uris.join(" "))
        }),
    );

    // Register vec:fuzzy_search
    let text_funcs = text_functions.clone();
    function_registry.insert(
        "http://oxirs.org/vec#fuzzy_search".to_string(),
        Box::new(move |args: &[String]| -> Result<String> {
            if args.len() < 2 {
                return Err(anyhow!("fuzzy_search requires at least 2 arguments"));
            }

            let literal = &args[0];
            let query = &args[1];
            let distance = args.get(2).and_then(|s| s.parse().ok());
            let limit = args.get(3).and_then(|s| s.parse().ok());

            let results = text_funcs.fuzzy_search(literal, query, distance, limit)?;

            let uris: Vec<String> = results.iter().map(|r| r.uri.clone()).collect();
            Ok(uris.join(" "))
        }),
    );

    // Register vec:field_search
    let text_funcs = text_functions;
    function_registry.insert(
        "http://oxirs.org/vec#field_search".to_string(),
        Box::new(move |args: &[String]| -> Result<String> {
            if args.len() < 2 {
                return Err(anyhow!("field_search requires at least 2 arguments"));
            }

            let literal = &args[0];
            let field_query = &args[1];
            let limit = args.get(2).and_then(|s| s.parse().ok());

            let results = text_funcs.field_search(literal, field_query, limit)?;

            let uris: Vec<String> = results.iter().map(|r| r.uri.clone()).collect();
            Ok(uris.join(" "))
        }),
    );
}

#[cfg(test)]
#[cfg(feature = "tantivy-search")]
mod tests {
    use super::*;
    use crate::hybrid_search::tantivy_searcher::{TantivyConfig, TantivySearcher};
    use std::env;

    fn create_test_searcher() -> TantivySearcher {
        let temp_dir =
            env::temp_dir().join(format!("tantivy_sparql_test_{}", uuid::Uuid::new_v4()));
        let config = TantivyConfig {
            index_path: temp_dir,
            heap_size_mb: 50,
            stemming: true,
            stopwords: true,
            fuzzy_distance: 2,
        };

        TantivySearcher::new(config).expect("Failed to create searcher")
    }

    #[test]
    fn test_sparql_text_search() -> Result<()> {
        let searcher = create_test_searcher();
        let mut text_funcs = SparqlTextFunctions::new(searcher);

        // Index some test literals
        let literals = vec![
            RdfLiteral {
                subject_uri: "http://example.org/paper1".to_string(),
                value: "Deep learning for natural language processing".to_string(),
                language: Some("en".to_string()),
                datatype: Some("xsd:string".to_string()),
            },
            RdfLiteral {
                subject_uri: "http://example.org/paper2".to_string(),
                value: "Machine learning algorithms and applications".to_string(),
                language: Some("en".to_string()),
                datatype: Some("xsd:string".to_string()),
            },
        ];

        text_funcs.index_literals(literals)?;

        // Test text search
        let results = text_funcs.text_search("", "learning", Some(10), Some(0.0))?;
        assert!(!results.is_empty(), "Should find matching documents");

        Ok(())
    }

    #[test]
    fn test_sparql_phrase_search() -> Result<()> {
        let searcher = create_test_searcher();
        let mut text_funcs = SparqlTextFunctions::new(searcher);

        let literals = vec![RdfLiteral {
            subject_uri: "http://example.org/article1".to_string(),
            value: "The quick brown fox jumps over the lazy dog".to_string(),
            language: Some("en".to_string()),
            datatype: None,
        }];

        text_funcs.index_literals(literals)?;

        // Test phrase search
        let results = text_funcs.phrase_search("", "brown fox", Some(10))?;
        assert_eq!(results.len(), 1, "Should find exact phrase");
        assert_eq!(results[0].uri, "http://example.org/article1");

        Ok(())
    }

    #[test]
    fn test_sparql_fuzzy_search() -> Result<()> {
        let searcher = create_test_searcher();
        let mut text_funcs = SparqlTextFunctions::new(searcher);

        let literals = vec![RdfLiteral {
            subject_uri: "http://example.org/doc1".to_string(),
            value: "Information retrieval systems".to_string(),
            language: Some("en".to_string()),
            datatype: None,
        }];

        text_funcs.index_literals(literals)?;

        // Test fuzzy search with misspelling
        let results = text_funcs.fuzzy_search("", "retreival", Some(2), Some(10))?;
        assert!(
            !results.is_empty(),
            "Should find fuzzy match for misspelling"
        );

        Ok(())
    }

    #[test]
    fn test_field_query_parsing() -> Result<()> {
        let query = "title:AI description:neural author:Smith";
        let fields = SparqlTextFunctions::parse_field_query(query)?;

        assert_eq!(fields.len(), 3);
        assert_eq!(fields.get("title"), Some(&"AI".to_string()));
        assert_eq!(fields.get("description"), Some(&"neural".to_string()));
        assert_eq!(fields.get("author"), Some(&"Smith".to_string()));

        Ok(())
    }

    #[test]
    fn test_search_stats() -> Result<()> {
        let searcher = create_test_searcher();
        let mut text_funcs = SparqlTextFunctions::new(searcher);

        let literals = vec![RdfLiteral {
            subject_uri: "http://example.org/test".to_string(),
            value: "Test document".to_string(),
            language: None,
            datatype: None,
        }];

        text_funcs.index_literals(literals)?;

        let stats = text_funcs.get_stats();
        assert_eq!(stats.total_indexed, 1);
        assert_eq!(stats.heap_size_mb, 50);

        Ok(())
    }
}