]> git.parisson.com Git - telemeta.git/commitdiff
optimise advance search with boost for principal location
authorKaltar5679 <killian.mary@outlook.fr>
Mon, 15 Jun 2015 09:13:50 +0000 (11:13 +0200)
committerKaltar5679 <killian.mary@outlook.fr>
Mon, 15 Jun 2015 09:13:50 +0000 (11:13 +0200)
telemeta/forms/haystack_form.py
telemeta/search_indexes.py

index 6c41fc2a786001fda8403a120a1f140b0e65c278..4288d5c1845e8f89887d74326bfa24305342bd15 100644 (file)
@@ -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') != '':
index a8d9472bc8b0b1f66e00526140e5e09b89187b36..47b1814a0572f3f4de642e54a56a3e8ed6418bc1 100644 (file)
@@ -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: