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