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"])