diff --git a/app/general/filters.py b/app/general/filters.py index 5f3ecbc4..3902eb27 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.db.models import F +from django.contrib.postgres.search import ( + SearchHeadline, + SearchQuery, + SearchRank, + SearchVector, +) +from django.db.models import F, IntegerField, Q, Value 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): @@ -36,19 +41,28 @@ def filter_queryset(self, queryset): queryset = super().filter_queryset(queryset) search = self.form.cleaned_data.get("search", "") + search_vector = SearchVector("title", weight="A") + SearchVector("description", weight="B") + + project_query = Project.objects.annotate( + search=SearchVector("name", weight="A") + SearchVector("description", weight="B") + ).filter(search=SearchQuery(search)) + queue = SearchQuery(search.strip()) search_rank = SearchRank(F("search_vector"), queue) search_headline = SearchHeadline("document_data", queue) queryset = ( queryset.annotate( + search=search_vector, rank=search_rank, search_headline=search_headline, + weight=Value(0, IntegerField()), ) .defer("document_data") .select_related("institution") ).order_by("-rank") - return queryset + combined_results = list(project_query) + list(queryset) + return combined_results def filter_search(self, queryset, name, value): if value: 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 @@