Add TrigramSearchManager to ChaloBEST models.

This commit is contained in:
Schuyler Erle 2012-02-28 18:14:19 +05:30
parent e345ea7a63
commit b5fabf412c
2 changed files with 22 additions and 2 deletions

View File

@ -42,7 +42,27 @@ SCHED = {
'2nd &4th':['???']
}
class TrigramSearchManager(models.Manager):
def set_threshold(self, threshold):
"""Set the limit for trigram similarity matching."""
cursor = connection.cursor()
cursor.execute("""SELECT set_limit(%f)""" % threshold)
def find_approximate(self, match=0.5, **kwargs):
self.set_threshold(match)
assert(len(kwargs) == 1)
column, value = kwargs.items()[0]
qset = self.get_query_set()
# use the pg_trgm index via the % operator
qset = qset.extra(select={"similarity":"similarity(" + column + ", %s)"},
select_params=[value],
where=[column + " %% %s"],
params=[value],
order_by=["-similarity"])
return qset
class Area(models.Model):
objects = TrigramSearchManager() # name, name_mr
code = models.IntegerField() #primary_key=True)
slug = models.SlugField(null=True)
name = models.TextField(blank=True, max_length=255)
@ -93,6 +113,7 @@ class Fare(models.Model):
class Stop(models.Model):
objects = TrigramSearchManager() # name, display, name_mr
code = models.IntegerField()
slug = models.SlugField(null=True)
name = models.TextField(blank=True, max_length=255)

View File

@ -2,8 +2,6 @@
# vim: ai ts=4 sts=4 et sw=4
# encoding=utf-8
# Put this in /srv/smsBEST/gateway and change the gateway secret.
# -------------------------------------------------------------------- #
# MAIN CONFIGURATION #
# -------------------------------------------------------------------- #
@ -51,6 +49,7 @@ GATEWAY = {
"push": "http://chalobest.in:8086/?from=%(from)s&txt=%(txt)s&secret=%(secret)s"
}
AJAX_PROXY_HOST = "0.0.0.0" # to open the gateway from the outside
# to help you get started quickly, many django/rapidsms apps are enabled
# by default. you may wish to remove some and/or add your own.