@@ -245,6 +245,80 @@ def test_get_documents_offset_optional_params_list_of_fields(index_with_document
245245 assert response_offset_limit.results[0].genre == response.results[1].genre
246246
247247
248+ def test_get_documents_sort_fields(index_with_documents):
249+ """Tests getting documents sorted by fields."""
250+ index = index_with_documents()
251+
252+ # Make fields sortable: include 'rating' and 'release_date'
253+ sortable_attributes = ["rating", "release_date"]
254+ task = index.update_sortable_attributes(sortable_attributes)
255+ index.wait_for_task(task.task_uid) # wait until sortable attributes are set
256+
257+ documents = [
258+ {"id": 1, "title": "Inception", "release_date": "2010-07-16", "rating": 8.8},
259+ {"id": 2, "title": "Interstellar", "release_date": "2014-11-07", "rating": 8.6},
260+ {"id": 3, "title": "Parasite", "release_date": "2019-05-30", "rating": 8.6},
261+ {"id": 4, "title": "The Matrix", "release_date": "1999-03-31", "rating": 8.7},
262+ {"id": 5, "title": "The Dark Knight", "release_date": "2008-07-18", "rating": 9.0},
263+ ]
264+
265+ # Add documents
266+ task = index.add_documents(documents)
267+ index.wait_for_task(task.task_uid)
268+
269+ params = {
270+ "limit": 5,
271+ "fields": ["id", "title", "release_date", "rating"],
272+ "sort": ["rating:desc", "release_date:asc"],
273+ }
274+ response = index.get_documents(params)
275+
276+ # prepare expected order
277+ sorted_docs = sorted(documents, key=lambda d: (-d["rating"], d["release_date"]))
278+
279+ for resp_doc, expected_doc in zip(response.results, sorted_docs):
280+ assert resp_doc.id == expected_doc["id"]
281+ assert resp_doc.rating == expected_doc["rating"]
282+ assert resp_doc.release_date == expected_doc["release_date"]
283+
284+
285+ @pytest.mark.parametrize(
286+ "sort_param",
287+ [
288+ ["rating:desc", "release_date:asc"], # list format
289+ "rating:desc, release_date:asc", # comma-separated string
290+ ],
291+ )
292+ def test_get_documents_sort_formats(index_with_documents, sort_param):
293+ index = index_with_documents()
294+
295+ # Make fields sortable
296+ sortable_attributes = ["rating", "release_date"]
297+ task = index.update_sortable_attributes(sortable_attributes)
298+ index.wait_for_task(task.task_uid)
299+
300+ documents = [
301+ {"id": 1, "title": "Inception", "release_date": "2010-07-16", "rating": 8.8},
302+ {"id": 2, "title": "Interstellar", "release_date": "2014-11-07", "rating": 8.6},
303+ {"id": 3, "title": "Parasite", "release_date": "2019-05-30", "rating": 8.6},
304+ {"id": 4, "title": "The Matrix", "release_date": "1999-03-31", "rating": 8.7},
305+ {"id": 5, "title": "The Dark Knight", "release_date": "2008-07-18", "rating": 9.0},
306+ ]
307+
308+ task = index.add_documents(documents)
309+ index.wait_for_task(task.task_uid)
310+
311+ params = {"limit": 5, "fields": ["id", "title", "release_date", "rating"], "sort": sort_param}
312+ response = index.get_documents(params)
313+
314+ sorted_docs = sorted(documents, key=lambda d: (-d["rating"], d["release_date"]))
315+
316+ for resp_doc, expected_doc in zip(response.results, sorted_docs):
317+ assert resp_doc.id == expected_doc["id"]
318+ assert resp_doc.rating == expected_doc["rating"]
319+ assert resp_doc.release_date == expected_doc["release_date"]
320+
321+
248322def test_get_documents_filter(index_with_documents):
249323 index = index_with_documents()
250324 response = index.update_filterable_attributes(["genre"])
0 commit comments