Skip to main content

qdrant_client/builders/
create_collection_builder.rs

1use std::collections::HashMap;
2
3use crate::grpc_conversions::metadata::MetadataWrapper;
4use crate::grpc_macros::convert_option;
5use crate::qdrant::*;
6
7#[must_use]
8#[derive(Debug, Clone)]
9pub struct CreateCollectionBuilder {
10    /// Name of the collection
11    pub(crate) collection_name: Option<String>,
12    /// Configuration of vector index
13    pub(crate) hnsw_config: Option<Option<HnswConfigDiff>>,
14    /// Configuration of the Write-Ahead-Log
15    pub(crate) wal_config: Option<Option<WalConfigDiff>>,
16    /// Configuration of the optimizers
17    pub(crate) optimizers_config: Option<Option<OptimizersConfigDiff>>,
18    /// Number of shards in the collection, default is 1 for standalone, otherwise equal to the number of nodes. Minimum is 1
19    pub(crate) shard_number: Option<Option<u32>>,
20    /// If true - point's payload will not be stored in memory
21    pub(crate) on_disk_payload: Option<Option<bool>>,
22    /// Wait timeout for operation commit in seconds, if not specified - default value will be supplied
23    pub(crate) timeout: Option<Option<u64>>,
24    /// Configuration for vectors
25    pub(crate) vectors_config: Option<Option<VectorsConfig>>,
26    /// Number of replicas of each shard that network tries to maintain, default = 1
27    pub(crate) replication_factor: Option<Option<u32>>,
28    /// How many replicas should apply the operation for us to consider it successful, default = 1
29    pub(crate) write_consistency_factor: Option<Option<u32>>,
30    /// Quantization configuration of vector
31    quantization_config: Option<quantization_config::Quantization>,
32    /// Sharding method
33    pub(crate) sharding_method: Option<Option<i32>>,
34    /// Configuration for sparse vectors
35    pub(crate) sparse_vectors_config: Option<Option<SparseVectorConfig>>,
36    /// Configuration for strict mode
37    pub(crate) strict_mode_config: Option<Option<StrictModeConfig>>,
38    /// Arbitrary JSON metadata for the collection
39    pub(crate) metadata: Option<HashMap<String, Value>>,
40    /// Configuration of the payload storage
41    pub(crate) payload: Option<Option<PayloadStorageParams>>,
42}
43
44#[allow(clippy::all)]
45#[allow(clippy::derive_partial_eq_without_eq)]
46impl CreateCollectionBuilder {
47    /// Name of the collection
48    pub fn collection_name<VALUE: Into<String>>(self, value: VALUE) -> Self {
49        let mut new = self;
50        new.collection_name = Option::Some(value.into());
51        new
52    }
53    /// Configuration of vector index
54    pub fn hnsw_config<VALUE: core::convert::Into<HnswConfigDiff>>(self, value: VALUE) -> Self {
55        let mut new = self;
56        new.hnsw_config = Option::Some(Option::Some(value.into()));
57        new
58    }
59    /// Configuration of the Write-Ahead-Log
60    pub fn wal_config<VALUE: core::convert::Into<WalConfigDiff>>(self, value: VALUE) -> Self {
61        let mut new = self;
62        new.wal_config = Option::Some(Option::Some(value.into()));
63        new
64    }
65    /// Configuration of the optimizers
66    pub fn optimizers_config<VALUE: core::convert::Into<OptimizersConfigDiff>>(
67        self,
68        value: VALUE,
69    ) -> Self {
70        let mut new = self;
71        new.optimizers_config = Option::Some(Option::Some(value.into()));
72        new
73    }
74    /// Number of shards in the collection, default is 1 for standalone, otherwise equal to the number of nodes. Minimum is 1
75    pub fn shard_number(self, value: u32) -> Self {
76        let mut new = self;
77        new.shard_number = Option::Some(Option::Some(value));
78        new
79    }
80    /// If true - point's payload will not be stored in memory
81    ///
82    /// Deprecated since 1.19.0, use [`payload`](Self::payload) instead.
83    pub fn on_disk_payload(self, value: bool) -> Self {
84        let mut new = self;
85        new.on_disk_payload = Option::Some(Option::Some(value));
86        new
87    }
88    /// Wait timeout for operation commit in seconds, if not specified - default value will be supplied
89    pub fn timeout(self, value: u64) -> Self {
90        let mut new = self;
91        new.timeout = Option::Some(Option::Some(value));
92        new
93    }
94    /// Configuration for vectors
95    pub fn vectors_config<VALUE: core::convert::Into<VectorsConfig>>(self, value: VALUE) -> Self {
96        let mut new = self;
97        new.vectors_config = Option::Some(Option::Some(value.into()));
98        new
99    }
100    /// Number of replicas of each shard that network tries to maintain, default = 1
101    pub fn replication_factor(self, value: u32) -> Self {
102        let mut new = self;
103        new.replication_factor = Option::Some(Option::Some(value));
104        new
105    }
106    /// How many replicas should apply the operation for us to consider it successful, default = 1
107    pub fn write_consistency_factor(self, value: u32) -> Self {
108        let mut new = self;
109        new.write_consistency_factor = Option::Some(Option::Some(value));
110        new
111    }
112    /// Quantization configuration of vector
113    pub fn quantization_config<VALUE: core::convert::Into<quantization_config::Quantization>>(
114        self,
115        value: VALUE,
116    ) -> Self {
117        let mut new = self;
118        new.quantization_config = Option::Some(value.into());
119        new
120    }
121    /// Sharding method
122    pub fn sharding_method(self, value: i32) -> Self {
123        let mut new = self;
124        new.sharding_method = Option::Some(Option::Some(value));
125        new
126    }
127    /// Configuration for sparse vectors
128    pub fn sparse_vectors_config<VALUE: core::convert::Into<SparseVectorConfig>>(
129        self,
130        value: VALUE,
131    ) -> Self {
132        let mut new = self;
133        new.sparse_vectors_config = Option::Some(Option::Some(value.into()));
134        new
135    }
136    /// Configuration for strict mode
137    pub fn strict_mode_config<VALUE: core::convert::Into<StrictModeConfig>>(
138        self,
139        value: VALUE,
140    ) -> Self {
141        let mut new = self;
142        new.strict_mode_config = Option::Some(Option::Some(value.into()));
143        new
144    }
145    /// Arbitrary JSON metadata for the collection
146    pub fn metadata(self, value: impl Into<MetadataWrapper>) -> Self {
147        let mut new = self;
148        new.metadata = Option::Some(value.into().0);
149        new
150    }
151    /// Configuration of the payload storage.
152    /// Overrides the deprecated `on_disk_payload` flag if both are set.
153    pub fn payload<VALUE: core::convert::Into<PayloadStorageParams>>(self, value: VALUE) -> Self {
154        let mut new = self;
155        new.payload = Option::Some(Option::Some(value.into()));
156        new
157    }
158
159    #[allow(deprecated)]
160    fn build_inner(self) -> Result<CreateCollection, std::convert::Infallible> {
161        Ok(CreateCollection {
162            collection_name: match self.collection_name {
163                Some(value) => value,
164                None => core::default::Default::default(),
165            },
166            hnsw_config: match self.hnsw_config {
167                Some(value) => value,
168                None => core::default::Default::default(),
169            },
170            wal_config: match self.wal_config {
171                Some(value) => value,
172                None => core::default::Default::default(),
173            },
174            optimizers_config: match self.optimizers_config {
175                Some(value) => value,
176                None => core::default::Default::default(),
177            },
178            shard_number: match self.shard_number {
179                Some(value) => value,
180                None => core::default::Default::default(),
181            },
182            on_disk_payload: match self.on_disk_payload {
183                Some(value) => value,
184                None => core::default::Default::default(),
185            },
186            timeout: match self.timeout {
187                Some(value) => value,
188                None => core::default::Default::default(),
189            },
190            vectors_config: match self.vectors_config {
191                Some(value) => value,
192                None => core::default::Default::default(),
193            },
194            replication_factor: match self.replication_factor {
195                Some(value) => value,
196                None => core::default::Default::default(),
197            },
198            write_consistency_factor: match self.write_consistency_factor {
199                Some(value) => value,
200                None => core::default::Default::default(),
201            },
202            quantization_config: { convert_option(&self.quantization_config) },
203            sharding_method: match self.sharding_method {
204                Some(value) => value,
205                None => core::default::Default::default(),
206            },
207            sparse_vectors_config: match self.sparse_vectors_config {
208                Some(value) => value,
209                None => core::default::Default::default(),
210            },
211            strict_mode_config: match self.strict_mode_config {
212                Some(value) => value,
213                None => core::default::Default::default(),
214            },
215            metadata: match self.metadata {
216                Some(value) => value,
217                None => core::default::Default::default(),
218            },
219            payload: match self.payload {
220                Some(value) => value,
221                None => core::default::Default::default(),
222            },
223        })
224    }
225    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
226    fn create_empty() -> Self {
227        Self {
228            collection_name: core::default::Default::default(),
229            hnsw_config: core::default::Default::default(),
230            wal_config: core::default::Default::default(),
231            optimizers_config: core::default::Default::default(),
232            shard_number: core::default::Default::default(),
233            on_disk_payload: core::default::Default::default(),
234            timeout: core::default::Default::default(),
235            vectors_config: core::default::Default::default(),
236            replication_factor: core::default::Default::default(),
237            write_consistency_factor: core::default::Default::default(),
238            quantization_config: core::default::Default::default(),
239            sharding_method: core::default::Default::default(),
240            sparse_vectors_config: core::default::Default::default(),
241            strict_mode_config: core::default::Default::default(),
242            metadata: core::default::Default::default(),
243            payload: core::default::Default::default(),
244        }
245    }
246}
247
248impl From<CreateCollectionBuilder> for CreateCollection {
249    fn from(value: CreateCollectionBuilder) -> Self {
250        value.build_inner().unwrap_or_else(|_| {
251            panic!(
252                "Failed to convert {0} to {1}",
253                "CreateCollectionBuilder", "CreateCollection"
254            )
255        })
256    }
257}
258
259impl CreateCollectionBuilder {
260    /// Builds the desired type. Can often be omitted.
261    pub fn build(self) -> CreateCollection {
262        self.build_inner().unwrap_or_else(|_| {
263            panic!(
264                "Failed to build {0} into {1}",
265                "CreateCollectionBuilder", "CreateCollection"
266            )
267        })
268    }
269}
270
271impl Default for CreateCollectionBuilder {
272    fn default() -> Self {
273        Self::create_empty()
274    }
275}