Loading django/contrib/postgres/apps.py +3 −1 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) django/contrib/postgres/lookups.py +5 −0 Original line number Diff line number Diff line Loading @@ -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 = '%%' django/contrib/postgres/operations.py +6 −0 Original line number Diff line number Diff line Loading @@ -40,3 +40,9 @@ class UnaccentExtension(CreateExtension): def __init__(self): self.name = 'unaccent' class TrigramExtension(CreateExtension): def __init__(self): self.name = 'pg_trgm' django/contrib/postgres/search.py +16 −0 Original line number Diff line number Diff line Loading @@ -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 = ' <-> ' docs/ref/contrib/postgres/lookups.txt +26 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,32 @@ PostgreSQL specific lookups =========================== Trigram similarity ================== .. fieldlookup:: trigram_similar .. versionadded:: 1.10 The ``trigram_similar`` lookup allows you to perform trigram lookups, measuring the number of trigrams (three consecutive characters) shared, using a dedicated PostgreSQL extension. A trigram lookup is given an expression and returns results that have a similarity measurement greater than the current similarity threshold. To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS` and activate the `pg_trgm extension <http://www.postgresql.org/docs/current/interactive/pgtrgm.html>`_ on PostgreSQL. You can install the extension using the :class:`~django.contrib.postgres.operations.TrigramExtension` migration operation. The ``trigram_similar`` lookup can be used on :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`:: >>> City.objects.filter(name__trigram_similar="Middlesborough") ['<City: Middlesbrough>'] ``Unaccent`` ============ Loading Loading
django/contrib/postgres/apps.py +3 −1 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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)
django/contrib/postgres/lookups.py +5 −0 Original line number Diff line number Diff line Loading @@ -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 = '%%'
django/contrib/postgres/operations.py +6 −0 Original line number Diff line number Diff line Loading @@ -40,3 +40,9 @@ class UnaccentExtension(CreateExtension): def __init__(self): self.name = 'unaccent' class TrigramExtension(CreateExtension): def __init__(self): self.name = 'pg_trgm'
django/contrib/postgres/search.py +16 −0 Original line number Diff line number Diff line Loading @@ -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 = ' <-> '
docs/ref/contrib/postgres/lookups.txt +26 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,32 @@ PostgreSQL specific lookups =========================== Trigram similarity ================== .. fieldlookup:: trigram_similar .. versionadded:: 1.10 The ``trigram_similar`` lookup allows you to perform trigram lookups, measuring the number of trigrams (three consecutive characters) shared, using a dedicated PostgreSQL extension. A trigram lookup is given an expression and returns results that have a similarity measurement greater than the current similarity threshold. To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS` and activate the `pg_trgm extension <http://www.postgresql.org/docs/current/interactive/pgtrgm.html>`_ on PostgreSQL. You can install the extension using the :class:`~django.contrib.postgres.operations.TrigramExtension` migration operation. The ``trigram_similar`` lookup can be used on :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`:: >>> City.objects.filter(name__trigram_similar="Middlesborough") ['<City: Middlesbrough>'] ``Unaccent`` ============ Loading