Skip to content

Commit 59971fc

Browse files
Merge #711
711: Add support for sorting on the documents API r=curquiza a=kumarUjjawal # Pull Request ## Related issue Fixes #699 ## What does this PR do? As per the requirements of the [Meilisearch 1.16](https://www.meilisearch.com/blog/meilisearch-1-16?utm_campaign=oss&utm_medium=integrations) the goal was to update the SDK to allow the sorting on the documents API. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added sorting options to document retrieval, allowing results to be ordered by specified attributes when sorting is configured. Supports multi-attribute sorting. Default behavior remains unchanged if no sort is provided. * **Tests** * Added test coverage to verify correct ordering of documents when sorting is applied. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Kumar Ujjawal <ujjawalpathak6@gmail.com>
2 parents 910f9e9 + 0329ee4 commit 59971fc

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/documents.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ pub struct DocumentsQuery<'a, Http: HttpClient> {
190190
#[serde(skip_serializing_if = "Option::is_none")]
191191
pub fields: Option<Vec<&'a str>>,
192192

193+
/// Attributes used to sort the returned documents.
194+
///
195+
/// Available since v1.16 of Meilisearch.
196+
#[serde(skip_serializing_if = "Option::is_none")]
197+
pub sort: Option<Vec<&'a str>>,
198+
193199
/// Filters to apply.
194200
///
195201
/// Available since v1.2 of Meilisearch
@@ -206,6 +212,7 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
206212
offset: None,
207213
limit: None,
208214
fields: None,
215+
sort: None,
209216
filter: None,
210217
}
211218
}
@@ -277,6 +284,31 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
277284
self
278285
}
279286

287+
/// Specify the sort order of the returned documents.
288+
///
289+
/// # Example
290+
///
291+
/// ```
292+
/// # use meilisearch_sdk::{client::*, indexes::*, documents::*};
293+
/// #
294+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
295+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
296+
/// #
297+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
298+
/// let index = client.index("documents_with_sort");
299+
///
300+
/// let mut documents_query = DocumentsQuery::new(&index);
301+
///
302+
/// documents_query.with_sort(["release_date:desc"]);
303+
/// ```
304+
pub fn with_sort(
305+
&mut self,
306+
sort: impl IntoIterator<Item = &'a str>,
307+
) -> &mut DocumentsQuery<'a, Http> {
308+
self.sort = Some(sort.into_iter().collect());
309+
self
310+
}
311+
280312
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut DocumentsQuery<'a, Http> {
281313
self.filter = Some(filter);
282314
self
@@ -538,6 +570,33 @@ mod tests {
538570
Ok(())
539571
}
540572

573+
#[meilisearch_test]
574+
async fn test_get_documents_with_sort(client: Client, index: Index) -> Result<(), Error> {
575+
setup_test_index(&client, &index).await?;
576+
577+
index
578+
.set_sortable_attributes(["id"])
579+
.await?
580+
.wait_for_completion(&client, None, None)
581+
.await?;
582+
583+
let documents = DocumentsQuery::new(&index)
584+
.with_sort(["id:desc"])
585+
.execute::<MyObject>()
586+
.await?;
587+
588+
assert_eq!(
589+
documents.results.first().and_then(|document| document.id),
590+
Some(3)
591+
);
592+
assert_eq!(
593+
documents.results.last().and_then(|document| document.id),
594+
Some(0)
595+
);
596+
597+
Ok(())
598+
}
599+
541600
#[meilisearch_test]
542601
async fn test_get_documents_with_error_hint() -> Result<(), Error> {
543602
let meilisearch_url = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");

0 commit comments

Comments
 (0)