qdrant_client/builders/
vector_params_builder.rs

1use crate::grpc_macros::convert_option;
2use crate::qdrant::*;
3
4#[derive(Clone)]
5pub struct VectorParamsBuilder {
6    /// Size of the vectors
7    pub(crate) size: Option<u64>,
8    /// Distance function used for comparing vectors
9    pub(crate) distance: Option<i32>,
10    /// Configuration of vector HNSW graph. If omitted - the collection configuration will be used
11    pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
12    /// Configuration of vector quantization config. If omitted - the collection configuration will be used
13    quantization_config: Option<quantization_config::Quantization>,
14    /// If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM.
15    pub(crate) on_disk: Option<Option<bool>>,
16    /// Data type of the vectors
17    pub(crate) datatype: Option<Option<i32>>,
18    /// Configuration for multi-vector search
19    pub(crate) multivector_config: Option<Option<MultiVectorConfig>>,
20}
21
22impl VectorParamsBuilder {
23    /// Size of the vectors
24    #[allow(unused_mut)]
25    pub fn size(self, value: u64) -> Self {
26        let mut new = self;
27        new.size = Option::Some(value);
28        new
29    }
30    /// Distance function used for comparing vectors
31    #[allow(unused_mut)]
32    pub fn distance<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
33        let mut new = self;
34        new.distance = Option::Some(value.into());
35        new
36    }
37    /// Configuration of vector HNSW graph. If omitted - the collection configuration will be used
38    #[allow(unused_mut)]
39    pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
40        let mut new = self;
41        new.hnsw_config = Option::Some(Option::Some(value.into()));
42        new
43    }
44    /// Configuration of vector quantization config. If omitted - the collection configuration will be used
45    #[allow(unused_mut)]
46    pub fn quantization_config<VALUE: core::convert::Into<quantization_config::Quantization>>(
47        self,
48        value: VALUE,
49    ) -> Self {
50        let mut new = self;
51        new.quantization_config = Option::Some(value.into());
52        new
53    }
54    /// If true - serve vectors from disk. If set to false, the vectors will be loaded in RAM.
55    #[allow(unused_mut)]
56    pub fn on_disk(self, value: bool) -> Self {
57        let mut new = self;
58        new.on_disk = Option::Some(Option::Some(value));
59        new
60    }
61    /// Data type of the vectors
62    #[allow(unused_mut)]
63    pub fn datatype<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
64        let mut new = self;
65        new.datatype = Option::Some(Option::Some(value.into()));
66        new
67    }
68    /// Configuration for multi-vector search
69    #[allow(unused_mut)]
70    pub fn multivector_config<VALUE: core::convert::Into<MultiVectorConfig>>(
71        self,
72        value: VALUE,
73    ) -> Self {
74        let mut new = self;
75        new.multivector_config = Option::Some(Option::Some(value.into()));
76        new
77    }
78
79    fn build_inner(self) -> Result<VectorParams, VectorParamsBuilderError> {
80        Ok(VectorParams {
81            size: self.size.unwrap_or_default(),
82            distance: self.distance.unwrap_or_default(),
83            hnsw_config: self.hnsw_config.unwrap_or_default(),
84            quantization_config: { convert_option(&self.quantization_config) },
85            on_disk: self.on_disk.unwrap_or_default(),
86            datatype: self.datatype.unwrap_or_default(),
87            multivector_config: self.multivector_config.unwrap_or_default(),
88        })
89    }
90    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
91    fn create_empty() -> Self {
92        Self {
93            size: core::default::Default::default(),
94            distance: core::default::Default::default(),
95            hnsw_config: core::default::Default::default(),
96            quantization_config: core::default::Default::default(),
97            on_disk: core::default::Default::default(),
98            datatype: core::default::Default::default(),
99            multivector_config: core::default::Default::default(),
100        }
101    }
102}
103
104impl From<VectorParamsBuilder> for VectorParams {
105    fn from(value: VectorParamsBuilder) -> Self {
106        value.build_inner().unwrap_or_else(|_| {
107            panic!(
108                "Failed to convert {0} to {1}",
109                "VectorParamsBuilder", "VectorParams"
110            )
111        })
112    }
113}
114
115impl VectorParamsBuilder {
116    /// Builds the desired type. Can often be omitted.
117    pub fn build(self) -> VectorParams {
118        self.build_inner().unwrap_or_else(|_| {
119            panic!(
120                "Failed to build {0} into {1}",
121                "VectorParamsBuilder", "VectorParams"
122            )
123        })
124    }
125}
126
127impl VectorParamsBuilder {
128    pub(crate) fn empty() -> Self {
129        Self::create_empty()
130    }
131}
132
133#[non_exhaustive]
134#[derive(Debug)]
135pub enum VectorParamsBuilderError {
136    /// Uninitialized field
137    UninitializedField(&'static str),
138    /// Custom validation error
139    ValidationError(String),
140}
141
142// Implementing the Display trait for better error messages
143impl std::fmt::Display for VectorParamsBuilderError {
144    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
145        match self {
146            Self::UninitializedField(field) => {
147                write!(f, "`{field}` must be initialized")
148            }
149            Self::ValidationError(error) => write!(f, "{error}"),
150        }
151    }
152}
153
154// Implementing the Error trait
155impl std::error::Error for VectorParamsBuilderError {}
156
157// Implementing From trait for conversion from UninitializedFieldError
158impl From<derive_builder::UninitializedFieldError> for VectorParamsBuilderError {
159    fn from(error: derive_builder::UninitializedFieldError) -> Self {
160        Self::UninitializedField(error.field_name())
161    }
162}
163
164// Implementing From trait for conversion from String
165impl From<String> for VectorParamsBuilderError {
166    fn from(error: String) -> Self {
167        Self::ValidationError(error)
168    }
169}