From 82cdb1bef7930f61e022e2af4909604d18df1263 Mon Sep 17 00:00:00 2001 From: Srija Talamarla Date: Mon, 9 Mar 2026 19:12:22 +0000 Subject: [PATCH 1/2] Add sort_by_date_modified parameter to search endpoints --- src/routers/search_router.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/routers/search_router.py b/src/routers/search_router.py index fc145fa5..8f6de675 100644 --- a/src/routers/search_router.py +++ b/src/routers/search_router.py @@ -142,7 +142,14 @@ def search( # noqa: C901 bool, Query( description="If true, the results are sorted by id." - "By default they are sorted by best score.", + " By default they are sorted by best score.", + ), + ] = False, + sort_by_date_modified: Annotated[ + bool, + Query( + description="If true, the results are sorted by date_modified (newest first)." + " By default they are sorted by best score.", ), ] = False, limit: Annotated[int, Query(ge=1, le=LIMIT_MAX)] = 10, @@ -196,6 +203,8 @@ def search( # noqa: C901 sort: dict[str, str | dict[str, str]] = {} if sort_by_id: sort = {"identifier": "asc"} + elif sort_by_date_modified: + sort = {"date_modified": "desc"} else: sort = {"_score": {"order": "desc"}} From d8d1ceac24faeaa31fddb91f3c1864332857ba55 Mon Sep 17 00:00:00 2001 From: Srija Talamarla Date: Thu, 12 Mar 2026 16:24:42 +0000 Subject: [PATCH 2/2] Add test for sort by date modified parameter --- .../search_routers/test_search_routers.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/tests/routers/search_routers/test_search_routers.py b/src/tests/routers/search_routers/test_search_routers.py index bb94b13a..fbb2aa8d 100644 --- a/src/tests/routers/search_routers/test_search_routers.py +++ b/src/tests/routers/search_routers/test_search_routers.py @@ -1,6 +1,5 @@ import json from http import HTTPStatus -from typing import Sequence from unittest.mock import Mock import pytest @@ -44,7 +43,7 @@ def test_search_happy_path_get_all(client: TestClient, mocked_privileged_token: response = client.post("/events", json=body, headers={"Authorization": "Fake token"}) response.raise_for_status() - identifier = response.json()['identifier'] + identifier = response.json()["identifier"] mock_elasticsearch(filename_mock="event_search.json", identifier=identifier) search_service = "/search/events" @@ -145,6 +144,19 @@ def test_search_bad_offset(client: TestClient, search_router): ] +@pytest.mark.parametrize("search_router", sr.router_list) +def test_search_sort_by_date_modified(client: TestClient, search_router): + mock_elasticsearch(filename_mock=f"{search_router.es_index}_search.json") + + search_service = f"/search/{search_router.resource_name_plural}" + params = {"search_query": "description", "sort_by_date_modified": True} + response = client.get(search_service, params=params) + + assert response.status_code == 200, response.json() + _, kwargs = ElasticsearchSingleton().client.search.call_args + assert kwargs["sort"] == {"date_modified": "desc"} + + def mock_elasticsearch(filename_mock: str, identifier: str = ""): with open(path_test_resources() / "elasticsearch" / filename_mock, "r") as f: mocked_results = json.load(f)