Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
score working
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Aug 26, 2024
1 parent ec0410b commit 8fe752e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
6 changes: 3 additions & 3 deletions backoffice/backoffice/workflows/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
FilteringFilterBackend,
OrderingFilterBackend,
)
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet
from drf_spectacular.utils import (
OpenApiExample,
OpenApiParameter,
Expand Down Expand Up @@ -243,7 +243,7 @@ def restart(self, request, pk=None):
],
),
)
class WorkflowDocumentView(BaseDocumentViewSet):
class WorkflowDocumentView(DocumentViewSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.search = self.search.extra(track_total_hits=True)
Expand Down Expand Up @@ -274,7 +274,7 @@ def __init__(self, *args, **kwargs):

ordering_fields = {"_updated_at": "_updated_at", "_score": "_score"}

ordering = ("-_updated_at", "_score")
ordering = ("_updated_at", "-_score")

faceted_search_fields = {
"status": {
Expand Down
30 changes: 26 additions & 4 deletions backoffice/backoffice/workflows/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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")
Expand All @@ -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"
Expand Down

0 comments on commit 8fe752e

Please sign in to comment.