Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ get_documents_post_1: |-
.execute::<Movies>()
.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::<Movies>()
.await
.unwrap();
add_or_replace_documents_1: |-
let task: TaskInfo = client
.index("movies")
Expand Down
46 changes: 46 additions & 0 deletions src/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<&'a str>>,
}

impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
Expand All @@ -214,6 +223,7 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
fields: None,
sort: None,
filter: None,
ids: None,
}
}

Expand Down Expand Up @@ -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<Item = &'a str>,
) -> &mut DocumentsQuery<'a, Http> {
self.ids = Some(ids.into_iter().collect());
self
}

/// Execute the get documents query.
///
/// # Example
Expand Down Expand Up @@ -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::<MyObject>()
.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?;
Expand Down
2 changes: 1 addition & 1 deletion src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ impl<Http: HttpClient> Index<Http> {
&self,
documents_query: &DocumentsQuery<'_, Http>,
) -> Result<DocumentsResults<T>, 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
Expand Down