|
1 | 1 | use crate::{ |
2 | 2 | client::Client, |
3 | | - documents::{DocumentsQuery, DocumentsResults}, |
| 3 | + documents::{DocumentQuery, DocumentsQuery, DocumentsResults}, |
4 | 4 | errors::Error, |
5 | 5 | request::*, |
6 | 6 | search::*, |
@@ -285,49 +285,52 @@ impl Index { |
285 | 285 | /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700"); |
286 | 286 | /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); |
287 | 287 | /// # |
288 | | - /// #[derive(Serialize, Deserialize, Debug)] |
289 | | - /// # #[derive(PartialEq)] |
| 288 | + /// #[derive(Serialize, Debug)] |
290 | 289 | /// struct Movie { |
291 | 290 | /// name: String, |
292 | 291 | /// description: String, |
| 292 | + /// age: Option<usize> |
| 293 | + /// } |
| 294 | + /// |
| 295 | + /// #[derive(Deserialize, Debug, PartialEq)] |
| 296 | + /// struct ReturnedMovie { |
| 297 | + /// name: String, |
| 298 | + /// description: String |
293 | 299 | /// } |
294 | 300 | /// |
295 | 301 | /// |
296 | 302 | /// # futures::executor::block_on(async move { |
297 | 303 | /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY); |
298 | 304 | /// let movies = client.index("get_document"); |
299 | | - /// # movies.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); |
| 305 | + /// # movies.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage."), age: Some(1)}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); |
300 | 306 | /// |
301 | 307 | /// // retrieve a document (you have to put the document in the index before) |
302 | | - /// let interstellar = movies.get_document::<Movie>("Interstellar").await.unwrap(); |
| 308 | + /// let interstellar = movies.get_document::<ReturnedMovie>("Interstellar", Some(["name", "description"].to_vec())).await.unwrap(); |
303 | 309 | /// |
304 | | - /// assert_eq!(interstellar, Movie { |
| 310 | + /// assert_eq!(interstellar, ReturnedMovie { |
305 | 311 | /// name: String::from("Interstellar"), |
306 | | - /// description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.") |
| 312 | + /// description: String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage."), |
307 | 313 | /// }); |
308 | 314 | /// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); |
309 | 315 | /// # }); |
310 | 316 | /// ``` |
311 | | - pub async fn get_document<T: 'static + DeserializeOwned>(&self, uid: &str) -> Result<T, Error> { |
312 | | - request::<(), T>( |
313 | | - &format!( |
314 | | - "{}/indexes/{}/documents/{}", |
315 | | - self.client.host, self.uid, uid |
316 | | - ), |
317 | | - &self.client.api_key, |
318 | | - Method::Get(()), |
319 | | - 200, |
320 | | - ) |
321 | | - .await |
| 317 | + pub async fn get_document<T: 'static + DeserializeOwned>( |
| 318 | + &self, |
| 319 | + document_id: &str, |
| 320 | + fields: Option<Vec<&str>>, |
| 321 | + ) -> Result<T, Error> { |
| 322 | + let url = format!( |
| 323 | + "{}/indexes/{}/documents/{}", |
| 324 | + self.client.host, self.uid, document_id |
| 325 | + ); |
| 326 | + |
| 327 | + let query = DocumentQuery { fields }; |
| 328 | + |
| 329 | + request::<&DocumentQuery, T>(&url, &self.client.api_key, Method::Get(&query), 200).await |
322 | 330 | } |
323 | 331 |
|
324 | 332 | /// Get [Document]s by batch. |
325 | 333 | /// |
326 | | - /// Using the optional parameters offset and limit, you can browse through all your documents. |
327 | | - /// If None, offset will be set to 0, limit to 20, and all attributes will be retrieved. |
328 | | - /// |
329 | | - /// *Note: Documents are ordered by Meilisearch depending on the hash of their id.* |
330 | | - /// |
331 | 334 | /// # Example |
332 | 335 | /// |
333 | 336 | /// ``` |
@@ -368,10 +371,42 @@ impl Index { |
368 | 371 | request::<(), DocumentsResults<T>>(&url, &self.client.api_key, Method::Get(()), 200).await |
369 | 372 | } |
370 | 373 |
|
| 374 | + /// Get [Document]s by batch with parameters. |
| 375 | + /// ``` |
| 376 | + /// use serde::{Serialize, Deserialize}; |
| 377 | + /// |
| 378 | + /// # use meilisearch_sdk::{client::*, indexes::*, documents::*}; |
| 379 | + /// # |
| 380 | + /// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700"); |
| 381 | + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); |
| 382 | + /// # |
| 383 | + /// |
| 384 | + /// #[derive(Serialize, Deserialize, Debug)] |
| 385 | + /// # #[derive(PartialEq)] |
| 386 | + /// struct Movie { |
| 387 | + /// name: String, |
| 388 | + /// description: String, |
| 389 | + /// } |
| 390 | + /// |
| 391 | + /// |
| 392 | + /// # futures::executor::block_on(async move { |
| 393 | + /// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY); |
| 394 | + /// let movie_index = client.index("get_documents"); |
| 395 | + /// |
| 396 | + /// # movie_index.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); |
| 397 | + /// |
| 398 | + /// let mut query = DocumentsQuery::new(&movie_index); |
| 399 | + /// query.with_limit(1); |
| 400 | + /// // retrieve movies (you have to put some movies in the index before) |
| 401 | + /// let movies = movie_index.get_documents_with::<Movie>(&query).await.unwrap(); |
| 402 | + /// |
| 403 | + /// assert!(movies.results.len() == 1); |
| 404 | + /// # movie_index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); |
| 405 | + /// # }); |
| 406 | + /// ``` |
371 | 407 | pub async fn get_documents_with<T: DeserializeOwned + 'static>( |
372 | 408 | &self, |
373 | 409 | documents_query: &DocumentsQuery<'_>, |
374 | | - ) -> Result<DocumentsResults<T>, Error> { |
375 | 410 | let url = format!("{}/indexes/{}/documents", self.client.host, self.uid); |
376 | 411 | request::<&DocumentsQuery, DocumentsResults<T>>( |
377 | 412 | &url, |
|
0 commit comments