From 46ce5fb2d59e0017591b6dea478150cfa394d9f9 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Mon, 9 Nov 2026 08:35:44 -0700 Subject: [PATCH] Keep _geoPoint arguments intact when sort is a comma-separated string Co-Authored-By: Claude Signed-off-by: Arpit Jain --- meilisearch/_utils.py | 23 +++++++++++++++++++ meilisearch/index.py | 4 ++-- .../index/test_index_document_meilisearch.py | 19 +++++++++++++++ tests/test_utils.py | 18 ++++++++++++++- 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/meilisearch/_utils.py b/meilisearch/_utils.py index ae915c85..35953921 100644 --- a/meilisearch/_utils.py +++ b/meilisearch/_utils.py @@ -40,3 +40,26 @@ def iso_to_date_time(iso_date: datetime | str | None) -> datetime | None: reduce = len(split[1]) - 6 reduced = f"{split[0]}.{split[1][:-reduce]}Z" return datetime.strptime(reduced, "%Y-%m-%dT%H:%M:%S.%fZ") + + +def split_sort_string(sort: str) -> list[str]: + """Split a comma-separated sort string into its individual sort expressions. + + Commas inside parentheses belong to the expression, as in + ``_geoPoint(48.8561,2.2978):asc``, so only top level commas separate terms. + """ + terms: list[str] = [] + current: list[str] = [] + depth = 0 + for char in sort: + if char == "(": + depth += 1 + elif char == ")": + depth = max(depth - 1, 0) + if char == "," and depth == 0: + terms.append("".join(current)) + current = [] + continue + current.append(char) + terms.append("".join(current)) + return [term.strip() for term in terms if term.strip()] diff --git a/meilisearch/index.py b/meilisearch/index.py index 82a74fed..1da2709c 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -9,7 +9,7 @@ from camel_converter import to_snake from meilisearch._httprequests import HttpRequests -from meilisearch._utils import iso_to_date_time +from meilisearch._utils import iso_to_date_time, split_sort_string from meilisearch.config import Config from meilisearch.errors import version_error_hint_message from meilisearch.models.document import Document, DocumentsResults, FieldsResults @@ -505,7 +505,7 @@ def get_documents(self, parameters: MutableMapping[str, Any] | None = None) -> D # convert comma-separated sort string to list sort = parameters.get("sort") if isinstance(sort, str): - parameters["sort"] = [s.strip() for s in sort.split(",") if s.strip()] + parameters["sort"] = split_sort_string(sort) response = self.http.post( f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}/fetch", diff --git a/tests/index/test_index_document_meilisearch.py b/tests/index/test_index_document_meilisearch.py index 2448e501..98c87a79 100644 --- a/tests/index/test_index_document_meilisearch.py +++ b/tests/index/test_index_document_meilisearch.py @@ -338,6 +338,25 @@ def test_get_documents_sort_formats(index_with_documents, sort_param): assert resp_doc.release_date == expected_doc["release_date"] +def test_get_documents_sort_string_keeps_geo_point_arguments(empty_index): + """A _geoPoint sort passed as a comma-separated string must stay one term.""" + index = empty_index() + + task = index.update_settings({"sortableAttributes": ["_geo"]}) + index.wait_for_task(task.task_uid) + + documents = [ + {"id": 1, "_geo": {"lat": 48.8561, "lng": 2.2978}}, + {"id": 2, "_geo": {"lat": 45.7640, "lng": 4.8357}}, + ] + task = index.add_documents(documents) + index.wait_for_task(task.task_uid) + + response = index.get_documents({"sort": "_geoPoint(48.8561,2.2978):asc"}) + + assert [doc.id for doc in response.results] == [1, 2] + + def test_get_documents_filter(index_with_documents): index = index_with_documents() response = index.update_filterable_attributes(["genre"]) diff --git a/tests/test_utils.py b/tests/test_utils.py index 4ca688ce..60536dd7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -2,7 +2,7 @@ import pytest -from meilisearch._utils import is_pydantic_2, iso_to_date_time +from meilisearch._utils import is_pydantic_2, iso_to_date_time, split_sort_string def test_is_pydantic_2(): @@ -33,6 +33,22 @@ def test_iso_to_date_time_invalid_format(): iso_to_date_time("2023-07-13T23:37:20Z") +@pytest.mark.parametrize( + "sort, expected", + [ + ("rating:desc, release_date:asc", ["rating:desc", "release_date:asc"]), + ("_geoPoint(48.8561,2.2978):asc", ["_geoPoint(48.8561,2.2978):asc"]), + ( + "_geoPoint(48.8561,2.2978):asc, rating:desc", + ["_geoPoint(48.8561,2.2978):asc", "rating:desc"], + ), + ("", []), + ], +) +def test_split_sort_string(sort, expected): + assert split_sort_string(sort) == expected + + # Refactor to use the unified API to toggle experimental features def reset_network_config(client): client.add_or_update_networks(body={"remotes": {}, "leader": None})