From c111d24c9645dbfb75fdc5b9edf048da9acd8d53 Mon Sep 17 00:00:00 2001 From: Philippe MILINK Date: Wed, 30 Oct 2024 22:49:38 +0100 Subject: [PATCH] =?UTF-8?q?G=C3=A8re=20le=20cas=20o=C3=B9=20les=20r=C3=A9s?= =?UTF-8?q?ultats=20de=20la=20recherche=20ne=20contiennent=20pas=20de=20hi?= =?UTF-8?q?ghlight?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zds/search/tests/tests_views.py | 4 ++++ zds/search/views.py | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/zds/search/tests/tests_views.py b/zds/search/tests/tests_views.py index d53f33a6bc..6bfaad1aa7 100644 --- a/zds/search/tests/tests_views.py +++ b/zds/search/tests/tests_views.py @@ -142,6 +142,10 @@ def test_basic_search(self): ) # … and only of the right type … self.assertEqual(r["document"]["id"], ids[doc_type][i]) # … with the right id ! + # Search with a query which returns results, but without highlights: + result = self.client.get(reverse("search:query") + "?q=-c", follow=False) + self.assertEqual(result.status_code, 200) + def test_search_many_pages(self): if not self.manager.connected: return diff --git a/zds/search/views.py b/zds/search/views.py index fd29c1b853..1427a8fb5c 100644 --- a/zds/search/views.py +++ b/zds/search/views.py @@ -214,7 +214,8 @@ def get_queryset(self): if "text_match" in entry: entry["collection"] = search_collections[i] entry["document"]["final_score"] = entry["text_match"] * entry["document"]["weight"] - entry["document"]["highlights"] = entry["highlights"][0] + if len(entry["highlights"]) > 0: + entry["document"]["highlights"] = entry["highlights"][0] if "tags" in entry["document"] and "tag_slugs" in entry["document"]: assert len(entry["document"]["tags"]) == len(entry["document"]["tag_slugs"])