velesdb_core/
point.rs

1//! Point data structure representing a vector with metadata.
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value as JsonValue;
5
6/// A point in the vector database.
7///
8/// A point consists of:
9/// - A unique identifier
10/// - A vector (embedding)
11/// - Optional payload (metadata)
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Point {
14    /// Unique identifier for the point.
15    pub id: u64,
16
17    /// The vector embedding.
18    pub vector: Vec<f32>,
19
20    /// Optional JSON payload containing metadata.
21    #[serde(default)]
22    pub payload: Option<JsonValue>,
23}
24
25impl Point {
26    /// Creates a new point with the given ID, vector, and optional payload.
27    ///
28    /// # Arguments
29    ///
30    /// * `id` - Unique identifier
31    /// * `vector` - Vector embedding
32    /// * `payload` - Optional metadata
33    #[must_use]
34    pub fn new(id: u64, vector: Vec<f32>, payload: Option<JsonValue>) -> Self {
35        Self {
36            id,
37            vector,
38            payload,
39        }
40    }
41
42    /// Creates a new point without payload.
43    #[must_use]
44    pub fn without_payload(id: u64, vector: Vec<f32>) -> Self {
45        Self::new(id, vector, None)
46    }
47
48    /// Creates a metadata-only point (no vector, only payload).
49    ///
50    /// Used for metadata-only collections that don't store vectors.
51    ///
52    /// # Arguments
53    ///
54    /// * `id` - Unique identifier
55    /// * `payload` - Metadata (JSON value)
56    #[must_use]
57    pub fn metadata_only(id: u64, payload: JsonValue) -> Self {
58        Self {
59            id,
60            vector: Vec::new(), // Empty vector
61            payload: Some(payload),
62        }
63    }
64
65    /// Returns the dimension of the vector.
66    #[must_use]
67    pub fn dimension(&self) -> usize {
68        self.vector.len()
69    }
70
71    /// Returns true if this point has no vector (metadata-only).
72    #[must_use]
73    pub fn is_metadata_only(&self) -> bool {
74        self.vector.is_empty()
75    }
76}
77
78/// A search result containing a point and its similarity score.
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct SearchResult {
81    /// The matching point.
82    pub point: Point,
83
84    /// Similarity score (interpretation depends on the distance metric).
85    pub score: f32,
86}
87
88impl SearchResult {
89    /// Creates a new search result.
90    #[must_use]
91    pub const fn new(point: Point, score: f32) -> Self {
92        Self { point, score }
93    }
94}