diff --git a/pfwb_thesaurus/settings.py b/pfwb_thesaurus/settings.py index 725e432..ab55907 100644 --- a/pfwb_thesaurus/settings.py +++ b/pfwb_thesaurus/settings.py @@ -40,6 +40,7 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'haystack', 'gadjo', 'pfwb_thesaurus.thesaurus', ) @@ -97,6 +98,16 @@ STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'pfwb_thesaurus', 'static'), ) +# indexing +HAYSTACK_CONNECTIONS = { + 'default': { + 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine', + 'PATH': os.path.join(BASE_DIR, 'whoosh_index'), + }, +} + +HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' + TABELLIO_DB_SETTINGS = {} HOMEPAGE_START_TERMS = ["matières culturelles", diff --git a/pfwb_thesaurus/thesaurus/search_indexes.py b/pfwb_thesaurus/thesaurus/search_indexes.py new file mode 100644 index 0000000..1d51b48 --- /dev/null +++ b/pfwb_thesaurus/thesaurus/search_indexes.py @@ -0,0 +1,36 @@ +# pfwb_thesaurus - thesaurus system +# Copyright (C) 2016 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from haystack import indexes + +from .models import Term + + +class TermIndex(indexes.SearchIndex, indexes.Indexable): + term = indexes.CharField(model_attr='term', boost=3) + text = indexes.CharField(document=True) + text_auto = indexes.EdgeNgramField() + + def get_model(self): + return Term + + def prepare_text(self, obj): + words = [obj.term] + words.extend([x.term for x in obj.alternativeterm_set.all()]) + return ' '.join(words) + + def prepare_text_auto(self, obj): + return self.prepare_text(obj) diff --git a/pfwb_thesaurus/urls.py b/pfwb_thesaurus/urls.py index 530e5b3..f7fe817 100644 --- a/pfwb_thesaurus/urls.py +++ b/pfwb_thesaurus/urls.py @@ -4,6 +4,7 @@ from django.contrib import admin urlpatterns = patterns('', url(r'^$', 'pfwb_thesaurus.views.home', name='home'), url(r'^thesaurus/plone-popup/$', 'pfwb_thesaurus.views.plone_popup'), + url(r'^thesaurus/term/search/json/$', 'pfwb_thesaurus.views.term_search_json', name='term-search-json'), url(r'^thesaurus/term/(?P\w+)$', 'pfwb_thesaurus.views.term', name='term'), url(r'^admin/', include(admin.site.urls)), diff --git a/pfwb_thesaurus/views.py b/pfwb_thesaurus/views.py index c3eae05..318e248 100644 --- a/pfwb_thesaurus/views.py +++ b/pfwb_thesaurus/views.py @@ -14,8 +14,14 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from django.views.generic import DetailView, ListView +import json + from django.conf import settings +from django.http import HttpResponse +from django.views.generic import DetailView, ListView + +from haystack.query import SearchQuerySet + from .thesaurus.models import Term @@ -39,3 +45,18 @@ class TermView(DetailView): model = Term term = TermView.as_view() + + +def term_search_json(request): + query = request.GET.get('q') + searchqueryset = SearchQuerySet() + sqs = searchqueryset.autocomplete(text_auto=query) + sqs.load_all() + + result = [] + for item in sqs: + result.append({'title': item.term, 'id': item.id}) + + response = HttpResponse(content_type='application/json') + json.dump({'data': result}, response, indent=2) + return response