Skip to content

Commit aa5c74f

Browse files
committed
Add method to get documents by ID
1 parent e90a3c2 commit aa5c74f

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

.code-samples.meilisearch.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ get_documents_post_1: |-
142142
.execute::<Movies>()
143143
.await
144144
.unwrap();
145+
get_documents_by_ids_1: |-
146+
let index = client.index("books");
147+
let documents: DocumentsResults = DocumentsQuery::new(&index)
148+
.with_ids(["1", "2"]) // retrieve documents by IDs
149+
.execute::<Movies>()
150+
.await
151+
.unwrap();
145152
add_or_replace_documents_1: |-
146153
let task: TaskInfo = client
147154
.index("movies")

src/documents.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ pub struct DocumentsQuery<'a, Http: HttpClient> {
202202
/// Read the [dedicated guide](https://www.meilisearch.com/docs/learn/filtering_and_sorting) to learn the syntax.
203203
#[serde(skip_serializing_if = "Option::is_none")]
204204
pub filter: Option<&'a str>,
205+
206+
/// Retrieve documents by their IDs.
207+
///
208+
/// When `ids` is provided, the SDK will call the `/documents/fetch` endpoint with a POST request.
209+
///
210+
/// Note: IDs are represented as strings to keep consistency with [`Index::get_document`]. If your IDs
211+
/// are numeric, pass them as strings (e.g., `"1"`, `"2"`).
212+
#[serde(skip_serializing_if = "Option::is_none")]
213+
pub ids: Option<Vec<&'a str>>,
205214
}
206215

207216
impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
@@ -214,6 +223,7 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
214223
fields: None,
215224
sort: None,
216225
filter: None,
226+
ids: None,
217227
}
218228
}
219229

@@ -314,6 +324,29 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
314324
self
315325
}
316326

327+
/// Specify a list of document IDs to retrieve.
328+
///
329+
/// # Example
330+
///
331+
/// ```
332+
/// # use meilisearch_sdk::{client::*, indexes::*, documents::*};
333+
/// # use serde::{Deserialize, Serialize};
334+
/// #
335+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
336+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
337+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
338+
/// let index = client.index("get_documents_by_ids_example");
339+
/// let mut query = DocumentsQuery::new(&index);
340+
/// query.with_ids(["1", "2"]);
341+
/// ```
342+
pub fn with_ids(
343+
&mut self,
344+
ids: impl IntoIterator<Item = &'a str>,
345+
) -> &mut DocumentsQuery<'a, Http> {
346+
self.ids = Some(ids.into_iter().collect());
347+
self
348+
}
349+
317350
/// Execute the get documents query.
318351
///
319352
/// # Example
@@ -468,6 +501,19 @@ mod tests {
468501
Ok(())
469502
}
470503

504+
#[meilisearch_test]
505+
async fn test_get_documents_by_ids(client: Client, index: Index) -> Result<(), Error> {
506+
setup_test_index(&client, &index).await?;
507+
508+
let documents = DocumentsQuery::new(&index)
509+
.with_ids(["1", "3"]) // retrieve by IDs
510+
.execute::<MyObject>()
511+
.await?;
512+
513+
assert_eq!(documents.results.len(), 2);
514+
Ok(())
515+
}
516+
471517
#[meilisearch_test]
472518
async fn test_delete_documents_with(client: Client, index: Index) -> Result<(), Error> {
473519
setup_test_index(&client, &index).await?;

src/indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl<Http: HttpClient> Index<Http> {
517517
&self,
518518
documents_query: &DocumentsQuery<'_, Http>,
519519
) -> Result<DocumentsResults<T>, Error> {
520-
if documents_query.filter.is_some() {
520+
if documents_query.filter.is_some() || documents_query.ids.is_some() {
521521
let url = format!("{}/indexes/{}/documents/fetch", self.client.host, self.uid);
522522
return self
523523
.client

0 commit comments

Comments
 (0)