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
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
//! Graph-aware vector search for named graph filtering and contextual search
//!
//! This module provides graph-scoped vector search capabilities that enable:
//! - Named graph filtering for vector searches
//! - Contextual search within specific RDF graphs
//! - Hierarchical graph search patterns
//! - Cross-graph similarity analysis

use crate::VectorStore;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Graph-aware search configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphAwareConfig {
    /// Enable graph-scoped filtering
    pub enable_graph_filtering: bool,
    /// Enable hierarchical graph search
    pub enable_hierarchical_search: bool,
    /// Enable cross-graph similarity
    pub enable_cross_graph_similarity: bool,
    /// Default graph context if none specified
    pub default_graph: Option<String>,
    /// Graph hierarchy configuration
    pub graph_hierarchy: GraphHierarchy,
    /// Cache graph metadata
    pub cache_graph_metadata: bool,
}

impl Default for GraphAwareConfig {
    fn default() -> Self {
        Self {
            enable_graph_filtering: true,
            enable_hierarchical_search: false,
            enable_cross_graph_similarity: false,
            default_graph: None,
            graph_hierarchy: GraphHierarchy::default(),
            cache_graph_metadata: true,
        }
    }
}

/// Graph hierarchy for hierarchical search
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GraphHierarchy {
    /// Parent-child relationships between graphs
    pub parent_child: HashMap<String, Vec<String>>,
    /// Graph categories/types
    pub graph_types: HashMap<String, String>,
    /// Graph priority weights for search ranking
    pub graph_weights: HashMap<String, f32>,
}

/// Graph context for search operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphContext {
    /// Primary graph to search in
    pub primary_graph: String,
    /// Additional graphs to include (if hierarchical search enabled)
    pub additional_graphs: Vec<String>,
    /// Search scope configuration
    pub scope: GraphSearchScope,
    /// Context-specific weights
    pub context_weights: HashMap<String, f32>,
}

/// Graph search scope
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum GraphSearchScope {
    /// Search only in the specified graph
    Exact,
    /// Search in specified graph and its children
    IncludeChildren,
    /// Search in specified graph and its parents
    IncludeParents,
    /// Search in entire hierarchy branch
    FullHierarchy,
    /// Search across all related graphs
    Related,
}

/// Graph-aware search result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphAwareSearchResult {
    /// Resource URI
    pub resource: String,
    /// Similarity score
    pub score: f32,
    /// Graph where the resource was found
    pub source_graph: String,
    /// Context relevance score
    pub context_score: f32,
    /// Combined final score
    pub final_score: f32,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Resource graph membership information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceGraphInfo {
    /// Resource URI
    pub resource: String,
    /// Graphs containing this resource
    pub graphs: HashSet<String>,
    /// Primary graph (most relevant)
    pub primary_graph: Option<String>,
    /// Last updated timestamp
    pub last_updated: std::time::SystemTime,
}

/// Graph-aware vector search engine
pub struct GraphAwareSearch {
    config: GraphAwareConfig,
    /// Resource to graph mappings
    resource_graph_map: HashMap<String, ResourceGraphInfo>,
    /// Graph metadata cache
    graph_metadata: HashMap<String, GraphMetadata>,
    /// Graph size cache for performance optimization
    graph_sizes: HashMap<String, usize>,
}

/// Graph metadata for optimization and ranking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphMetadata {
    /// Graph URI
    pub graph_uri: String,
    /// Number of resources in this graph
    pub resource_count: usize,
    /// Average vector similarity within graph
    pub avg_internal_similarity: f32,
    /// Graph creation/update time
    pub last_modified: std::time::SystemTime,
    /// Graph type/category
    pub graph_type: Option<String>,
    /// Graph quality score
    pub quality_score: f32,
}

impl GraphAwareSearch {
    pub fn new(config: GraphAwareConfig) -> Self {
        Self {
            config,
            resource_graph_map: HashMap::new(),
            graph_metadata: HashMap::new(),
            graph_sizes: HashMap::new(),
        }
    }

    /// Register a resource as belonging to specific graphs
    pub fn register_resource_graph(&mut self, resource: String, graphs: Vec<String>) {
        let graph_set: HashSet<String> = graphs.iter().cloned().collect();
        let primary_graph = graphs.first().cloned();

        let info = ResourceGraphInfo {
            resource: resource.clone(),
            graphs: graph_set,
            primary_graph,
            last_updated: std::time::SystemTime::now(),
        };

        self.resource_graph_map.insert(resource, info);

        // Update graph sizes
        for graph in graphs {
            *self.graph_sizes.entry(graph).or_insert(0) += 1;
        }
    }

    /// Perform graph-aware vector search
    pub fn search_in_graph(
        &self,
        vector_store: &VectorStore,
        query_text: &str,
        graph_context: &GraphContext,
        limit: usize,
    ) -> Result<Vec<GraphAwareSearchResult>> {
        // Determine which graphs to search in
        let target_graphs = self.resolve_search_graphs(graph_context)?;

        // Perform vector search across all target graphs
        let mut all_results = Vec::new();

        for graph_uri in &target_graphs {
            let graph_results =
                self.search_single_graph(vector_store, query_text, graph_uri, limit * 2)?;
            all_results.extend(graph_results);
        }

        // Apply graph-aware ranking and filtering
        let ranked_results = self.rank_results_by_graph_context(all_results, graph_context)?;

        // Return top results up to limit
        Ok(ranked_results.into_iter().take(limit).collect())
    }

    /// Search within a specific named graph
    pub fn search_single_graph(
        &self,
        vector_store: &VectorStore,
        query_text: &str,
        graph_uri: &str,
        limit: usize,
    ) -> Result<Vec<GraphAwareSearchResult>> {
        // Get all potential results from vector store
        let vector_results = vector_store.similarity_search(query_text, limit * 3)?; // Get more candidates

        let mut graph_results = Vec::new();

        for (resource, score) in vector_results {
            // Check if resource belongs to the target graph
            if let Some(resource_info) = self.resource_graph_map.get(&resource) {
                if resource_info.graphs.contains(graph_uri) {
                    let context_score = self.calculate_context_score(&resource, graph_uri)?;
                    let final_score = self.combine_scores(score, context_score, graph_uri);

                    graph_results.push(GraphAwareSearchResult {
                        resource,
                        score,
                        source_graph: graph_uri.to_string(),
                        context_score,
                        final_score,
                        metadata: HashMap::new(),
                    });
                }
            }
        }

        // Sort by final score
        graph_results.sort_by(|a, b| {
            b.final_score
                .partial_cmp(&a.final_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Ok(graph_results.into_iter().take(limit).collect())
    }

    /// Resolve which graphs to search based on context and hierarchy
    fn resolve_search_graphs(&self, context: &GraphContext) -> Result<Vec<String>> {
        let mut target_graphs = vec![context.primary_graph.clone()];

        match context.scope {
            GraphSearchScope::Exact => {
                // Only search in the primary graph
            }
            GraphSearchScope::IncludeChildren => {
                // Add child graphs if hierarchy is configured
                if let Some(children) = self
                    .config
                    .graph_hierarchy
                    .parent_child
                    .get(&context.primary_graph)
                {
                    target_graphs.extend(children.clone());
                }
            }
            GraphSearchScope::IncludeParents => {
                // Find parent graphs
                for (parent, children) in &self.config.graph_hierarchy.parent_child {
                    if children.contains(&context.primary_graph) {
                        target_graphs.push(parent.clone());
                    }
                }
            }
            GraphSearchScope::FullHierarchy => {
                // Include all graphs in the hierarchy branch
                target_graphs.extend(self.get_hierarchy_branch(&context.primary_graph));
            }
            GraphSearchScope::Related => {
                // Include additional graphs specified in context
                target_graphs.extend(context.additional_graphs.clone());
            }
        }

        // Add additional graphs from context
        target_graphs.extend(context.additional_graphs.clone());

        // Remove duplicates and return
        target_graphs.sort();
        target_graphs.dedup();
        Ok(target_graphs)
    }

    /// Get all graphs in a hierarchy branch
    fn get_hierarchy_branch(&self, graph_uri: &str) -> Vec<String> {
        let mut branch_graphs = Vec::new();

        // Add children recursively
        self.add_children_recursive(graph_uri, &mut branch_graphs);

        // Add parents recursively
        self.add_parents_recursive(graph_uri, &mut branch_graphs);

        branch_graphs
    }

    /// Recursively add child graphs
    fn add_children_recursive(&self, graph_uri: &str, result: &mut Vec<String>) {
        if let Some(children) = self.config.graph_hierarchy.parent_child.get(graph_uri) {
            for child in children {
                if !result.contains(child) {
                    result.push(child.clone());
                    self.add_children_recursive(child, result);
                }
            }
        }
    }

    /// Recursively add parent graphs
    fn add_parents_recursive(&self, graph_uri: &str, result: &mut Vec<String>) {
        for (parent, children) in &self.config.graph_hierarchy.parent_child {
            if children.contains(&graph_uri.to_string()) && !result.contains(parent) {
                result.push(parent.clone());
                self.add_parents_recursive(parent, result);
            }
        }
    }

    /// Calculate context relevance score for a resource in a graph
    fn calculate_context_score(&self, resource: &str, graph_uri: &str) -> Result<f32> {
        let mut context_score = 1.0;

        // Apply graph weight if configured
        if let Some(&weight) = self.config.graph_hierarchy.graph_weights.get(graph_uri) {
            context_score *= weight;
        }

        // Consider graph metadata quality
        if let Some(metadata) = self.graph_metadata.get(graph_uri) {
            context_score *= metadata.quality_score;
        }

        // Check if this is the primary graph for the resource
        if let Some(resource_info) = self.resource_graph_map.get(resource) {
            if resource_info.primary_graph.as_ref() == Some(&graph_uri.to_string()) {
                context_score *= 1.2; // Boost for primary graph
            }
        }

        Ok(context_score.min(1.0)) // Cap at 1.0
    }

    /// Combine vector similarity score with context score
    fn combine_scores(&self, similarity_score: f32, context_score: f32, graph_uri: &str) -> f32 {
        // Weighted combination of similarity and context scores
        let similarity_weight = 0.7;
        let context_weight = 0.3;

        // Apply graph-specific boosting
        let graph_boost = self
            .config
            .graph_hierarchy
            .graph_weights
            .get(graph_uri)
            .unwrap_or(&1.0);

        (similarity_score * similarity_weight + context_score * context_weight) * graph_boost
    }

    /// Rank results considering graph context and hierarchy
    fn rank_results_by_graph_context(
        &self,
        mut results: Vec<GraphAwareSearchResult>,
        context: &GraphContext,
    ) -> Result<Vec<GraphAwareSearchResult>> {
        // Apply context-specific weights
        for result in &mut results {
            if let Some(&weight) = context.context_weights.get(&result.source_graph) {
                result.final_score *= weight;
            }

            // Boost results from primary graph
            if result.source_graph == context.primary_graph {
                result.final_score *= 1.1;
            }
        }

        // Sort by final score (descending)
        results.sort_by(|a, b| {
            b.final_score
                .partial_cmp(&a.final_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Apply diversity filtering if enabled (ensure results from different graphs)
        if self.config.enable_cross_graph_similarity {
            results = self.apply_diversity_filtering(results);
        }

        Ok(results)
    }

    /// Apply diversity filtering to ensure results from multiple graphs
    fn apply_diversity_filtering(
        &self,
        results: Vec<GraphAwareSearchResult>,
    ) -> Vec<GraphAwareSearchResult> {
        let mut filtered_results = Vec::new();
        let mut graph_counts: HashMap<String, usize> = HashMap::new();
        let max_per_graph = 3; // Maximum results per graph

        for result in results {
            let count = graph_counts.entry(result.source_graph.clone()).or_insert(0);
            if *count < max_per_graph {
                filtered_results.push(result);
                *count += 1;
            }
        }

        filtered_results
    }

    /// Update graph metadata for optimization
    pub fn update_graph_metadata(&mut self, graph_uri: String, metadata: GraphMetadata) {
        self.graph_metadata.insert(graph_uri, metadata);
    }

    /// Get graph statistics
    pub fn get_graph_stats(&self, graph_uri: &str) -> Option<(usize, Option<&GraphMetadata>)> {
        let size = self.graph_sizes.get(graph_uri).cloned();
        let metadata = self.graph_metadata.get(graph_uri);
        size.map(|s| (s, metadata))
    }

    /// Clear graph caches
    pub fn clear_caches(&mut self) {
        self.resource_graph_map.clear();
        self.graph_metadata.clear();
        self.graph_sizes.clear();
    }

    /// Check if a resource exists in a specific graph
    pub fn resource_in_graph(&self, resource: &str, graph_uri: &str) -> bool {
        self.resource_graph_map
            .get(resource)
            .map(|info| info.graphs.contains(graph_uri))
            .unwrap_or(false)
    }

    /// Get all graphs containing a resource
    pub fn get_resource_graphs(&self, resource: &str) -> Option<&HashSet<String>> {
        self.resource_graph_map
            .get(resource)
            .map(|info| &info.graphs)
    }

    /// Calculate cross-graph similarity
    pub fn cross_graph_similarity(
        &self,
        vector_store: &VectorStore,
        resource1: &str,
        graph1: &str,
        resource2: &str,
        graph2: &str,
    ) -> Result<f32> {
        if !self.config.enable_cross_graph_similarity {
            return Err(anyhow!("Cross-graph similarity is disabled"));
        }

        // Verify resources exist in specified graphs
        if !self.resource_in_graph(resource1, graph1) || !self.resource_in_graph(resource2, graph2)
        {
            return Err(anyhow!("Resources not found in specified graphs"));
        }

        // Calculate base similarity
        let base_similarity = vector_store.calculate_similarity(resource1, resource2)?;

        // Apply cross-graph penalty/boost based on graph relationship
        let graph_relationship_factor = self.calculate_graph_relationship_factor(graph1, graph2);

        Ok(base_similarity * graph_relationship_factor)
    }

    /// Calculate relationship factor between two graphs
    fn calculate_graph_relationship_factor(&self, graph1: &str, graph2: &str) -> f32 {
        if graph1 == graph2 {
            return 1.0; // Same graph, no adjustment
        }

        // Check if graphs are in parent-child relationship
        if let Some(children) = self.config.graph_hierarchy.parent_child.get(graph1) {
            if children.contains(&graph2.to_string()) {
                return 0.9; // Parent-child relationship, slight boost
            }
        }

        if let Some(children) = self.config.graph_hierarchy.parent_child.get(graph2) {
            if children.contains(&graph1.to_string()) {
                return 0.9; // Child-parent relationship, slight boost
            }
        }

        // Check if graphs are of the same type
        if let (Some(type1), Some(type2)) = (
            self.config.graph_hierarchy.graph_types.get(graph1),
            self.config.graph_hierarchy.graph_types.get(graph2),
        ) {
            if type1 == type2 {
                return 0.8; // Same type, moderate boost
            }
        }

        0.7 // Different, unrelated graphs - apply penalty
    }

    /// Set graph hierarchy configuration
    pub fn set_graph_hierarchy(&mut self, parent_child: HashMap<String, Vec<String>>) {
        self.config.graph_hierarchy.parent_child = parent_child;
    }

    /// Set graph weights for ranking
    pub fn set_graph_weights(&mut self, weights: HashMap<String, f32>) {
        self.config.graph_hierarchy.graph_weights = weights;
    }
}

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

    #[test]
    fn test_graph_context_creation() {
        let context = GraphContext {
            primary_graph: "http://example.org/graph1".to_string(),
            additional_graphs: vec!["http://example.org/graph2".to_string()],
            scope: GraphSearchScope::IncludeChildren,
            context_weights: HashMap::new(),
        };

        assert_eq!(context.primary_graph, "http://example.org/graph1");
        assert_eq!(context.scope, GraphSearchScope::IncludeChildren);
    }

    #[test]
    fn test_resource_graph_registration() {
        let mut search = GraphAwareSearch::new(GraphAwareConfig::default());

        search.register_resource_graph(
            "http://example.org/resource1".to_string(),
            vec!["http://example.org/graph1".to_string()],
        );

        assert!(
            search.resource_in_graph("http://example.org/resource1", "http://example.org/graph1")
        );
        assert!(
            !search.resource_in_graph("http://example.org/resource1", "http://example.org/graph2")
        );
    }

    #[test]
    fn test_graph_hierarchy() {
        let mut config = GraphAwareConfig::default();
        config.graph_hierarchy.parent_child.insert(
            "http://example.org/parent".to_string(),
            vec![
                "http://example.org/child1".to_string(),
                "http://example.org/child2".to_string(),
            ],
        );

        let search = GraphAwareSearch::new(config);
        let branch = search.get_hierarchy_branch("http://example.org/parent");

        assert!(branch.contains(&"http://example.org/child1".to_string()));
        assert!(branch.contains(&"http://example.org/child2".to_string()));
    }

    #[test]
    fn test_graph_search_scope() -> Result<()> {
        let context = GraphContext {
            primary_graph: "http://example.org/main".to_string(),
            additional_graphs: vec![],
            scope: GraphSearchScope::Exact,
            context_weights: HashMap::new(),
        };

        let search = GraphAwareSearch::new(GraphAwareConfig::default());
        let graphs = search.resolve_search_graphs(&context)?;

        assert_eq!(graphs.len(), 1);
        assert_eq!(graphs[0], "http://example.org/main");
        Ok(())
    }
}