1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub enum PointId {
9 Uuid(String),
11 Num(u64),
13}
14
15impl From<&str> for PointId {
16 fn from(s: &str) -> Self {
17 PointId::Uuid(s.to_string())
18 }
19}
20
21impl From<String> for PointId {
22 fn from(s: String) -> Self {
23 PointId::Uuid(s)
24 }
25}
26
27impl From<u64> for PointId {
28 fn from(n: u64) -> Self {
29 PointId::Num(n)
30 }
31}
32
33#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
35#[serde(untagged)]
36pub enum PayloadValue {
37 String(String),
39 Integer(i64),
41 Float(f64),
43 Bool(bool),
45 List(Vec<PayloadValue>),
47 Object(HashMap<String, PayloadValue>),
49 Null,
51}
52
53pub type Payload = HashMap<String, PayloadValue>;
55
56#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct Point {
59 pub id: PointId,
61 pub vector: Vec<f32>,
63 #[serde(default)]
65 pub payload: Payload,
66}
67
68impl Point {
69 pub fn new(id: impl Into<PointId>, vector: Vec<f32>) -> Self {
71 Self {
72 id: id.into(),
73 vector,
74 payload: HashMap::new(),
75 }
76 }
77
78 pub fn new_num(id: u64, vector: Vec<f32>) -> Self {
80 Self {
81 id: PointId::Num(id),
82 vector,
83 payload: HashMap::new(),
84 }
85 }
86
87 pub fn with_payload(mut self, key: impl Into<String>, value: impl Into<PayloadValue>) -> Self {
89 self.payload.insert(key.into(), value.into());
90 self
91 }
92}
93
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
96pub struct SparseVector {
97 pub indices: Vec<u32>,
99 pub values: Vec<f32>,
101}
102
103impl SparseVector {
104 pub fn new(indices: Vec<u32>, values: Vec<f32>) -> Self {
106 Self { indices, values }
107 }
108
109 pub fn from_dense(dense: &[f32], threshold: f32) -> Self {
111 let mut indices = Vec::new();
112 let mut values = Vec::new();
113 for (i, &v) in dense.iter().enumerate() {
114 if v.abs() > threshold {
115 indices.push(i as u32);
116 values.push(v);
117 }
118 }
119 Self { indices, values }
120 }
121}
122
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
125#[serde(untagged)]
126pub enum VectorData {
127 Single(Vec<f32>),
129 Named(HashMap<String, Vec<f32>>),
131 Sparse {
133 indices: Vec<u32>,
135 values: Vec<f32>,
137 },
138}
139
140#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142pub struct MultiVectorPoint {
143 pub id: PointId,
145 pub vectors: HashMap<String, Vec<f32>>,
147 #[serde(default)]
149 pub payload: Payload,
150}
151
152impl MultiVectorPoint {
153 pub fn new(id: impl Into<PointId>) -> Self {
155 Self {
156 id: id.into(),
157 vectors: HashMap::new(),
158 payload: HashMap::new(),
159 }
160 }
161
162 pub fn with_vector(mut self, name: impl Into<String>, vector: Vec<f32>) -> Self {
164 self.vectors.insert(name.into(), vector);
165 self
166 }
167
168 pub fn with_payload(mut self, key: impl Into<String>, value: impl Into<PayloadValue>) -> Self {
170 self.payload.insert(key.into(), value.into());
171 self
172 }
173}
174
175#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
177pub struct ScoredPoint {
178 pub id: PointId,
180 pub score: f32,
182 #[serde(default)]
184 pub payload: Payload,
185 #[serde(default)]
187 pub vector: Option<Vec<f32>>,
188}
189
190impl From<String> for PayloadValue {
192 fn from(s: String) -> Self {
193 PayloadValue::String(s)
194 }
195}
196
197impl From<&str> for PayloadValue {
198 fn from(s: &str) -> Self {
199 PayloadValue::String(s.to_string())
200 }
201}
202
203impl From<i64> for PayloadValue {
204 fn from(n: i64) -> Self {
205 PayloadValue::Integer(n)
206 }
207}
208
209impl From<f64> for PayloadValue {
210 fn from(n: f64) -> Self {
211 PayloadValue::Float(n)
212 }
213}
214
215impl From<bool> for PayloadValue {
216 fn from(b: bool) -> Self {
217 PayloadValue::Bool(b)
218 }
219}