From a6dfee7375143c65bdd9032a2bfb6a871aca5e17 Mon Sep 17 00:00:00 2001 From: Schuyler Erle Date: Sat, 27 Aug 2011 17:56:57 -0700 Subject: [PATCH] Fix up (renamed) FeatureSearchManager; add set_threshold --- gazetteer/places/models.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/gazetteer/places/models.py b/gazetteer/places/models.py index dc1a268..8b730c6 100644 --- a/gazetteer/places/models.py +++ b/gazetteer/places/models.py @@ -10,15 +10,21 @@ class AuthorityRecord(models.Model): def __unicode__(self): return self.preferred_name -class GeoSearchManager(manager.GeoManager): +class FeatureSearchManager(manager.GeoManager): + def set_threshold(self, threshold): + """Set the limit for trigram similarity matching.""" + cursor = connection.cursor + cursor.execute("""SELECT set_limit(%f)""" % threshold) + def overlaps(self, bbox, text=None): bbox = 'POLYGON(((%f %f, %f %f, %f %f, %f %f, %f %f)))' % (bbox[0], bbox[1], bbox[2], bbox[1], bbox[2], bbox[3], bbox[0], bbox[3], bbox[0], bbox[1]) - qset = super(GeoSearchManager, self).get_query_set().filter(geometry_bboverlaps=bbox) + qset = super(FeatureSearchManager, self).get_query_set().filter(geometry_bboverlaps=bbox) if text: text = text.replace("'", "''") # escape the ' - qset = qset.extra(where="preferred_name %% '%s'" % text) + # use the pg_trgm index + qset = qset.extra(where=["preferred_name %% '%s']" % text) return qset class Feature(models.Model): @@ -33,7 +39,7 @@ class Feature(models.Model): time_frame = models.ForeignKey("TimeFrame", null=True, blank=True) relationships = models.ManyToManyField("Feature", through='Relationship', blank=True) objects = models.GeoManager() - search = models.GeoSearchManager() + search = models.FeatureSearchManager() class Meta: ordering = ['preferred_name']