diff --git a/app/app/views.py b/app/app/views.py index 5efe5b2d..4a15de11 100644 --- a/app/app/views.py +++ b/app/app/views.py @@ -278,9 +278,7 @@ def search(request): template = "app/search.html" - ordered_queryset = f.qs.order_by("id") - - paginator = Paginator(ordered_queryset, 5) # 5 documents per page + paginator = Paginator(f.qs, 5) # 5 documents per page try: page_obj = paginator.page(page_number) diff --git a/app/general/filters.py b/app/general/filters.py index 18cc44b6..5f3ecbc4 100644 --- a/app/general/filters.py +++ b/app/general/filters.py @@ -32,23 +32,28 @@ class Meta: "languages", ] + def filter_queryset(self, queryset): + queryset = super().filter_queryset(queryset) + + search = self.form.cleaned_data.get("search", "") + queue = SearchQuery(search.strip()) + search_rank = SearchRank(F("search_vector"), queue) + search_headline = SearchHeadline("document_data", queue) + queryset = ( + queryset.annotate( + rank=search_rank, + search_headline=search_headline, + ) + .defer("document_data") + .select_related("institution") + ).order_by("-rank") + + return queryset + def filter_search(self, queryset, name, value): if value: queue = SearchQuery(value.strip()) - search_rank = SearchRank(F("search_vector"), queue) - search_headline = SearchHeadline("document_data", queue) - - queryset = ( - queryset.annotate( - rank=search_rank, - search_headline=search_headline, - ) - .defer("document_data") - .select_related("institution") - .filter(search_vector=queue) - .order_by("-rank", "id") - ) - return queryset + return queryset.filter(search_vector=queue) else: return queryset