From 9fb297abbd77ebf53ca8eca58f1d3ae70e1be55e Mon Sep 17 00:00:00 2001 From: DonHaul Date: Fri, 23 Aug 2024 16:58:52 +0200 Subject: [PATCH] backoffice: ordering by best match added * ref: cern-sis/issues-inspire/issues/531 --- backoffice/backoffice/workflows/api/views.py | 4 +-- .../backoffice/workflows/tests/test_views.py | 30 ++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/backoffice/backoffice/workflows/api/views.py b/backoffice/backoffice/workflows/api/views.py index 2a67bfdd..17172077 100644 --- a/backoffice/backoffice/workflows/api/views.py +++ b/backoffice/backoffice/workflows/api/views.py @@ -272,9 +272,9 @@ def __init__(self, *args, **kwargs): "is_update": "is_update", } - ordering_fields = {"_updated_at": "_updated_at"} + ordering_fields = {"_updated_at": "_updated_at", "_score": "_score"} - ordering = ("-_updated_at",) + ordering = ("-_updated_at", "-_score") faceted_search_fields = { "status": { diff --git a/backoffice/backoffice/workflows/tests/test_views.py b/backoffice/backoffice/workflows/tests/test_views.py index d9395e95..319d6c94 100644 --- a/backoffice/backoffice/workflows/tests/test_views.py +++ b/backoffice/backoffice/workflows/tests/test_views.py @@ -447,12 +447,15 @@ class TestWorkflowSearchFilterViewSet(BaseTransactionTestCase): reset_sequences = True fixtures = ["backoffice/fixtures/groups.json"] - def setUp(self): - super().setUp() - @classmethod def setUpClass(cls): super().setUpClass() + + index = registry.get_indices().pop() + with contextlib.suppress(opensearchpy.exceptions.NotFoundError): + index.delete() + index.create() + Workflow.objects.update_or_create( data={ "ids": [ @@ -528,7 +531,7 @@ def test_filter_workflow_type(self): for item in response.json()["results"]: assert item["workflow_type"] == WorkflowType.AUTHOR_CREATE - def test_ordering(self): + def test_ordering_updated_at(self): self.api_client.force_authenticate(user=self.admin) base_url = reverse("search:workflow-list") @@ -545,6 +548,25 @@ def test_ordering(self): assert cur_date < previous_date previous_date = cur_date + def test_ordering_score(self): + self.api_client.force_authenticate(user=self.admin) + + search_str = "search=Frank Castle^10 OR John^6" + + url = reverse("search:workflow-list") + f"?ordering=_score&{search_str}" + response = self.api_client.get(url) + self.assertEqual( + response.json()["results"][0]["data"]["name"]["preferred_name"], + "John Smith", + ) + + url = reverse("search:workflow-list") + f"?ordering=-_score&{search_str}" + response = self.api_client.get(url) + self.assertEqual( + response.json()["results"][0]["data"]["name"]["preferred_name"], + "Frank Castle", + ) + class TestDecisionsViewSet(BaseTransactionTestCase): endpoint = "/api/decisions"