Skip to main content

qdrant_client/builders/
vector_params_builder.rs

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