From 2e230709c1861e3d0cf8c5b481a25b5775dc88dc Mon Sep 17 00:00:00 2001 From: Daniel Gray Date: Tue, 16 Jul 2024 08:27:01 +0200 Subject: [PATCH] projects to search filters updated tests --- app/general/filters.py | 36 +++++++++++++++++++++++++++----- app/general/tests/test_filter.py | 4 ++-- app/templates/app/search.html | 21 +++++++++++++++++-- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/app/general/filters.py b/app/general/filters.py index 5f3ecbc4..26d20e97 100644 --- a/app/general/filters.py +++ b/app/general/filters.py @@ -1,10 +1,15 @@ import django_filters from django import forms -from django.contrib.postgres.search import SearchHeadline, SearchQuery, SearchRank +from django.contrib.postgres.search import ( + SearchHeadline, + SearchQuery, + SearchRank, + SearchVector, +) from django.db.models import F from django_filters import ModelMultipleChoiceFilter, MultipleChoiceFilter -from general.models import DocumentFile, Institution, Language, Subject +from general.models import DocumentFile, Institution, Language, Project, Subject class DocumentFileFilter(django_filters.FilterSet): @@ -35,8 +40,27 @@ class Meta: def filter_queryset(self, queryset): queryset = super().filter_queryset(queryset) - search = self.form.cleaned_data.get("search", "") - queue = SearchQuery(search.strip()) + search = self.form.cleaned_data.get("search", "").strip() + queue = SearchQuery(search) + + search_document_files = ( + DocumentFile.objects.annotate( + search=SearchVector("title") + SearchVector("description"), + search_headline=SearchHeadline("description", queue), + ) + .filter(search=SearchQuery(search)) + .only("title", "description") + ) + + project_query = ( + Project.objects.annotate( + search=SearchVector("name") + SearchVector("description"), + search_headline=SearchHeadline("description", queue), + ) + .filter(search=SearchQuery(search)) + .defer("institution_id", "subjects", "languages") + ) + search_rank = SearchRank(F("search_vector"), queue) search_headline = SearchHeadline("document_data", queue) queryset = ( @@ -48,7 +72,9 @@ def filter_queryset(self, queryset): .select_related("institution") ).order_by("-rank") - return queryset + combined_results = list(project_query) + list(search_document_files) + list(queryset) + + return combined_results def filter_search(self, queryset, name, value): if value: diff --git a/app/general/tests/test_filter.py b/app/general/tests/test_filter.py index 1cc5650c..c9d0ca92 100644 --- a/app/general/tests/test_filter.py +++ b/app/general/tests/test_filter.py @@ -77,13 +77,13 @@ def test_combined_filters(self): def test_search_filter(self): data = {"search": "Document"} filter = DocumentFileFilter(data=data) - self.assertEqual(len(filter.qs), 2) + self.assertEqual(len(filter.qs), 4) self.assertIn(self.doc1, filter.qs) self.assertIn(self.doc2, filter.qs) data = {"search": "Document 1"} filter = DocumentFileFilter(data=data) - self.assertEqual(len(filter.qs), 1) + self.assertEqual(len(filter.qs), 2) self.assertIn(self.doc1, filter.qs) diff --git a/app/templates/app/search.html b/app/templates/app/search.html index 4709731b..30e593db 100644 --- a/app/templates/app/search.html +++ b/app/templates/app/search.html @@ -21,6 +21,23 @@
{% trans "Search a term" %}
{% for document in search_results %}