oxify_vector/
types.rs

1//! Vector search types and configuration
2//!
3//! Ported from OxiRS (<https://github.com/cool-japan/oxirs>)
4//! Original implementation: Copyright (c) OxiRS Contributors
5//! Adapted for OxiFY (simplified for LLM workflow focus)
6//! License: MIT OR Apache-2.0 (compatible with OxiRS)
7
8use serde::{Deserialize, Serialize};
9
10/// Distance metric for vector search
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12pub enum DistanceMetric {
13    /// Cosine similarity (normalized dot product)
14    Cosine,
15    /// Euclidean distance (L2 norm)
16    Euclidean,
17    /// Dot product similarity
18    DotProduct,
19    /// Manhattan distance (L1 norm)
20    Manhattan,
21}
22
23/// Vector search configuration
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct SearchConfig {
26    /// Distance metric to use
27    pub metric: DistanceMetric,
28    /// Enable parallel search
29    pub parallel: bool,
30    /// Normalize vectors before search
31    pub normalize: bool,
32}
33
34impl Default for SearchConfig {
35    fn default() -> Self {
36        Self {
37            metric: DistanceMetric::Cosine,
38            parallel: true,
39            normalize: true,
40        }
41    }
42}
43
44/// Search result
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct SearchResult {
47    /// Entity ID
48    pub entity_id: String,
49    /// Similarity score (higher is better)
50    pub score: f32,
51    /// Distance (lower is better, depends on metric)
52    pub distance: f32,
53    /// Rank in results (1-indexed)
54    pub rank: usize,
55}
56
57/// Index statistics
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct IndexStats {
60    /// Number of entities in index
61    pub num_entities: usize,
62    /// Embedding dimensions
63    pub dimensions: usize,
64    /// Whether index is built
65    pub is_built: bool,
66    /// Distance metric
67    pub metric: DistanceMetric,
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_distance_metric() {
76        let metric = DistanceMetric::Cosine;
77        assert_eq!(metric, DistanceMetric::Cosine);
78    }
79
80    #[test]
81    fn test_search_config_default() {
82        let config = SearchConfig::default();
83        assert_eq!(config.metric, DistanceMetric::Cosine);
84        assert!(config.parallel);
85        assert!(config.normalize);
86    }
87}