From 404b825de4d293ad4e3637bf012610c149b0bdc3 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 5 Dec 2018 17:56:09 +0100 Subject: [PATCH] forum: make search fuzzy --- core/search_indexes.py | 1 + core/templates/search/indexes/forum/forummessage_auto.txt | 4 ++++ forum/views.py | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 core/templates/search/indexes/forum/forummessage_auto.txt diff --git a/core/search_indexes.py b/core/search_indexes.py index 7c8a50aa..d765fcf9 100644 --- a/core/search_indexes.py +++ b/core/search_indexes.py @@ -59,6 +59,7 @@ class UserOnlySignalProcessor(signals.BaseSignalProcessor): class ForumMessageIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.CharField(document=True, use_template=True) + auto = indexes.EdgeNgramField(use_template=True) def get_model(self): return ForumMessage diff --git a/core/templates/search/indexes/forum/forummessage_auto.txt b/core/templates/search/indexes/forum/forummessage_auto.txt new file mode 100644 index 00000000..c74419ce --- /dev/null +++ b/core/templates/search/indexes/forum/forummessage_auto.txt @@ -0,0 +1,4 @@ +{{ object.topic }} +{{ object.title }} +{{ object.message }} +{{ object.author }} diff --git a/forum/views.py b/forum/views.py index 44352f17..59d5a163 100644 --- a/forum/views.py +++ b/forum/views.py @@ -47,7 +47,7 @@ class ForumSearchView(ListView): def get_queryset(self): query = self.request.GET.get("query", "") - q = SearchQuerySet().models(ForumMessage).filter_or(text__contains=query) + q = SearchQuerySet().models(ForumMessage).autocomplete(auto=query) return [r.object for r in q]