Skip to content

Commit af88cd3

Browse files
committed
Fix merge conflict
2 parents 95f86a2 + 70f5424 commit af88cd3

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

src/documents.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ pub struct DocumentsResults<T> {
1010
}
1111

1212
#[derive(Debug, Clone, Serialize)]
13+
pub struct DocumentQuery<'a> {
14+
/// The fields that should appear in the documents. By default all of the fields are present.
15+
#[serde(skip_serializing_if = "Option::is_none")]
16+
pub fields: Option<Vec<&'a str>>,
17+
}
18+
19+
#[derive(Debug, Clone, Serialize)]
20+
1321
pub struct DocumentsQuery<'a> {
1422
#[serde(skip_serializing)]
1523
pub index: &'a Index,
@@ -31,6 +39,8 @@ pub struct DocumentsQuery<'a> {
3139
#[serde(skip_serializing_if = "Option::is_none")]
3240
pub limit: Option<usize>,
3341

42+
/// The fields that should appear in the documents. By default all of the fields are present.
43+
#[serde(skip_serializing_if = "Option::is_none")]
3444
pub fields: Option<Vec<&'a str>>,
3545
}
3646

@@ -55,8 +65,7 @@ impl<'a> DocumentsQuery<'a> {
5565
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
5666
/// #
5767
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
58-
///
59-
/// let index = client.index("documents_query_offset");
68+
/// let index = client.index("my_index");
6069
///
6170
/// let mut documents_query = DocumentsQuery::new(&index);
6271
///
@@ -78,12 +87,11 @@ impl<'a> DocumentsQuery<'a> {
7887
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
7988
/// #
8089
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
81-
///
82-
/// let index = client.index("documents_query_offset");
90+
/// let index = client.index("my_index");
8391
///
8492
/// let mut documents_query = DocumentsQuery::new(&index);
8593
///
86-
/// documents_query.with_offset(1);
94+
/// documents_query.with_limit(1);
8795
/// ```
8896
pub fn with_limit(&mut self, limit: usize) -> &mut DocumentsQuery<'a> {
8997
self.limit = Some(limit);
@@ -101,7 +109,7 @@ impl<'a> DocumentsQuery<'a> {
101109
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
102110
/// #
103111
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
104-
/// let index = client.index("documents_query_offset");
112+
/// let index = client.index("my_index");
105113
///
106114
/// let mut documents_query = DocumentsQuery::new(&index);
107115
///
@@ -115,7 +123,7 @@ impl<'a> DocumentsQuery<'a> {
115123
self
116124
}
117125

118-
/// Specify the limit.
126+
/// Execute the get documents query.
119127
///
120128
/// # Example
121129
///

src/indexes.rs

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
client::Client,
3-
documents::{DocumentsQuery, DocumentsResults},
3+
documents::{DocumentQuery, DocumentsQuery, DocumentsResults},
44
errors::Error,
55
request::*,
66
search::*,
@@ -285,49 +285,52 @@ impl Index {
285285
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
286286
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
287287
/// #
288-
/// #[derive(Serialize, Deserialize, Debug)]
289-
/// # #[derive(PartialEq)]
288+
/// #[derive(Serialize, Debug)]
290289
/// struct Movie {
291290
/// name: String,
292291
/// description: String,
292+
/// age: Option<usize>
293+
/// }
294+
///
295+
/// #[derive(Deserialize, Debug, PartialEq)]
296+
/// struct ReturnedMovie {
297+
/// name: String,
298+
/// description: String
293299
/// }
294300
///
295301
///
296302
/// # futures::executor::block_on(async move {
297303
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
298304
/// 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();
300306
///
301307
/// // 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();
303309
///
304-
/// assert_eq!(interstellar, Movie {
310+
/// assert_eq!(interstellar, ReturnedMovie {
305311
/// 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."),
307313
/// });
308314
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
309315
/// # });
310316
/// ```
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
322330
}
323331

324332
/// Get [Document]s by batch.
325333
///
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-
///
331334
/// # Example
332335
///
333336
/// ```
@@ -368,10 +371,42 @@ impl Index {
368371
request::<(), DocumentsResults<T>>(&url, &self.client.api_key, Method::Get(()), 200).await
369372
}
370373

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+
/// ```
371407
pub async fn get_documents_with<T: DeserializeOwned + 'static>(
372408
&self,
373409
documents_query: &DocumentsQuery<'_>,
374-
) -> Result<DocumentsResults<T>, Error> {
375410
let url = format!("{}/indexes/{}/documents", self.client.host, self.uid);
376411
request::<&DocumentsQuery, DocumentsResults<T>>(
377412
&url,

0 commit comments

Comments
 (0)