From d59cdb24a9e9f5dc099e9539608a0d2469863d9f Mon Sep 17 00:00:00 2001 From: mathieu Date: Wed, 11 May 2016 17:42:02 +0200 Subject: [PATCH] Add new index for autocomplete + locations in autocomplete --- app/settings.py | 13 +++++++++++++ telemeta/search_indexes.py | 10 ++++++++-- .../search/indexes/telemeta/location_text.txt | 1 + telemeta/views/search.py | 11 +++++++---- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 telemeta/templates/search/indexes/telemeta/location_text.txt diff --git a/app/settings.py b/app/settings.py index ee965244..2b0c90c8 100644 --- a/app/settings.py +++ b/app/settings.py @@ -306,6 +306,19 @@ HAYSTACK_CONNECTIONS = { 'URL': 'http://search:9200/', 'INDEX_NAME': 'haystack', 'INLUDE_SPELLING': True, + 'EXCLUDED_INDEXES': ['telemeta.search_indexes.LocationIndex'] + }, + 'autocomplete': { + # 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', + 'ENGINE': 'telemeta.util.backend.CustomElasticEngine', + 'URL': 'http://search:9200/', + 'INDEX_NAME': 'haystackauto', + 'INLUDE_SPELLING': True, + 'EXCLUDED_INDEXES': ['telemeta.search_indexes.MediaItemIndex', + 'telemeta.search_indexes.MediaCollectionIndex', + 'telemeta.search_indexes.MediaCorpusIndex', + 'telemeta.search_indexes.MediaFondsIndex' + ] }, } HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor' diff --git a/telemeta/search_indexes.py b/telemeta/search_indexes.py index 9d767a7e..b8fb98c1 100644 --- a/telemeta/search_indexes.py +++ b/telemeta/search_indexes.py @@ -239,7 +239,13 @@ class MediaFondsIndex(indexes.SearchIndex, indexes.Indexable): def get_model(self): return MediaFonds -#class LocationIndex(indexes.SearchIndex, indexes.Indexable): +class LocationIndex(indexes.SearchIndex, indexes.Indexable): -# text = indexes.CharField(document=True, use_template=True) + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + return Location + + def index_queryset(self, using=None): + return MediaItem.objects.all().locations() diff --git a/telemeta/templates/search/indexes/telemeta/location_text.txt b/telemeta/templates/search/indexes/telemeta/location_text.txt new file mode 100644 index 00000000..28701c17 --- /dev/null +++ b/telemeta/templates/search/indexes/telemeta/location_text.txt @@ -0,0 +1 @@ +{{ object.name }} \ No newline at end of file diff --git a/telemeta/views/search.py b/telemeta/views/search.py index 33d173f7..475d3eec 100644 --- a/telemeta/views/search.py +++ b/telemeta/views/search.py @@ -167,14 +167,14 @@ class HaystackAdvanceSearch(SavedSearchView): def autocomplete(request): sqs = SearchQuerySet().load_all() - if request.GET.get('attr', '') == "instruments" or request.GET.get('attr', '') == "location": + if request.GET.get('attr', '') == "instruments": if request.GET.get('attr', '') == "instruments": sqs = sqs.filter(instruments__startswith=request.GET.get('q', '')) objets = [result.instruments for result in sqs] #instrus = [result.instruments for result in sqs] - elif request.GET.get('attr', '') == "location": - sqs = sqs.filter(SQ(location_principal__startswith=request.GET.get('q', ''))|SQ(location_relation__startswith=request.GET.get('q', ''))) - objets = [result.location_principal+result.location_relation for result in sqs] + #elif request.GET.get('attr', '') == "location": + # sqs = sqs.filter(SQ(location_principal__startswith=request.GET.get('q', ''))|SQ(location_relation__startswith=request.GET.get('q', ''))) + # objets = [result.location_principal+result.location_relation for result in sqs] suggestions = [] for chaine in objets : #for chaine in instrus: @@ -193,6 +193,9 @@ def autocomplete(request): for word in chaine.split('; '): if word != "" and escapeAccentAndLower(request.GET.get('q', '')) in escapeAccentAndLower(word): suggestions.append(word) + elif request.GET.get('attr', '') == "location": + sqs = SearchQuerySet().using('autocomplete').filter(content__startswith=request.GET.get('q', '')) + suggestions = [obj.text for obj in sqs] else: suggestions = [] -- 2.39.5