diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 6bdef025..d4786996 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -142,6 +142,13 @@ get_documents_post_1: |- .execute::() .await .unwrap(); +get_documents_by_ids_1: |- + let index = client.index("books"); + let documents: DocumentsResults = DocumentsQuery::new(&index) + .with_ids(["1", "2"]) // retrieve documents by IDs + .execute::() + .await + .unwrap(); add_or_replace_documents_1: |- let task: TaskInfo = client .index("movies") diff --git a/src/documents.rs b/src/documents.rs index 63a36caa..90beefee 100644 --- a/src/documents.rs +++ b/src/documents.rs @@ -202,6 +202,15 @@ pub struct DocumentsQuery<'a, Http: HttpClient> { /// Read the [dedicated guide](https://www.meilisearch.com/docs/learn/filtering_and_sorting) to learn the syntax. #[serde(skip_serializing_if = "Option::is_none")] pub filter: Option<&'a str>, + + /// Retrieve documents by their IDs. + /// + /// When `ids` is provided, the SDK will call the `/documents/fetch` endpoint with a POST request. + /// + /// Note: IDs are represented as strings to keep consistency with [`Index::get_document`]. If your IDs + /// are numeric, pass them as strings (e.g., `"1"`, `"2"`). + #[serde(skip_serializing_if = "Option::is_none")] + pub ids: Option>, } impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> { @@ -214,6 +223,7 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> { fields: None, sort: None, filter: None, + ids: None, } } @@ -314,6 +324,29 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> { self } + /// Specify a list of document IDs to retrieve. + /// + /// # Example + /// + /// ``` + /// # use meilisearch_sdk::{client::*, indexes::*, documents::*}; + /// # use serde::{Deserialize, Serialize}; + /// # + /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); + /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap(); + /// let index = client.index("get_documents_by_ids_example"); + /// let mut query = DocumentsQuery::new(&index); + /// query.with_ids(["1", "2"]); + /// ``` + pub fn with_ids( + &mut self, + ids: impl IntoIterator, + ) -> &mut DocumentsQuery<'a, Http> { + self.ids = Some(ids.into_iter().collect()); + self + } + /// Execute the get documents query. /// /// # Example @@ -468,6 +501,19 @@ mod tests { Ok(()) } + #[meilisearch_test] + async fn test_get_documents_by_ids(client: Client, index: Index) -> Result<(), Error> { + setup_test_index(&client, &index).await?; + + let documents = DocumentsQuery::new(&index) + .with_ids(["1", "3"]) // retrieve by IDs + .execute::() + .await?; + + assert_eq!(documents.results.len(), 2); + Ok(()) + } + #[meilisearch_test] async fn test_delete_documents_with(client: Client, index: Index) -> Result<(), Error> { setup_test_index(&client, &index).await?; diff --git a/src/indexes.rs b/src/indexes.rs index febf25fa..b9196b54 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -517,7 +517,7 @@ impl Index { &self, documents_query: &DocumentsQuery<'_, Http>, ) -> Result, Error> { - if documents_query.filter.is_some() { + if documents_query.filter.is_some() || documents_query.ids.is_some() { let url = format!("{}/indexes/{}/documents/fetch", self.client.host, self.uid); return self .client