Fixed #32492 -- Added TrigramWordSimilarity() and TrigramWordDistance() on PostgreSQL.

This commit is contained in:
Nikita Marchant 2021-09-15 12:57:49 +02:00 committed by Mariusz Felisiak
parent 4ca508a689
commit 4e4082f939
10 changed files with 148 additions and 9 deletions

View file

@ -280,8 +280,9 @@ 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.
three consecutive characters. In addition to the :lookup:`trigram_similar` and
:lookup:`trigram_word_similar` lookups, you can use a couple of other
expressions.
To use them, you need to activate the `pg_trgm extension
<https://www.postgresql.org/docs/current/pgtrgm.html>`_ on PostgreSQL. You can
@ -308,6 +309,27 @@ Usage example::
... ).filter(similarity__gt=0.3).order_by('-similarity')
[<Author: Katy Stevens>, <Author: Stephen Keats>]
``TrigramWordSimilarity``
-------------------------
.. versionadded:: 4.0
.. class:: TrigramWordSimilarity(string, expression, **extra)
Accepts a string or expression, and a field name or expression. Returns the
trigram word similarity between the two arguments.
Usage example::
>>> from django.contrib.postgres.search import TrigramWordSimilarity
>>> Author.objects.create(name='Katy Stevens')
>>> Author.objects.create(name='Stephen Keats')
>>> test = 'Kat'
>>> Author.objects.annotate(
... similarity=TrigramWordSimilarity(test, 'name'),
... ).filter(similarity__gt=0.3).order_by('-similarity')
[<Author: Katy Stevens>]
``TrigramDistance``
-------------------
@ -326,3 +348,24 @@ Usage example::
... distance=TrigramDistance('name', test),
... ).filter(distance__lte=0.7).order_by('distance')
[<Author: Katy Stevens>, <Author: Stephen Keats>]
``TrigramWordDistance``
-----------------------
.. versionadded:: 4.0
.. class:: TrigramWordDistance(string, expression, **extra)
Accepts a string or expression, and a field name or expression. Returns the
trigram word distance between the two arguments.
Usage example::
>>> from django.contrib.postgres.search import TrigramWordDistance
>>> Author.objects.create(name='Katy Stevens')
>>> Author.objects.create(name='Stephen Keats')
>>> test = 'Kat'
>>> Author.objects.annotate(
... distance=TrigramWordDistance(test, 'name'),
... ).filter(distance__lte=0.7).order_by('distance')
[<Author: Katy Stevens>]