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