@@ -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