Skip to content

Commit 6b91c19

Browse files
committed
feat: Add support for sorting on the documents API.
1 parent 7f13a50 commit 6b91c19

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/index/test_index_document_meilisearch.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,43 @@ def test_get_documents_sort_fields(index_with_documents):
282282
assert resp_doc.release_date == expected_doc["release_date"]
283283

284284

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+
285322
def test_get_documents_filter(index_with_documents):
286323
index = index_with_documents()
287324
response = index.update_filterable_attributes(["genre"])

0 commit comments

Comments
 (0)