From 2f9c832e74528ed78261767aebdd03b48a786cb1 Mon Sep 17 00:00:00 2001 From: Kaltar5679 Date: Mon, 15 Jun 2015 11:13:50 +0200 Subject: [PATCH] optimise advance search with boost for principal location --- telemeta/forms/haystack_form.py | 2 +- telemeta/search_indexes.py | 39 ++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/telemeta/forms/haystack_form.py b/telemeta/forms/haystack_form.py index 6c41fc2a..4288d5c1 100644 --- a/telemeta/forms/haystack_form.py +++ b/telemeta/forms/haystack_form.py @@ -157,7 +157,7 @@ class HayAdvanceForm(SearchForm): sqs = sqs.filter(code__contains=self.cleaned_data['code']) if self.cleaned_data.get('location'): - sqs = sqs.filter(location__contains=self.cleaned_data['location']) + sqs = sqs.filter(location_principal__contains=self.cleaned_data['location']).filter_or(location_relation__contains=self.cleaned_data['location']) if self.cleaned_data['ethnic_group']: if self.cleaned_data.get('ethnic_group') != '': diff --git a/telemeta/search_indexes.py b/telemeta/search_indexes.py index a8d9472b..47b1814a 100644 --- a/telemeta/search_indexes.py +++ b/telemeta/search_indexes.py @@ -16,8 +16,8 @@ class MediaItemIndex(indexes.SearchIndex, indexes.Indexable): #advance search title = indexes.NgramField(model_attr='title') code = indexes.NgramField(model_attr='code', default='') - #location = indexes.NgramField(default='', null='None') - location = indexes.MultiValueField() + location_principal = indexes.CharField(null='None', boost=1.05) + location_relation = indexes.CharField() ethnic_group = indexes.CharField(model_attr='ethnic_group', default='') instruments = indexes.NgramField(default='') collectors = indexes.NgramField(model_attr='collector', default='') @@ -36,10 +36,15 @@ class MediaItemIndex(indexes.SearchIndex, indexes.Indexable): def get_model(self): return MediaItem - def prepare_location(self, obj): + def prepare_location_principal(self, obj): + if obj.location is not None: + return u"".join(obj.location.name) + else: + return None + + def prepare_location_relation(self, obj): location = [] if obj.location is not None: - location.append(obj.location.name) location_alias = LocationAlias.objects.filter(location__name=obj.location) location_rela = LocationRelation.objects.filter(location__name=obj.location) for rela in location_rela: @@ -48,8 +53,8 @@ class MediaItemIndex(indexes.SearchIndex, indexes.Indexable): location.append(alias.alias) #print u"".join(' ' + local for local in location).encode("utf-8") #print u"%s".encode("utf-8") % location - print [local for local in location] - return [local for local in location] + #print [local for local in location] + return u"".join(' ' + local for local in location) def prepare_instruments(self, obj): item = MediaItemPerformance.objects.all().filter(media_item__exact=obj) @@ -59,13 +64,13 @@ class MediaItemIndex(indexes.SearchIndex, indexes.Indexable): instruments.append(material.instrument.name) if material.alias is not None: instruments.append(material.alias.name) - return u"".join(' ' + instru for instru in instruments).encode("utf-8") + return u"".join(' ' + instru for instru in instruments) def prepare_collectors(self, obj): collectors = [] collectors.append(obj.collection.collector) collectors.append(obj.collector) - return u"".join(' ' + collector for collector in collectors).encode("utf-8") + return u"".join(' ' + collector for collector in collectors) class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): @@ -81,7 +86,8 @@ class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): #advance search title = indexes.NgramField(model_attr='title') code = indexes.NgramField(model_attr='code', default='') - location = indexes.CharField(default='') + location_principal = indexes.CharField(default='', boost=1.05) + location_relation = indexes.CharField() ethnic_group = indexes.CharField(default='') instruments = indexes.NgramField(default='') collectors = indexes.NgramField(model_attr='collector', default='') @@ -95,12 +101,19 @@ class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): def get_model(self): return MediaCollection - def prepare_location(self, obj): + def prepare_location_principal(self, obj): + collec_location = [] + for item in obj.items.all(): + location = [] + if item.location is not None: + collec_location.append(item.location.name) + return u"".join(' ' + location for location in collec_location) + + def prepare_location_relation(self, obj): collec_location = [] for item in obj.items.all(): location = [] if item.location is not None: - location.append(item.location.name) location_alias = LocationAlias.objects.filter(location__name=item.location) location_rela = LocationRelation.objects.filter(location__name=item.location) for rela in location_rela: @@ -110,7 +123,7 @@ class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): for name in location: if name and not name in collec_location: collec_location.append(name) - return u"".join(' ' + location for location in collec_location).encode("utf-8") + return u"".join(' ' + location for location in collec_location) def prepare_ethnic_group(self, obj): return "%s" % obj.ethnic_groups() @@ -126,7 +139,7 @@ class MediaCollectionIndex(indexes.SearchIndex, indexes.Indexable): if material.alias and not material.alias in instruments: instruments.append(material.alias.name) - return u"".join(' ' + instru for instru in instruments).encode("utf-8") + return u"".join(' ' + instru for instru in instruments) def prepare_recorded_from_date(self, obj): if obj.recorded_from_year != 0: -- 2.39.5