add search of terms

This commit is contained in:
Frédéric Péters 2016-05-17 16:57:05 +02:00
parent 64a6744bd2
commit 2be6423a2f
4 changed files with 70 additions and 1 deletions

View File

@ -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",

View File

@ -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 <http://www.gnu.org/licenses/>.
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)

View File

@ -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<pk>\w+)$', 'pfwb_thesaurus.views.term', name='term'),
url(r'^admin/', include(admin.site.urls)),

View File

@ -14,8 +14,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
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