qdrant_client/qdrant_client/builders/
vectors.rs

1use crate::qdrant::{
2    vector, DenseVectorBuilder, MultiDenseVector, NamedVectors, SparseVectorBuilder, Vector,
3};
4use crate::QdrantError;
5
6impl Vector {
7    #[inline]
8    pub fn new(values: Vec<f32>) -> Self {
9        Self::new_dense(values)
10    }
11
12    #[inline]
13    pub fn new_dense(values: impl Into<Vec<f32>>) -> Self {
14        DenseVectorBuilder::new(values.into()).build().into()
15    }
16
17    #[inline]
18    pub fn new_sparse(indices: impl Into<Vec<u32>>, values: impl Into<Vec<f32>>) -> Self {
19        SparseVectorBuilder::new(indices, values).build().into()
20    }
21
22    #[inline]
23    pub fn new_multi(vectors: impl Into<Vec<Vec<f32>>>) -> Self {
24        MultiDenseVector::from(vectors.into()).into()
25    }
26
27    #[expect(deprecated)]
28    pub fn try_into_dense(self) -> Result<Vec<f32>, QdrantError> {
29        let Vector {
30            data,
31            indices,
32            vectors_count,
33            vector,
34        } = self;
35
36        if let Some(v) = vector {
37            return match v {
38                vector::Vector::Dense(dense) => Ok(dense.data),
39                vector::Vector::Sparse(_) => Err(QdrantError::ConversionError(
40                    "Cannot convert sparse vector to dense".to_string(),
41                )),
42                vector::Vector::MultiDense(_) => Err(QdrantError::ConversionError(
43                    "Cannot convert multi-vector to dense".to_string(),
44                )),
45                vector::Vector::Document(_) => Err(QdrantError::ConversionError(
46                    "Cannot convert document vector to dense".to_string(),
47                )),
48                vector::Vector::Image(_) => Err(QdrantError::ConversionError(
49                    "Cannot convert image vector to dense".to_string(),
50                )),
51                vector::Vector::Object(_) => Err(QdrantError::ConversionError(
52                    "Cannot convert object vector to dense".to_string(),
53                )),
54            };
55        }
56
57        if indices.is_some() {
58            return Err(QdrantError::ConversionError(
59                "Cannot convert sparse vector to dense".to_string(),
60            ));
61        }
62
63        if vectors_count.is_some() && vectors_count.unwrap() > 1 {
64            return Err(QdrantError::ConversionError(
65                "Cannot convert multi vector to dense".to_string(),
66            ));
67        }
68
69        Ok(data)
70    }
71
72    #[expect(deprecated)]
73    pub fn try_into_sparse(self) -> Result<(Vec<u32>, Vec<f32>), QdrantError> {
74        let Vector {
75            data,
76            indices,
77            vectors_count,
78            vector,
79        } = self;
80
81        if let Some(v) = vector {
82            return match v {
83                vector::Vector::Dense(_) => Err(QdrantError::ConversionError(
84                    "Cannot convert dense vector to sparse".to_string(),
85                )),
86                vector::Vector::Sparse(sparse) => Ok((sparse.indices, sparse.values)),
87                vector::Vector::MultiDense(_) => Err(QdrantError::ConversionError(
88                    "Cannot convert multi-vector to sparse".to_string(),
89                )),
90                vector::Vector::Document(_) => Err(QdrantError::ConversionError(
91                    "Cannot convert document vector to sparse".to_string(),
92                )),
93                vector::Vector::Image(_) => Err(QdrantError::ConversionError(
94                    "Cannot convert image vector to sparse".to_string(),
95                )),
96                vector::Vector::Object(_) => Err(QdrantError::ConversionError(
97                    "Cannot convert object vector to sparse".to_string(),
98                )),
99            };
100        }
101
102        if indices.is_none() {
103            return Err(QdrantError::ConversionError(
104                "Cannot convert dense vector to sparse".to_string(),
105            ));
106        }
107
108        if vectors_count.is_some() && vectors_count.unwrap() > 1 {
109            return Err(QdrantError::ConversionError(
110                "Cannot convert multi vector to sparse".to_string(),
111            ));
112        }
113
114        let indices = indices.unwrap().data;
115
116        if indices.len() != data.len() {
117            return Err(QdrantError::ConversionError(format!(
118                "Malformed sparse vector: indices length {} is not equal to data length {}",
119                indices.len(),
120                data.len()
121            )));
122        }
123
124        Ok((indices, data))
125    }
126
127    #[expect(deprecated)]
128    pub fn try_into_multi(self) -> Result<Vec<Vec<f32>>, QdrantError> {
129        let Vector {
130            data,
131            indices,
132            vectors_count,
133            vector,
134        } = self;
135
136        if let Some(v) = vector {
137            return match v {
138                vector::Vector::Dense(_) => Err(QdrantError::ConversionError(
139                    "Cannot convert dense vector to multi-vector".to_string(),
140                )),
141                vector::Vector::Sparse(_) => Err(QdrantError::ConversionError(
142                    "Cannot convert sparse vector to multi-vector".to_string(),
143                )),
144                vector::Vector::MultiDense(multivec) => Ok(multivec
145                    .vectors
146                    .into_iter()
147                    .map(|v| v.data)
148                    .collect::<Vec<_>>()),
149                vector::Vector::Document(_) => Err(QdrantError::ConversionError(
150                    "Cannot convert document vector to multi-vector".to_string(),
151                )),
152                vector::Vector::Image(_) => Err(QdrantError::ConversionError(
153                    "Cannot convert image vector to multi-vector".to_string(),
154                )),
155                vector::Vector::Object(_) => Err(QdrantError::ConversionError(
156                    "Cannot convert object vector to multi-vector".to_string(),
157                )),
158            };
159        }
160
161        if vectors_count.is_none() {
162            return Err(QdrantError::ConversionError(
163                "Cannot convert single vector to multi".to_string(),
164            ));
165        }
166
167        if indices.is_some() {
168            return Err(QdrantError::ConversionError(
169                "Cannot convert sparse vector to multi-vector".to_string(),
170            ));
171        }
172
173        let vectors_count = vectors_count.unwrap();
174
175        if !data.len().is_multiple_of(vectors_count as usize) {
176            return Err(QdrantError::ConversionError(format!(
177                "Malformed multi vector: data length {} is not divisible by vectors count {}",
178                data.len(),
179                vectors_count
180            )));
181        }
182
183        Ok(data
184            .chunks(data.len() / vectors_count as usize)
185            .map(|v| v.to_vec())
186            .collect())
187    }
188}
189
190impl NamedVectors {
191    pub fn add_vector(mut self, name: impl Into<String>, vector: impl Into<Vector>) -> Self {
192        self.vectors.insert(name.into(), vector.into());
193        self
194    }
195}
196
197impl From<crate::qdrant::vector::Vector> for Vector {
198    #[allow(deprecated)]
199    fn from(vector: crate::qdrant::vector::Vector) -> Self {
200        #[expect(deprecated)]
201        Vector {
202            vector: Some(vector),
203            // Deprecated
204            data: vec![],
205            indices: None,
206            vectors_count: None,
207        }
208    }
209}