Fix the dummheiten in the TrigramSearchManager.

This commit is contained in:
Schuyler Erle 2012-02-28 07:00:28 -08:00
parent e8e5b6e85a
commit 44e91fe2ca
2 changed files with 14 additions and 12 deletions

View File

@ -7,12 +7,14 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
cursor = connection.cursor() cursor = connection.cursor()
for name, model in models: for name in dir(models):
model = getattr(models, name)
if not hasattr(model, "objects") or \ if not hasattr(model, "objects") or \
not isinstance(model.objects, model.TrigramSearchManager): not isinstance(model.objects, models.TrigramSearchManager):
continue continue
table = model._meta.db_table table = model._meta.db_table
for column in model.objects.trigram_columns: for column in model.objects.trigram_columns:
cursor.execute(""" sql = """CREATE INDEX %s_%s_trgm_idx ON %s USING gin (%s gin_trgm_ops);""" % (
CREATE INDEX %s_%s_trgm_idx ON %s USING gin (%s gin_trgm_ops);""" % ( table, column, table, column)
table, column, table, column)) cursor.execute(sql)
cursor.execute("COMMIT;")

View File

@ -3,6 +3,7 @@ from django.contrib.gis.geos import Point
from django import forms from django import forms
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from django.db import connection
import json import json
STOP_CHOICES = ( ('U','Up'), STOP_CHOICES = ( ('U','Up'),
@ -43,8 +44,8 @@ SCHED = {
} }
class TrigramSearchManager(models.Manager): class TrigramSearchManager(models.Manager):
def __init__(self, trigram_columns, *args, **kwargs): def __init__(self, trigram_columns=[]):
super(models.Manager, self).__init__(*args, **kwargs) super(TrigramSearchManager, self).__init__()
self.trigram_columns = trigram_columns self.trigram_columns = trigram_columns
def set_threshold(self, threshold): def set_threshold(self, threshold):
@ -57,18 +58,17 @@ class TrigramSearchManager(models.Manager):
similarity_measure = "greatest(%s)" % ",".join(["similarity(%s, %%s)" % col for col in self.trigram_columns]) similarity_measure = "greatest(%s)" % ",".join(["similarity(%s, %%s)" % col for col in self.trigram_columns])
similarity_filter = " OR ".join(["%s %%%% %%s" % col for col in self.trigram_columns]) similarity_filter = " OR ".join(["%s %%%% %%s" % col for col in self.trigram_columns])
text_values = [text] * len(self.trigram_columns) text_values = [text] * len(self.trigram_columns)
qset = self.get_query_set() qset = self.get_query_set()
# use the pg_trgm index via the % operator # use the pg_trgm index via the % operator
qset = qset.extra(select={"similarity":similarity_measure, qset = qset.extra(select={"similarity":similarity_measure},
select_params=text_values, select_params=text_values,
where=similarity_filter, where=[similarity_filter],
params=text_values, params=text_values,
order_by=["-similarity"]) order_by=["-similarity"])
return qset return qset
class Area(models.Model): class Area(models.Model):
objects = TrigramSearchManager("name", "name_mr", "display_name") objects = TrigramSearchManager(("name", "name_mr", "display_name"))
code = models.IntegerField() #primary_key=True) code = models.IntegerField() #primary_key=True)
slug = models.SlugField(null=True) slug = models.SlugField(null=True)
name = models.TextField(blank=True, max_length=255) name = models.TextField(blank=True, max_length=255)
@ -118,7 +118,7 @@ class Fare(models.Model):
return str(self.slab) return str(self.slab)
class Stop(models.Model): class Stop(models.Model):
objects = TrigramSearchManager("name", "name_mr", "display_name") objects = TrigramSearchManager(("name", "name_mr", "display_name"))
code = models.IntegerField() code = models.IntegerField()
slug = models.SlugField(null=True) slug = models.SlugField(null=True)
name = models.TextField(blank=True, max_length=255) name = models.TextField(blank=True, max_length=255)