mirror of
https://github.com/django/django.git
synced 2025-07-24 13:44:32 +00:00
Fixed #24938 -- Added PostgreSQL trigram support.
This commit is contained in:
parent
d7334b405f
commit
1962a96a30
11 changed files with 184 additions and 4 deletions
|
@ -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``
|
||||
============
|
||||
|
||||
|
|
|
@ -27,6 +27,16 @@ the ``django.contrib.postgres.operations`` module.
|
|||
which will install the ``hstore`` extension and also immediately set up the
|
||||
connection to interpret hstore data.
|
||||
|
||||
``TrigramExtension``
|
||||
====================
|
||||
|
||||
.. class:: TrigramExtension()
|
||||
|
||||
.. versionadded:: 1.10
|
||||
|
||||
A subclass of :class:`~django.contrib.postgres.operations.CreateExtension`
|
||||
that installs the ``pg_trgm`` extension.
|
||||
|
||||
``UnaccentExtension``
|
||||
=====================
|
||||
|
||||
|
|
|
@ -189,3 +189,58 @@ if it were an annotated ``SearchVector``::
|
|||
[<Entry: Cheese on Toast recipes>, <Entry: Pizza recipes>]
|
||||
|
||||
.. _PostgreSQL documentation: http://www.postgresql.org/docs/current/static/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS
|
||||
|
||||
Trigram similarity
|
||||
==================
|
||||
|
||||
Another approach to searching is trigram similarity. A trigram is a group of
|
||||
three consecutive characters. In addition to the :lookup:`trigram_similar`
|
||||
lookup, you can use a couple of other expressions.
|
||||
|
||||
To use them, you need to activate the `pg_trgm extension
|
||||
<http://www.postgresql.org/docs/current/interactive/pgtrgm.html>`_ on
|
||||
PostgreSQL. You can install it using the
|
||||
:class:`~django.contrib.postgres.operations.TrigramExtension` migration
|
||||
operation.
|
||||
|
||||
``TrigramSimilarity``
|
||||
---------------------
|
||||
|
||||
.. class:: TrigramSimilarity(expression, string, **extra)
|
||||
|
||||
.. versionadded:: 1.10
|
||||
|
||||
Accepts a field name or expression, and a string or expression. Returns the
|
||||
trigram similarity between the two arguments.
|
||||
|
||||
Usage example::
|
||||
|
||||
>>> from django.contrib.postgres.search import TrigramSimilarity
|
||||
>>> Author.objects.create(name='Katy Stevens')
|
||||
>>> Author.objects.create(name='Stephen Keats')
|
||||
>>> test = 'Katie Stephens'
|
||||
>>> Author.objects.annotate(
|
||||
... similarity=TrigramSimilarity('name', test),
|
||||
... ).filter(similarity__gt=0.3).order_by('-similarity')
|
||||
[<Author: Katy Stephens>, <Author: Stephen Keats>]
|
||||
|
||||
``TrigramDistance``
|
||||
-------------------
|
||||
|
||||
.. class:: TrigramDistance(expression, string, **extra)
|
||||
|
||||
.. versionadded:: 1.10
|
||||
|
||||
Accepts a field name or expression, and a string or expression. Returns the
|
||||
trigram distance between the two arguments.
|
||||
|
||||
Usage example::
|
||||
|
||||
>>> from django.contrib.postgres.search import TrigramDistance
|
||||
>>> Author.objects.create(name='Katy Stevens')
|
||||
>>> Author.objects.create(name='Stephen Keats')
|
||||
>>> test = 'Katie Stephens'
|
||||
>>> Author.objects.annotate(
|
||||
... distance=TrigramDistance('name', test),
|
||||
... ).filter(distance__lte=0.7).order_by('distance')
|
||||
[<Author: Katy Stephens>, <Author: Stephen Keats>]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue