Try out postgres fulltext search.

This commit is contained in:
Mario Hüttel 2022-04-20 17:33:24 +02:00
parent 0aadf4305f
commit c47350f449
3 changed files with 10 additions and 1 deletions

View File

@ -23,6 +23,7 @@ from django.db.models import Prefetch
from django.db.models.functions import Lower from django.db.models.functions import Lower
from django.forms import formset_factory from django.forms import formset_factory
import uuid import uuid
from django.contrib.postgres.search import SearchVector, SearchQuery, SearchRank, TrigramSimilarity
ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=0) ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=0)
@ -299,12 +300,18 @@ class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
qs = qs.filter(s_filter) qs = qs.filter(s_filter)
return qs return qs
def search_packages_adv(self, search):
if not search:
return Package.objects.all()
return Package.objects.annotate(similarity=TrigramSimilarity('name', search)).order_by('-similarity')
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
page_num = self.request.GET.get('page', default=1) page_num = self.request.GET.get('page', default=1)
search_string = self.request.GET.get('search', default=None) search_string = self.request.GET.get('search', default=None)
package_queryset = self.search_packages(search_string) package_queryset = self.search_packages_adv(search_string)
paginator = Paginator(package_queryset, self.default_page_size) paginator = Paginator(package_queryset, self.default_page_size)

View File

@ -47,6 +47,7 @@ INSTALLED_APPS = [
'crispy_forms', 'crispy_forms',
'crispy_bootstrap5', 'crispy_bootstrap5',
'django.forms', 'django.forms',
'django.contrib.postgres'
] ]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'

View File

@ -68,6 +68,7 @@ INSTALLED_APPS = [
'crispy_forms', 'crispy_forms',
'crispy_bootstrap5', 'crispy_bootstrap5',
'django.forms', 'django.forms',
'django.contrib.postgres'
] ]