weaviate_community/collections/batch.rs
1use crate::collections::objects::Object;
2/// All batch associated type components
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6/// A new BatchDeleteRequest used to make batch delete requests
7#[derive(Serialize, Deserialize, Debug)]
8#[serde(rename_all = "camelCase")]
9pub struct BatchDeleteRequest {
10 #[serde(rename = "match")]
11 pub matches: MatchConfig,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 #[serde(default)]
14 pub output: Option<Verbosity>,
15 #[serde(skip_serializing_if = "Option::is_none")]
16 #[serde(default)]
17 pub dry_run: Option<bool>,
18}
19
20impl BatchDeleteRequest {
21 /// Create a new builder for the BatchDeleteRequest object.
22 ///
23 /// This is the same as `BatchDeleteRequestBuilder::new()`.
24 ///
25 /// # Parameters
26 /// - matches: the match config of the BatchDeleteRequest
27 ///
28 /// # Example
29 /// ```rust
30 /// use weaviate_community::collections::batch::{BatchDeleteRequest, MatchConfig};
31 ///
32 /// let map = serde_json::json!({
33 /// "operator": "NotEqual",
34 /// "path": ["name"],
35 /// "valueText": "aaa"
36 /// });
37 /// let match_config = MatchConfig::new("Article", map);
38 ///
39 /// let builder = BatchDeleteRequest::builder(match_config);
40 /// ```
41 pub fn builder(matches: MatchConfig) -> BatchDeleteRequestBuilder {
42 BatchDeleteRequestBuilder::new(matches)
43 }
44}
45
46/// BatchDeleteRequestBuilder for building new BatchDeleteRequests
47pub struct BatchDeleteRequestBuilder {
48 pub matches: MatchConfig,
49 pub output: Option<Verbosity>,
50 pub dry_run: Option<bool>,
51}
52
53impl BatchDeleteRequestBuilder {
54 /// Create a new builder for the BatchDeleteRequest object.
55 ///
56 /// This is the same as `BatchDeleteRequest::builder()`.
57 ///
58 /// # Parameters
59 /// - matches: the match config of the BatchDeleteRequest
60 ///
61 /// # Example
62 /// ```rust
63 /// use weaviate_community::collections::batch::{BatchDeleteRequestBuilder, MatchConfig};
64 ///
65 /// let map = serde_json::json!({
66 /// "operator": "NotEqual",
67 /// "path": ["name"],
68 /// "valueText": "aaa"
69 /// });
70 /// let match_config = MatchConfig::new("Article", map);
71 ///
72 /// let builder = BatchDeleteRequestBuilder::new(match_config);
73 /// ```
74 pub fn new(matches: MatchConfig) -> BatchDeleteRequestBuilder {
75 BatchDeleteRequestBuilder {
76 matches,
77 output: None,
78 dry_run: None,
79 }
80 }
81
82 /// Add a value to the optional `output` value of the BatchDeleteRequest.
83 ///
84 /// # Parameters
85 /// - output: the verbosity level of the batch request
86 ///
87 /// # Example
88 /// ```rust
89 /// use weaviate_community::collections::batch::{
90 /// BatchDeleteRequestBuilder,
91 /// MatchConfig,
92 /// Verbosity
93 /// };
94 ///
95 /// let map = serde_json::json!({
96 /// "operator": "NotEqual",
97 /// "path": ["name"],
98 /// "valueText": "aaa"
99 /// });
100 /// let match_config = MatchConfig::new("Article", map);
101 ///
102 /// let builder = BatchDeleteRequestBuilder::new(match_config)
103 /// .with_output(Verbosity::VERBOSE);
104 /// ```
105 pub fn with_output(mut self, output: Verbosity) -> BatchDeleteRequestBuilder {
106 self.output = Some(output);
107 self
108 }
109
110 /// Add a value to the optional `dry_run` value of the BatchDeleteRequest.
111 ///
112 /// # Parameters
113 /// - dry_run: the dry_run flag to set in the batch request
114 ///
115 /// # Example
116 /// ```rust
117 /// use weaviate_community::collections::batch::{
118 /// BatchDeleteRequestBuilder,
119 /// MatchConfig,
120 /// };
121 ///
122 /// let map = serde_json::json!({
123 /// "operator": "NotEqual",
124 /// "path": ["name"],
125 /// "valueText": "aaa"
126 /// });
127 /// let match_config = MatchConfig::new("Article", map);
128 ///
129 /// let builder = BatchDeleteRequestBuilder::new(match_config)
130 /// .with_dry_run(true);
131 /// ```
132 pub fn with_dry_run(mut self, dry_run: bool) -> BatchDeleteRequestBuilder {
133 self.dry_run = Some(dry_run);
134 self
135 }
136
137 /// Build the BatchDeleteRequest from the BatchDeleteRequestBuilder
138 ///
139 /// # Example
140 /// Using BatchDeleteRequestBuilder
141 /// ```rust
142 /// use weaviate_community::collections::batch::{BatchDeleteRequestBuilder, MatchConfig};
143 ///
144 /// let map = serde_json::json!({
145 /// "operator": "NotEqual",
146 /// "path": ["name"],
147 /// "valueText": "aaa"
148 /// });
149 /// let match_config = MatchConfig::new("Article", map);
150 ///
151 /// let builder = BatchDeleteRequestBuilder::new(match_config).build();
152 /// ```
153 ///
154 /// Using BatchDeleteRequest
155 /// ```rust
156 /// use weaviate_community::collections::batch::{BatchDeleteRequest, MatchConfig};
157 ///
158 /// let map = serde_json::json!({
159 /// "operator": "NotEqual",
160 /// "path": ["name"],
161 /// "valueText": "aaa"
162 /// });
163 /// let match_config = MatchConfig::new("Article", map);
164 ///
165 /// let builder = BatchDeleteRequest::builder(match_config).build();
166 /// ```
167 pub fn build(self) -> BatchDeleteRequest {
168 BatchDeleteRequest {
169 matches: self.matches,
170 output: self.output,
171 dry_run: self.dry_run,
172 }
173 }
174}
175
176/// MatchConfig object outlining how to find the objects to be deleted.
177///
178/// Used explicitly in batch deletes.
179#[derive(Serialize, Deserialize, Debug)]
180pub struct MatchConfig {
181 pub class: String,
182 #[serde(rename = "where")]
183 pub match_where: serde_json::Value,
184}
185
186impl MatchConfig {
187 /// Create a new MatchConfig
188 ///
189 /// To revisit to strict type the map
190 ///
191 /// ```rust
192 /// use weaviate_community::collections::batch::MatchConfig;
193 /// let map = serde_json::json!({
194 /// "operator": "NotEqual",
195 /// "path": ["name"],
196 /// "valueText": "aaa"
197 /// });
198 /// let match_config = MatchConfig::new("Article", map);
199 /// ```
200 pub fn new(class: &str, match_where: serde_json::Value) -> MatchConfig {
201 MatchConfig {
202 class: class.into(),
203 match_where,
204 }
205 }
206}
207
208/// Strict definitions of the different verbosity levels available.
209///
210/// Weaviate supports MINIMAL and VERBOSE.
211#[derive(Serialize, Deserialize, Debug)]
212pub enum Verbosity {
213 #[serde(rename = "minimal")]
214 MINIMAL,
215 #[serde(rename = "verbose")]
216 VERBOSE,
217}
218
219/// The BatchDeleteResponse object resulting from a BatchDeleteRequest
220///
221/// You shouldn't need to create this yourself unless for asserting against.
222#[derive(Serialize, Deserialize, Debug)]
223#[serde(rename_all = "camelCase")]
224pub struct BatchDeleteResponse {
225 #[serde(rename = "match")]
226 pub matches: MatchConfig,
227 #[serde(skip_serializing_if = "Option::is_none")]
228 #[serde(default)]
229 pub output: Option<Verbosity>,
230 #[serde(skip_serializing_if = "Option::is_none")]
231 #[serde(default)]
232 pub dry_run: Option<bool>,
233 pub results: BatchDeleteResult,
234}
235
236/// The BatchDeleteResult of a BatchDeleteResponse
237///
238/// You shouldn't need to create this yourself unless for asserting against.
239#[derive(Serialize, Deserialize, Debug)]
240pub struct BatchDeleteResult {
241 pub matches: u64,
242 pub limit: u64,
243 pub successful: u64,
244 pub failed: u64,
245 #[serde(default)]
246 pub objects: Option<DeleteObjects>,
247}
248
249/// Container for multiple individual DeleteObject items
250///
251/// You shouldn't need to create this yourself.
252#[derive(Serialize, Deserialize, Debug)]
253pub struct DeleteObjects(Vec<DeleteObject>);
254
255#[derive(Serialize, Deserialize, Debug)]
256pub struct DeleteObject {
257 pub id: Uuid,
258 pub status: GeneralStatus,
259 #[serde(default)]
260 pub errors: Option<BatchRequestErrors>,
261}
262
263/// Strict definitions of the different status levels available for batch requests.
264///
265/// Weaviate supports SUCCESS, FAILED, and DRYRUN.
266#[derive(Serialize, Deserialize, Debug)]
267pub enum GeneralStatus {
268 SUCCESS,
269 FAILED,
270 DRYRUN,
271}
272
273/// The ResultStatus of a request
274///
275/// You shouldn't need to create this yourself unless for asserting against.
276#[derive(Serialize, Deserialize, Debug)]
277pub struct ResultStatus {
278 pub status: GeneralStatus,
279}
280
281/// The errors received as a result of a failed request
282///
283/// You shouldn't need to create this yourself unless for asserting against.
284#[derive(Serialize, Deserialize, Debug)]
285pub struct BatchRequestErrors {
286 pub error: ErrorMessages,
287}
288
289/// Container for multiple individual DeleteObjectErrorMessage items
290///
291/// You shouldn't need to create this yourself.
292#[derive(Serialize, Deserialize, Debug)]
293pub struct ErrorMessages(Vec<ErrorMessage>);
294
295/// A single error message received as a result of a failed request
296///
297/// You shouldn't need to create this yourself unless for asserting against.
298#[derive(Serialize, Deserialize, Debug)]
299pub struct ErrorMessage {
300 pub message: String,
301}
302
303/// Container for multiple individual BatchAddObject items
304///
305/// You shouldn't need to create this yourself.
306#[derive(Serialize, Deserialize, Debug)]
307pub struct BatchAddObjects(Vec<BatchAddObject>);
308
309/// This is basically the same as the collections::objects variant of an Object,
310/// however there is an extra field which Weaviate polls with a ResultStatus.
311///
312/// There should be no need to manually create this object, it forms part of the response from the
313/// batch add endpoint.
314#[derive(Serialize, Deserialize, Debug)]
315#[serde(rename_all = "camelCase")]
316pub struct BatchAddObject {
317 pub class: String,
318 pub properties: serde_json::Value,
319 #[serde(skip_serializing_if = "Option::is_none")]
320 #[serde(default)]
321 pub id: Option<Uuid>,
322 #[serde(skip_serializing_if = "Option::is_none")]
323 #[serde(default)]
324 pub vector: Option<Vec<f64>>,
325 #[serde(skip_serializing_if = "Option::is_none")]
326 #[serde(default)]
327 pub tenant: Option<String>,
328 #[serde(skip_serializing_if = "Option::is_none")]
329 #[serde(default)]
330 pub creation_time_unix: Option<u64>,
331 #[serde(skip_serializing_if = "Option::is_none")]
332 #[serde(default)]
333 pub last_update_time_unix: Option<u64>,
334 #[serde(skip_serializing_if = "Option::is_none")]
335 #[serde(default)]
336 pub vector_weights: Option<u64>,
337 pub result: ResultStatus,
338}
339
340impl BatchAddObject {
341 /// Transform the BatchAddObject response item to an Object item.
342 pub fn to_object(self) -> Object {
343 Object {
344 class: self.class,
345 properties: self.properties,
346 id: self.id,
347 vector: self.vector,
348 tenant: self.tenant,
349 creation_time_unix: self.creation_time_unix,
350 last_update_time_unix: self.last_update_time_unix,
351 vector_weights: self.vector_weights,
352 additional: None,
353 }
354 }
355}
356
357/// Wrapper for the response of the batch add response payload items for each beacon.
358///
359/// There should be no need to make this manually.
360#[derive(Serialize, Deserialize, Debug)]
361pub struct BatchAddReferencesResponse(pub Vec<BatchAddReferenceResponse>);
362
363/// An individual item received as part of the batch add response payload.
364///
365/// There should be no need to make this manually.
366#[derive(Serialize, Deserialize, Debug)]
367pub struct BatchAddReferenceResponse {
368 result: BatchAddReferenceResult,
369}
370
371/// The response field of the BatchAddReferenceResponse
372///
373/// There should be no need to make this manually.
374#[derive(Serialize, Deserialize, Debug)]
375pub struct BatchAddReferenceResult {
376 pub status: GeneralStatus,
377 #[serde(default)]
378 pub errors: Option<BatchRequestErrors>,
379}