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