Skip to main content

qdrant_client/
builder_ext.rs

1use std::collections::HashMap;
2
3use crate::qdrant::update_collection_cluster_setup_request::Operation;
4use crate::qdrant::{
5    shard_key, AbortShardTransferBuilder, BinaryQuantizationBuilder, ClearPayloadPointsBuilder,
6    ContextExamplePair, CountPointsBuilder, CreateAliasBuilder, CreateCollectionBuilder,
7    CreateFieldIndexCollectionBuilder, CreateShardKeyRequestBuilder,
8    CreateVectorNameRequestBuilder, DeleteCollectionBuilder, DeleteFieldIndexCollectionBuilder,
9    DeletePayloadPointsBuilder, DeletePointVectorsBuilder, DeletePointsBuilder, DeleteShardKey,
10    DeleteShardKeyRequestBuilder, DeleteSnapshotRequestBuilder, DeleteVectorNameRequestBuilder,
11    DenseVectorCreationConfigBuilder, DiscoverBatchPointsBuilder, DiscoverPoints,
12    DiscoverPointsBuilder, Distance, FacetCountsBuilder, FieldType, GetPointsBuilder,
13    LookupLocationBuilder, MoveShardBuilder, PayloadExcludeSelector, PayloadIncludeSelector,
14    PointId, PointStruct, PointVectors, PointsUpdateOperation, ProductQuantizationBuilder,
15    QuantizationType, QueryBatchPointsBuilder, QueryPointGroupsBuilder, QueryPoints,
16    QueryPointsBuilder, RecommendBatchPointsBuilder, RecommendExample, RecommendPointGroupsBuilder,
17    RecommendPoints, RecommendPointsBuilder, RenameAliasBuilder, ReplicaBuilder,
18    ReplicateShardBuilder, ScalarQuantizationBuilder, ScrollPointsBuilder,
19    SearchBatchPointsBuilder, SearchMatrixPointsBuilder, SearchPointGroupsBuilder, SearchPoints,
20    SearchPointsBuilder, SetPayloadPointsBuilder, ShardKey, SparseVectorCreationConfigBuilder,
21    TurboQuantizationBuilder, UpdateBatchPointsBuilder, UpdateCollectionBuilder,
22    UpdateCollectionClusterSetupRequestBuilder, UpdatePointVectorsBuilder, UpsertPointsBuilder,
23    Value, VectorParamsBuilder, VectorsSelector, WithLookupBuilder,
24};
25
26impl VectorParamsBuilder {
27    pub fn new(size: u64, distance: Distance) -> Self {
28        let mut builder = Self::empty();
29        builder.size = Some(size);
30        builder.distance = Some(distance.into());
31        builder
32    }
33}
34
35impl Default for ScalarQuantizationBuilder {
36    fn default() -> Self {
37        let mut builder = Self::empty();
38        builder.r#type = Some(QuantizationType::Int8.into());
39        builder
40    }
41}
42
43impl ProductQuantizationBuilder {
44    pub fn new(compression: i32) -> Self {
45        let mut builder = Self::empty();
46        builder.compression = Some(compression);
47        builder
48    }
49}
50
51impl BinaryQuantizationBuilder {
52    pub fn new(always_ram: bool) -> Self {
53        let mut builder = Self::empty();
54        builder.always_ram = Some(Some(always_ram));
55        builder
56    }
57}
58
59impl TurboQuantizationBuilder {
60    pub fn new() -> Self {
61        Self::empty()
62    }
63}
64
65impl SearchPointsBuilder {
66    pub fn new(
67        collection_name: impl Into<String>,
68        vector: impl Into<Vec<f32>>,
69        limit: u64,
70    ) -> Self {
71        let mut builder = Self::empty();
72        builder.collection_name = Some(collection_name.into());
73        builder.vector = Some(vector.into());
74        builder.limit = Some(limit);
75        builder
76    }
77}
78
79impl UpdateCollectionBuilder {
80    pub fn new(collection_name: impl Into<String>) -> Self {
81        let mut builder = Self::empty();
82        builder.collection_name = Some(collection_name.into());
83        builder
84    }
85}
86
87impl SetPayloadPointsBuilder {
88    pub fn new(
89        collection_name: impl Into<String>,
90        payload: impl Into<HashMap<String, Value>>,
91    ) -> Self {
92        let mut builder = Self::empty();
93        builder.collection_name = Some(collection_name.into());
94        builder.payload = Some(payload.into());
95        builder
96    }
97}
98
99impl UpdateBatchPointsBuilder {
100    pub fn new(
101        collection_name: impl Into<String>,
102        operations: impl Into<Vec<PointsUpdateOperation>>,
103    ) -> Self {
104        let mut builder = Self::empty();
105        builder.collection_name = Some(collection_name.into());
106        builder.operations = Some(operations.into());
107        builder
108    }
109}
110
111impl DeletePayloadPointsBuilder {
112    pub fn new(collection_name: impl Into<String>, keys: impl Into<Vec<String>>) -> Self {
113        let mut builder = Self::empty();
114        builder.collection_name = Some(collection_name.into());
115        builder.keys = Some(keys.into());
116        builder
117    }
118}
119
120impl ClearPayloadPointsBuilder {
121    pub fn new(collection_name: impl Into<String>) -> Self {
122        let mut builder = Self::empty();
123        builder.collection_name = Some(collection_name.into());
124        builder
125    }
126}
127
128impl GetPointsBuilder {
129    pub fn new(collection_name: impl Into<String>, ids: impl Into<Vec<PointId>>) -> Self {
130        let mut builder = Self::empty();
131        builder.collection_name = Some(collection_name.into());
132        builder.ids = Some(ids.into());
133        builder
134    }
135}
136
137impl SearchBatchPointsBuilder {
138    pub fn new(
139        collection_name: impl Into<String>,
140        search_points: impl Into<Vec<SearchPoints>>,
141    ) -> Self {
142        let mut builder = Self::empty();
143        builder.collection_name = Some(collection_name.into());
144        builder.search_points = Some(search_points.into());
145        builder
146    }
147}
148
149impl SearchPointGroupsBuilder {
150    pub fn new(
151        collection_name: impl Into<String>,
152        vector: impl Into<Vec<f32>>,
153        limit: u32,
154        group_by: impl Into<String>,
155        group_size: u32,
156    ) -> Self {
157        let mut builder = Self::empty();
158        builder.collection_name = Some(collection_name.into());
159        builder.vector = Some(vector.into());
160        builder.limit = Some(limit);
161        builder.group_by = Some(group_by.into());
162        builder.group_size = Some(group_size);
163        builder
164    }
165}
166
167impl WithLookupBuilder {
168    pub fn new(collection: impl Into<String>) -> Self {
169        let mut builder = Self::empty();
170        builder.collection = Some(collection.into());
171        builder
172    }
173}
174
175impl DeletePointsBuilder {
176    pub fn new(collection_name: impl Into<String>) -> Self {
177        let mut builder = Self::empty();
178        builder.collection_name = Some(collection_name.into());
179        builder
180    }
181}
182
183impl DeletePointVectorsBuilder {
184    pub fn new(collection_name: impl Into<String>) -> Self {
185        let mut builder = Self::empty();
186        builder.collection_name = Some(collection_name.into());
187        builder
188    }
189}
190
191impl UpdatePointVectorsBuilder {
192    pub fn new(collection_name: impl Into<String>, points: impl Into<Vec<PointVectors>>) -> Self {
193        let mut builder = Self::empty();
194        builder.collection_name = Some(collection_name.into());
195        builder.points = Some(points.into());
196        builder
197    }
198}
199
200impl ScrollPointsBuilder {
201    pub fn new(collection_name: impl Into<String>) -> Self {
202        let mut builder = Self::empty();
203        builder.collection_name = Some(collection_name.into());
204        builder
205    }
206}
207
208impl RecommendPointsBuilder {
209    pub fn new(collection_name: impl Into<String>, limit: u64) -> Self {
210        let mut builder = Self::empty();
211        builder.collection_name = Some(collection_name.into());
212        builder.limit = Some(limit);
213        builder
214    }
215}
216
217impl LookupLocationBuilder {
218    pub fn new(collection_name: impl Into<String>) -> Self {
219        let mut builder = Self::empty();
220        builder.collection_name = Some(collection_name.into());
221        builder
222    }
223}
224
225impl RecommendBatchPointsBuilder {
226    pub fn new(
227        collection_name: impl Into<String>,
228        recommend_points: impl Into<Vec<RecommendPoints>>,
229    ) -> Self {
230        let mut builder = Self::empty();
231        builder.collection_name = Some(collection_name.into());
232        builder.recommend_points = Some(recommend_points.into());
233        builder
234    }
235}
236
237impl RecommendPointGroupsBuilder {
238    pub fn new(
239        collection_name: impl Into<String>,
240        group_by: impl Into<String>,
241        group_size: u32,
242        limit: u32,
243    ) -> Self {
244        let mut builder = Self::empty();
245        builder.collection_name = Some(collection_name.into());
246        builder.group_by = Some(group_by.into());
247        builder.group_size = Some(group_size);
248        builder.limit = Some(limit);
249        builder
250    }
251}
252
253impl DiscoverPointsBuilder {
254    pub fn new(
255        collection_name: impl Into<String>,
256        context: impl Into<Vec<ContextExamplePair>>,
257        limit: u64,
258    ) -> Self {
259        let mut builder = Self::empty();
260        builder.collection_name = Some(collection_name.into());
261        builder.context = Some(context.into());
262        builder.limit = Some(limit);
263        builder
264    }
265}
266
267impl DiscoverBatchPointsBuilder {
268    pub fn new(
269        collection_name: impl Into<String>,
270        discover_points: impl Into<Vec<DiscoverPoints>>,
271    ) -> Self {
272        let mut builder = Self::empty();
273        builder.collection_name = Some(collection_name.into());
274        builder.discover_points = Some(discover_points.into());
275        builder
276    }
277}
278
279impl CountPointsBuilder {
280    pub fn new(collection_name: impl Into<String>) -> Self {
281        let mut builder = Self::empty();
282        builder.collection_name = Some(collection_name.into());
283        builder
284    }
285}
286
287impl UpsertPointsBuilder {
288    pub fn new(collection_name: impl Into<String>, points: impl Into<Vec<PointStruct>>) -> Self {
289        let mut builder = Self::empty();
290        builder.collection_name = Some(collection_name.into());
291        builder.points = Some(points.into());
292        builder
293    }
294}
295
296impl CreateFieldIndexCollectionBuilder {
297    pub fn new(
298        collection_name: impl Into<String>,
299        field_name: impl Into<String>,
300        field_type: FieldType,
301    ) -> Self {
302        let mut builder = Self::empty();
303        builder.collection_name = Some(collection_name.into());
304        builder.field_name = Some(field_name.into());
305        builder.field_type(field_type)
306    }
307}
308
309impl DeleteFieldIndexCollectionBuilder {
310    pub fn new(collection_name: impl Into<String>, field_name: impl Into<String>) -> Self {
311        let mut builder = Self::empty();
312        builder.collection_name = Some(collection_name.into());
313        builder.field_name = Some(field_name.into());
314        builder
315    }
316}
317
318impl DenseVectorCreationConfigBuilder {
319    pub fn new(size: u64, distance: Distance) -> Self {
320        let mut builder = Self::empty();
321        builder.size = Some(size);
322        builder.distance = Some(distance.into());
323        builder
324    }
325}
326
327impl SparseVectorCreationConfigBuilder {
328    pub fn new() -> Self {
329        Self::empty()
330    }
331}
332
333impl CreateVectorNameRequestBuilder {
334    pub fn new(
335        collection_name: impl Into<String>,
336        vector_name: impl Into<String>,
337        vector_config: impl Into<crate::qdrant::create_vector_name_request::VectorConfig>,
338    ) -> Self {
339        let mut builder = Self::empty();
340        builder.collection_name = Some(collection_name.into());
341        builder.vector_name = Some(vector_name.into());
342        builder.vector_config(vector_config)
343    }
344}
345
346impl DeleteVectorNameRequestBuilder {
347    pub fn new(collection_name: impl Into<String>, vector_name: impl Into<String>) -> Self {
348        let mut builder = Self::empty();
349        builder.collection_name = Some(collection_name.into());
350        builder.vector_name = Some(vector_name.into());
351        builder
352    }
353}
354
355impl UpdateCollectionClusterSetupRequestBuilder {
356    pub fn new(collection_name: impl Into<String>, operation: impl Into<Operation>) -> Self {
357        let mut builder = Self::empty();
358        builder.collection_name = Some(collection_name.into());
359        builder.operation = Some(Some(operation.into()));
360        builder
361    }
362}
363
364impl MoveShardBuilder {
365    pub fn new(shard_id: u32, from_peer_id: u64, to_peer_id: u64) -> Self {
366        let mut builder = Self::empty();
367        builder.shard_id = Some(shard_id);
368        builder.from_peer_id = Some(from_peer_id);
369        builder.to_peer_id = Some(to_peer_id);
370        builder
371    }
372}
373
374impl ReplicateShardBuilder {
375    pub fn new(shard_id: u32, from_peer_id: u64, to_peer_id: u64) -> Self {
376        let mut builder = Self::empty();
377        builder.shard_id = Some(shard_id);
378        builder.from_peer_id = Some(from_peer_id);
379        builder.to_peer_id = Some(to_peer_id);
380        builder
381    }
382}
383
384impl AbortShardTransferBuilder {
385    pub fn new(shard_id: u32, from_peer_id: u64, to_peer_id: u64) -> Self {
386        let mut builder = Self::empty();
387        builder.shard_id = Some(shard_id);
388        builder.from_peer_id = Some(from_peer_id);
389        builder.to_peer_id = Some(to_peer_id);
390        builder
391    }
392}
393
394impl ReplicaBuilder {
395    pub fn new(shard_id: u32, peer_id: u64) -> Self {
396        let mut builder = Self::empty();
397        builder.shard_id = Some(shard_id);
398        builder.peer_id = Some(peer_id);
399        builder
400    }
401}
402
403impl CreateShardKeyRequestBuilder {
404    pub fn new(collection_name: impl Into<String>) -> Self {
405        let mut builder = Self::empty();
406        builder.collection_name = Some(collection_name.into());
407        builder
408    }
409}
410
411impl DeleteShardKeyRequestBuilder {
412    pub fn new(collection_name: impl Into<String>) -> Self {
413        let mut builder = Self::empty();
414        builder.collection_name = Some(collection_name.into());
415        builder
416    }
417
418    /// Shard key to delete
419    pub fn key(mut self, key: impl Into<shard_key::Key>) -> Self {
420        self.request = Some(Some(DeleteShardKey {
421            shard_key: Some(ShardKey {
422                key: Some(key.into()),
423            }),
424        }));
425        self
426    }
427}
428
429impl DeleteCollectionBuilder {
430    pub fn new(collection_name: impl Into<String>) -> Self {
431        let mut builder = Self::empty();
432        builder.collection_name = Some(collection_name.into());
433        builder
434    }
435}
436
437impl PayloadIncludeSelector {
438    pub fn new(fileds: impl Into<Vec<String>>) -> Self {
439        Self {
440            fields: fileds.into(),
441        }
442    }
443}
444
445impl PayloadExcludeSelector {
446    pub fn new(fileds: impl Into<Vec<String>>) -> Self {
447        Self {
448            fields: fileds.into(),
449        }
450    }
451}
452
453impl VectorsSelector {
454    pub fn new(names: impl Into<Vec<String>>) -> Self {
455        Self {
456            names: names.into(),
457        }
458    }
459}
460
461impl RecommendPointsBuilder {
462    /// Look for vectors closest to the vectors from these points or vectors
463    pub fn add_positive(mut self, recommend_example: impl Into<RecommendExample>) -> Self {
464        let recommend_example = recommend_example.into();
465        match recommend_example {
466            RecommendExample::PointId(point_id) => {
467                self.positive.get_or_insert_with(Vec::new).push(point_id);
468            }
469            RecommendExample::Vector(vector) => {
470                self.positive_vectors
471                    .get_or_insert_with(Vec::new)
472                    .push(vector);
473            }
474        }
475        self
476    }
477
478    /// Try to avoid vectors like the vector from these points or vectors
479    pub fn add_negative(mut self, recommend_example: impl Into<RecommendExample>) -> Self {
480        let recommend_example = recommend_example.into();
481        match recommend_example {
482            RecommendExample::PointId(point_id) => {
483                self.negative.get_or_insert_with(Vec::new).push(point_id);
484            }
485            RecommendExample::Vector(vector) => {
486                self.negative_vectors
487                    .get_or_insert_with(Vec::new)
488                    .push(vector);
489            }
490        }
491        self
492    }
493}
494
495impl RecommendPointGroupsBuilder {
496    /// Look for vectors closest to the vectors from these points or vectors
497    pub fn add_positive(mut self, recommend_example: impl Into<RecommendExample>) -> Self {
498        let recommend_example = recommend_example.into();
499        match recommend_example {
500            RecommendExample::PointId(point_id) => {
501                self.positive.get_or_insert_with(Vec::new).push(point_id);
502            }
503            RecommendExample::Vector(vector) => {
504                self.positive_vectors
505                    .get_or_insert_with(Vec::new)
506                    .push(vector);
507            }
508        }
509        self
510    }
511
512    /// Try to avoid vectors like the vector from these points or vectors
513    pub fn add_negative(mut self, recommend_example: impl Into<RecommendExample>) -> Self {
514        let recommend_example = recommend_example.into();
515        match recommend_example {
516            RecommendExample::PointId(point_id) => {
517                self.negative.get_or_insert_with(Vec::new).push(point_id);
518            }
519            RecommendExample::Vector(vector) => {
520                self.negative_vectors
521                    .get_or_insert_with(Vec::new)
522                    .push(vector);
523            }
524        }
525        self
526    }
527}
528
529impl CreateCollectionBuilder {
530    pub fn new(collection_name: impl Into<String>) -> Self {
531        Self::default().collection_name(collection_name)
532    }
533}
534
535impl CreateAliasBuilder {
536    pub fn new(collection_name: impl Into<String>, alias_name: impl Into<String>) -> Self {
537        let mut builder = Self::empty();
538        builder.collection_name = Some(collection_name.into());
539        builder.alias_name = Some(alias_name.into());
540        builder
541    }
542}
543
544impl RenameAliasBuilder {
545    pub fn new(old_alias_name: impl Into<String>, new_alias_name: impl Into<String>) -> Self {
546        let mut builder = Self::empty();
547        builder.old_alias_name = Some(old_alias_name.into());
548        builder.new_alias_name = Some(new_alias_name.into());
549        builder
550    }
551}
552
553impl QueryPointsBuilder {
554    pub fn new(collection_name: impl Into<String>) -> Self {
555        let mut builder = Self::empty();
556        builder.collection_name = Some(collection_name.into());
557        builder
558    }
559}
560
561impl QueryBatchPointsBuilder {
562    pub fn new(
563        collection_name: impl Into<String>,
564        query_points: impl Into<Vec<QueryPoints>>,
565    ) -> Self {
566        let mut builder = Self::empty();
567        builder.collection_name = Some(collection_name.into());
568        builder.query_points = Some(query_points.into());
569        builder
570    }
571}
572
573impl DeleteSnapshotRequestBuilder {
574    pub fn new(collection_name: impl Into<String>, snapshot_name: impl Into<String>) -> Self {
575        let mut builder = Self::empty();
576        builder.collection_name = Some(collection_name.into());
577        builder.snapshot_name = Some(snapshot_name.into());
578        builder
579    }
580}
581
582impl QueryPointGroupsBuilder {
583    pub fn new(
584        collection_name: impl Into<String>,
585        group_by: impl Into<String>,
586    ) -> QueryPointGroupsBuilder {
587        let mut builder = Self::empty();
588        builder.collection_name = Some(collection_name.into());
589        builder.group_by = Some(group_by.into());
590        builder
591    }
592}
593
594impl FacetCountsBuilder {
595    pub fn new(collection_name: impl Into<String>, key: impl Into<String>) -> FacetCountsBuilder {
596        let mut builder = Self::empty();
597        builder.collection_name = Some(collection_name.into());
598        builder.key = Some(key.into());
599        builder
600    }
601}
602
603impl SearchMatrixPointsBuilder {
604    pub fn new(collection_name: impl Into<String>) -> SearchMatrixPointsBuilder {
605        let mut builder = Self::empty();
606        builder.collection_name = Some(collection_name.into());
607        builder
608    }
609}