@@ -258,6 +258,78 @@ describe("Documents tests", () => {
258258 expect ( documentsGet . results [ 0 ] ) . not . toHaveProperty ( "_vectors" ) ;
259259 } ) ;
260260
261+ test ( `${ permission } key: Get documents with sorting by single field` , async ( ) => {
262+ const client = await getClient ( permission ) ;
263+
264+ await client
265+ . index ( indexPk . uid )
266+ . updateSortableAttributes ( [ "id" ] )
267+ . waitTask ( ) ;
268+
269+ await client . index ( indexPk . uid ) . addDocuments ( dataset ) . waitTask ( ) ;
270+
271+ const documents = await client . index ( indexPk . uid ) . getDocuments < Book > ( {
272+ sort : [ "id:asc" ] ,
273+ } ) ;
274+
275+ expect ( documents . results . length ) . toEqual ( dataset . length ) ;
276+ // Verify documents are sorted by id in ascending order
277+ const ids = documents . results . map ( ( doc ) => doc . id ) ;
278+ const sortedIds = [ ...ids ] . sort ( ( a , b ) => a - b ) ;
279+ expect ( ids ) . toEqual ( sortedIds ) ;
280+ } ) ;
281+
282+ test ( `${ permission } key: Get documents with sorting by multiple fields` , async ( ) => {
283+ const client = await getClient ( permission ) ;
284+
285+ await client
286+ . index ( indexPk . uid )
287+ . updateSortableAttributes ( [ "id" , "title" ] )
288+ . waitTask ( ) ;
289+
290+ await client . index ( indexPk . uid ) . addDocuments ( dataset ) . waitTask ( ) ;
291+
292+ const documents = await client . index ( indexPk . uid ) . getDocuments < Book > ( {
293+ sort : [ "id:desc" , "title:asc" ] ,
294+ } ) ;
295+
296+ expect ( documents . results . length ) . toEqual ( dataset . length ) ;
297+ // Verify documents are sorted by id in descending order, then by title ascending
298+ const ids = documents . results . map ( ( doc ) => doc . id ) ;
299+ const sortedIds = [ ...ids ] . sort ( ( a , b ) => b - a ) ;
300+ expect ( ids ) . toEqual ( sortedIds ) ;
301+ } ) ;
302+
303+ test ( `${ permission } key: Get documents with empty sort array` , async ( ) => {
304+ const client = await getClient ( permission ) ;
305+
306+ await client
307+ . index ( indexPk . uid )
308+ . updateSortableAttributes ( [ "id" ] )
309+ . waitTask ( ) ;
310+
311+ await client . index ( indexPk . uid ) . addDocuments ( dataset ) . waitTask ( ) ;
312+
313+ const documents = await client . index ( indexPk . uid ) . getDocuments < Book > ( {
314+ sort : [ ] ,
315+ } ) ;
316+
317+ expect ( documents . results . length ) . toEqual ( dataset . length ) ;
318+ // Should return documents in default order (no specific sorting)
319+ } ) ;
320+
321+ test ( `${ permission } key: Get documents with sorting should trigger error for non-sortable attribute` , async ( ) => {
322+ const client = await getClient ( permission ) ;
323+
324+ await client . index ( indexPk . uid ) . addDocuments ( dataset ) . waitTask ( ) ;
325+
326+ await assert . rejects (
327+ client . index ( indexPk . uid ) . getDocuments ( { sort : [ "title:asc" ] } ) ,
328+ Error ,
329+ / A t t r i b u t e ` t i t l e ` i s n o t s o r t a b l e / ,
330+ ) ;
331+ } ) ;
332+
261333 test ( `${ permission } key: Replace documents from index that has NO primary key` , async ( ) => {
262334 const client = await getClient ( permission ) ;
263335 await client . index ( indexNoPk . uid ) . addDocuments ( dataset ) . waitTask ( ) ;
0 commit comments