Skip to content

Commit

Permalink
Added weights and projects to search filters
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-gray-tangent committed Jul 17, 2024
1 parent b248001 commit caad86d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
22 changes: 18 additions & 4 deletions app/general/filters.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 19 additions & 2 deletions app/templates/app/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ <h5 class="card-title">{% trans "Search a term" %}</h5>
{% for document in search_results %}
<div>
<ul>
<li>
<span class="mr-5 text-primary"><b>{% trans "Project Name:" %}</b></span>
<span>{{ document.name }}</span>
</li>
<li>
<span class="mr-5 text-primary"><b>{% trans "Project description:" %}</b></span>
<span>{{ document.description }}</span>
</li>
<li>
<span> {% if document.logo %}
<img src="{{ document.logo.url }}" alt="
{% blocktrans with project_name=document.name %}{{ project_name }} logo{% endblocktrans %}"
class="project-logo">
{% endif %}
</span>
</li>
<li>
<span class="mr-5 text-primary"><b>{% trans "Title:" %}</b></span>
<span>{{ document.title }}</span>
Expand Down Expand Up @@ -66,8 +83,8 @@ <h5 class="card-title">{% trans "Search a term" %}</h5>
<a href="{{ search_params }}&page={{ documents.previous_page_number }}">&lt; previous</a>
{% endif %}
<span class="current">
Page {{ documents.number }} of {{ documents.paginator.num_pages }}.
</span>
Page {{ documents.number }} of {{ documents.paginator.num_pages }}.
</span>
{% if documents.has_next %}
<a href="{{ search_params }}&page={{ documents.next_page_number }}">next &gt;</a>
<a href="{{ search_params }}&page={{ documents.paginator.num_pages }}">last
Expand Down

0 comments on commit caad86d

Please sign in to comment.