Commit 1962a96a authored by Matthew Somerville's avatar Matthew Somerville Committed by Tim Graham
Browse files

Fixed #24938 -- Added PostgreSQL trigram support.

parent d7334b40
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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)
+5 −0
Original line number Diff line number Diff line
@@ -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 = '%%'
+6 −0
Original line number Diff line number Diff line
@@ -40,3 +40,9 @@ class UnaccentExtension(CreateExtension):

    def __init__(self):
        self.name = 'unaccent'


class TrigramExtension(CreateExtension):

    def __init__(self):
        self.name = 'pg_trgm'
+16 −0
Original line number Diff line number Diff line
@@ -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 = ' <-> '
+26 −0
Original line number Diff line number Diff line
@@ -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