|
7 | 7 | use crate::{
|
8 | 8 | Client, Error, execute_wrapper,
|
9 | 9 | models::{DocumentIndexParameters, SearchResult},
|
| 10 | + traits, |
10 | 11 | };
|
11 | 12 | use serde::{Serialize, de::DeserializeOwned};
|
12 | 13 | use typesense_codegen::{
|
|
63 | 64 | execute_wrapper!(self, documents_api::index_document, params)
|
64 | 65 | }
|
65 | 66 |
|
66 |
| - /// Creates a new document in the collection. |
67 |
| - /// |
68 |
| - /// Fails if a document with the same ID already exists. If the document has an `id` field |
69 |
| - /// of type `string`, it will be used as the document's ID. Otherwise, Typesense will |
70 |
| - /// auto-generate an ID. The newly indexed document is returned. |
71 |
| - /// |
72 |
| - /// # Arguments |
73 |
| - /// * `document` - A serializable struct or a `serde_json::Value` representing the document to create. |
74 |
| - /// * `params` - Optional parameters like `dirty_values`. |
75 |
| - pub async fn create<U: Serialize>( |
76 |
| - &self, |
77 |
| - document: U, |
78 |
| - params: Option<DocumentIndexParameters>, |
79 |
| - ) -> Result<D, Error<documents_api::IndexDocumentError>> { |
80 |
| - let doc_value = serde_json::to_value(document)?; |
81 |
| - let result_value = self.index(doc_value, "create", params).await?; |
82 |
| - serde_json::from_value(result_value).map_err(Error::from) |
83 |
| - } |
84 |
| - |
85 |
| - /// Creates a new document or updates an existing one if an ID match is found. |
86 |
| - /// |
87 |
| - /// This method requires the full document to be sent. For partial updates, use |
88 |
| - /// `collection().document("...").update()`. The indexed document is returned. |
89 |
| - /// |
90 |
| - /// # Arguments |
91 |
| - /// * `document` - A serializable struct or a `serde_json::Value` representing the document to upsert. |
92 |
| - /// * `params` - Optional parameters like `dirty_values`. |
93 |
| - pub async fn upsert<U: Serialize>( |
94 |
| - &self, |
95 |
| - document: U, |
96 |
| - params: Option<DocumentIndexParameters>, |
97 |
| - ) -> Result<D, Error<documents_api::IndexDocumentError>> { |
98 |
| - let doc_value = serde_json::to_value(document)?; |
99 |
| - let result_value = self.index(doc_value, "upsert", params).await?; |
100 |
| - serde_json::from_value(result_value).map_err(Error::from) |
101 |
| - } |
102 |
| - |
103 | 67 | // --- Bulk Operation Methods ---
|
104 | 68 |
|
105 | 69 | /// Imports a batch of documents in JSONL format.
|
@@ -164,25 +128,6 @@ where
|
164 | 128 | execute_wrapper!(self, documents_api::delete_documents, params)
|
165 | 129 | }
|
166 | 130 |
|
167 |
| - /// Updates a batch of documents matching a specific filter condition. |
168 |
| - /// |
169 |
| - /// # Arguments |
170 |
| - /// * `document` - A serializable struct or a `serde_json::Value` containing the fields to update. |
171 |
| - /// * `params` - A `UpdateDocumentsParameters` describing the conditions for updating documents. |
172 |
| - pub async fn update<U: Serialize>( |
173 |
| - &self, |
174 |
| - document: U, |
175 |
| - params: UpdateDocumentsParameters, |
176 |
| - ) -> Result<raw_models::UpdateDocuments200Response, Error<documents_api::UpdateDocumentsError>> |
177 |
| - { |
178 |
| - let params = documents_api::UpdateDocumentsParams { |
179 |
| - collection_name: self.collection_name.to_owned(), |
180 |
| - filter_by: params.filter_by, |
181 |
| - body: serde_json::to_value(document)?, |
182 |
| - }; |
183 |
| - execute_wrapper!(self, documents_api::update_documents, params) |
184 |
| - } |
185 |
| - |
186 | 131 | /// Searches for documents in the collection that match the given criteria.
|
187 | 132 | /// The search results will have their `document` field deserialized into type `D`.
|
188 | 133 | ///
|
@@ -271,3 +216,64 @@ where
|
271 | 216 | execute_wrapper!(self, documents_api::search_collection, search_params)
|
272 | 217 | }
|
273 | 218 | }
|
| 219 | + |
| 220 | +impl<'c, 'n, D> Documents<'c, 'n, D> |
| 221 | +where |
| 222 | + D: traits::Document, |
| 223 | +{ |
| 224 | + /// Creates a new document in the collection. |
| 225 | + /// |
| 226 | + /// Fails if a document with the same ID already exists. If the document has an `id` field |
| 227 | + /// of type `string`, it will be used as the document's ID. Otherwise, Typesense will |
| 228 | + /// auto-generate an ID. The newly indexed document is returned. |
| 229 | + /// |
| 230 | + /// # Arguments |
| 231 | + /// * `document` - A document struct to create. |
| 232 | + /// * `params` - Optional parameters like `dirty_values`. |
| 233 | + pub async fn create( |
| 234 | + &self, |
| 235 | + document: &D, |
| 236 | + params: Option<DocumentIndexParameters>, |
| 237 | + ) -> Result<D, Error<documents_api::IndexDocumentError>> { |
| 238 | + let doc_value = serde_json::to_value(document)?; |
| 239 | + let result_value = self.index(doc_value, "create", params).await?; |
| 240 | + serde_json::from_value(result_value).map_err(Error::from) |
| 241 | + } |
| 242 | + |
| 243 | + /// Creates a new document or updates an existing one if an ID match is found. |
| 244 | + /// |
| 245 | + /// This method requires the full document to be sent. For partial updates, use |
| 246 | + /// `collection().document("...").update()`. The indexed document is returned. |
| 247 | + /// |
| 248 | + /// # Arguments |
| 249 | + /// * `document` - A document struct to upsert. |
| 250 | + /// * `params` - Optional parameters like `dirty_values`. |
| 251 | + pub async fn upsert( |
| 252 | + &self, |
| 253 | + document: &D, |
| 254 | + params: Option<DocumentIndexParameters>, |
| 255 | + ) -> Result<D, Error<documents_api::IndexDocumentError>> { |
| 256 | + let doc_value = serde_json::to_value(document)?; |
| 257 | + let result_value = self.index(doc_value, "upsert", params).await?; |
| 258 | + serde_json::from_value(result_value).map_err(Error::from) |
| 259 | + } |
| 260 | + |
| 261 | + /// Updates a batch of documents matching a specific filter condition. |
| 262 | + /// |
| 263 | + /// # Arguments |
| 264 | + /// * `document` - A struct containing the fields to update. |
| 265 | + /// * `params` - A `UpdateDocumentsParameters` describing the conditions for updating documents. |
| 266 | + pub async fn update( |
| 267 | + &self, |
| 268 | + document: &D::Partial, |
| 269 | + params: UpdateDocumentsParameters, |
| 270 | + ) -> Result<raw_models::UpdateDocuments200Response, Error<documents_api::UpdateDocumentsError>> |
| 271 | + { |
| 272 | + let params = documents_api::UpdateDocumentsParams { |
| 273 | + collection_name: self.collection_name.to_owned(), |
| 274 | + filter_by: params.filter_by, |
| 275 | + body: document, |
| 276 | + }; |
| 277 | + execute_wrapper!(self, documents_api::update_documents, params) |
| 278 | + } |
| 279 | +} |
0 commit comments