qdrant_client/grpc_conversions/
vectors.rs

1use crate::qdrant::vector_output::Vector;
2use crate::qdrant::vectors_output::VectorsOptions;
3use crate::qdrant::{MultiDenseVector, SparseVector, VectorOutput, VectorsOutput};
4
5impl VectorOutput {
6    #[allow(deprecated)]
7    pub fn into_vector(self) -> Vector {
8        let VectorOutput {
9            data,
10            indices,
11            vectors_count,
12            vector,
13        } = self;
14
15        if let Some(v) = vector {
16            return v;
17        }
18
19        if let Some(indices) = indices {
20            return Vector::Sparse(SparseVector::from((indices.data, data)));
21        }
22
23        if let Some(vectors_count) = vectors_count {
24            let vectors: Vec<_> = data
25                .chunks(data.len() / vectors_count as usize)
26                .collect::<Vec<_>>();
27
28            return Vector::MultiDense(MultiDenseVector::from(vectors));
29        }
30
31        Vector::Dense(crate::qdrant::DenseVector { data })
32    }
33}
34
35impl VectorsOutput {
36    /// Get default (unnamed) vector from VectorsOutput.
37    ///
38    /// Return `None` if the default vector is not present.
39    ///
40    /// Use `Self::get_vector_by_name` to get named vectors from VectorsOutput.
41    pub fn get_vector(&self) -> Option<Vector> {
42        self.vectors_options
43            .as_ref()
44            .and_then(|option| match option {
45                VectorsOptions::Vector(vector) => Some(vector.clone().into_vector()),
46                VectorsOptions::Vectors(_) => None,
47            })
48    }
49
50    /// Get vector by name from VectorsOutput.
51    ///
52    /// Return `None` if the named vector is not present or is a default (unnamed) vector.
53    ///
54    /// Use `Self::get_vector` to get the default (unnamed) vector from VectorsOutput.
55    pub fn get_vector_by_name(&self, name: &str) -> Option<Vector> {
56        self.vectors_options
57            .as_ref()
58            .and_then(|option| match option {
59                VectorsOptions::Vector(vector) => {
60                    if name.is_empty() {
61                        Some(vector.clone().into_vector())
62                    } else {
63                        None
64                    }
65                }
66                VectorsOptions::Vectors(vectors) => vectors
67                    .vectors
68                    .get(name)
69                    .cloned()
70                    .map(|vector| vector.into_vector()),
71            })
72    }
73}