mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #24938 -- Added PostgreSQL trigram support.
This commit is contained in:
committed by
Tim Graham
parent
d7334b405f
commit
1962a96a30
@@ -3,7 +3,7 @@ from django.db.backends.signals import connection_created
|
||||
from django.db.models import CharField, TextField
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .lookups import SearchLookup, Unaccent
|
||||
from .lookups import SearchLookup, TrigramSimilar, Unaccent
|
||||
from .signals import register_hstore_handler
|
||||
|
||||
|
||||
@@ -17,3 +17,5 @@ class PostgresConfig(AppConfig):
|
||||
TextField.register_lookup(Unaccent)
|
||||
CharField.register_lookup(SearchLookup)
|
||||
TextField.register_lookup(SearchLookup)
|
||||
CharField.register_lookup(TrigramSimilar)
|
||||
TextField.register_lookup(TrigramSimilar)
|
||||
|
||||
@@ -60,3 +60,8 @@ class SearchLookup(SearchVectorExact):
|
||||
self.lhs = SearchVector(self.lhs)
|
||||
lhs, lhs_params = super(SearchLookup, self).process_lhs(qn, connection)
|
||||
return lhs, lhs_params
|
||||
|
||||
|
||||
class TrigramSimilar(PostgresSimpleLookup):
|
||||
lookup_name = 'trigram_similar'
|
||||
operator = '%%'
|
||||
|
||||
@@ -40,3 +40,9 @@ class UnaccentExtension(CreateExtension):
|
||||
|
||||
def __init__(self):
|
||||
self.name = 'unaccent'
|
||||
|
||||
|
||||
class TrigramExtension(CreateExtension):
|
||||
|
||||
def __init__(self):
|
||||
self.name = 'pg_trgm'
|
||||
|
||||
@@ -185,3 +185,19 @@ class SearchRank(Func):
|
||||
|
||||
|
||||
SearchVectorField.register_lookup(SearchVectorExact)
|
||||
|
||||
|
||||
class TrigramBase(Func):
|
||||
def __init__(self, expression, string, **extra):
|
||||
if not hasattr(string, 'resolve_expression'):
|
||||
string = Value(string)
|
||||
super(TrigramBase, self).__init__(expression, string, output_field=FloatField(), **extra)
|
||||
|
||||
|
||||
class TrigramSimilarity(TrigramBase):
|
||||
function = 'SIMILARITY'
|
||||
|
||||
|
||||
class TrigramDistance(TrigramBase):
|
||||
function = ''
|
||||
arg_joiner = ' <-> '
|
||||
|
||||
Reference in New Issue
Block a user