From c47350f449136e79221ec76b5cc4b25f17b20767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Wed, 20 Apr 2022 17:33:24 +0200 Subject: [PATCH] Try out postgres fulltext search. --- shimatta_kenkyusho/parts/views.py | 9 ++++++++- shimatta_kenkyusho/shimatta_kenkyusho/settings.py | 1 + .../shimatta_kenkyusho/settings_production.py | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index 3da80e8..5fa8ce7 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -23,6 +23,7 @@ from django.db.models import Prefetch from django.db.models.functions import Lower from django.forms import formset_factory import uuid +from django.contrib.postgres.search import SearchVector, SearchQuery, SearchRank, TrigramSimilarity ParameterSearchFormSet = formset_factory(ComponentParameterSearchForm, extra=0) @@ -299,12 +300,18 @@ class PackageView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): qs = qs.filter(s_filter) 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): context = super().get_context_data(**kwargs) page_num = self.request.GET.get('page', default=1) 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) diff --git a/shimatta_kenkyusho/shimatta_kenkyusho/settings.py b/shimatta_kenkyusho/shimatta_kenkyusho/settings.py index 8966031..937633b 100644 --- a/shimatta_kenkyusho/shimatta_kenkyusho/settings.py +++ b/shimatta_kenkyusho/shimatta_kenkyusho/settings.py @@ -47,6 +47,7 @@ INSTALLED_APPS = [ 'crispy_forms', 'crispy_bootstrap5', 'django.forms', + 'django.contrib.postgres' ] FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' diff --git a/shimatta_kenkyusho/shimatta_kenkyusho/settings_production.py b/shimatta_kenkyusho/shimatta_kenkyusho/settings_production.py index 43c9681..863c3a0 100644 --- a/shimatta_kenkyusho/shimatta_kenkyusho/settings_production.py +++ b/shimatta_kenkyusho/shimatta_kenkyusho/settings_production.py @@ -68,6 +68,7 @@ INSTALLED_APPS = [ 'crispy_forms', 'crispy_bootstrap5', 'django.forms', + 'django.contrib.postgres' ]