lookups.txt 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ===========================
  2. PostgreSQL specific lookups
  3. ===========================
  4. Trigram similarity
  5. ==================
  6. .. fieldlookup:: trigram_similar
  7. ``trigram_similar``
  8. -------------------
  9. The ``trigram_similar`` lookup allows you to perform trigram lookups,
  10. measuring the number of trigrams (three consecutive characters) shared, using a
  11. dedicated PostgreSQL extension. A trigram lookup is given an expression and
  12. returns results that have a similarity measurement greater than the current
  13. similarity threshold.
  14. To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`
  15. and activate the `pg_trgm extension`_ on PostgreSQL. You can install the
  16. extension using the
  17. :class:`~django.contrib.postgres.operations.TrigramExtension` migration
  18. operation.
  19. The ``trigram_similar`` lookup can be used on
  20. :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`:
  21. .. code-block:: pycon
  22. >>> City.objects.filter(name__trigram_similar="Middlesborough")
  23. ['<City: Middlesbrough>']
  24. .. fieldlookup:: trigram_word_similar
  25. ``trigram_word_similar``
  26. ------------------------
  27. The ``trigram_word_similar`` lookup allows you to perform trigram word
  28. similarity lookups using a dedicated PostgreSQL extension. It can be
  29. approximately understood as measuring the greatest number of trigrams shared
  30. between the parameter and any substring of the field. A trigram word lookup is
  31. given an expression and returns results that have a word similarity measurement
  32. greater than the current similarity threshold.
  33. To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`
  34. and activate the `pg_trgm extension`_ on PostgreSQL. You can install the
  35. extension using the
  36. :class:`~django.contrib.postgres.operations.TrigramExtension` migration
  37. operation.
  38. The ``trigram_word_similar`` lookup can be used on
  39. :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`:
  40. .. code-block:: pycon
  41. >>> Sentence.objects.filter(name__trigram_word_similar="Middlesborough")
  42. ['<Sentence: Gumby rides on the path of Middlesbrough>']
  43. .. fieldlookup:: trigram_strict_word_similar
  44. ``trigram_strict_word_similar``
  45. -------------------------------
  46. Similar to :lookup:`trigram_word_similar`, except that it forces extent
  47. boundaries to match word boundaries.
  48. To use it, add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`
  49. and activate the `pg_trgm extension`_ on PostgreSQL. You can install the
  50. extension using the
  51. :class:`~django.contrib.postgres.operations.TrigramExtension` migration
  52. operation.
  53. The ``trigram_strict_word_similar`` lookup can be used on
  54. :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`.
  55. .. _`pg_trgm extension`: https://www.postgresql.org/docs/current/pgtrgm.html
  56. ``Unaccent``
  57. ============
  58. .. fieldlookup:: unaccent
  59. The ``unaccent`` lookup allows you to perform accent-insensitive lookups using
  60. a dedicated PostgreSQL extension.
  61. This lookup is implemented using :class:`~django.db.models.Transform`, so it
  62. can be chained with other lookup functions. To use it, you need to add
  63. ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS` and activate
  64. the `unaccent extension on PostgreSQL`_. The
  65. :class:`~django.contrib.postgres.operations.UnaccentExtension` migration
  66. operation is available if you want to perform this activation using migrations).
  67. .. _unaccent extension on PostgreSQL: https://www.postgresql.org/docs/current/unaccent.html
  68. The ``unaccent`` lookup can be used on
  69. :class:`~django.db.models.CharField` and :class:`~django.db.models.TextField`:
  70. .. code-block:: pycon
  71. >>> City.objects.filter(name__unaccent="México")
  72. ['<City: Mexico>']
  73. >>> User.objects.filter(first_name__unaccent__startswith="Jerem")
  74. ['<User: Jeremy>', '<User: Jérémy>', '<User: Jérémie>', '<User: Jeremie>']
  75. .. warning::
  76. ``unaccent`` lookups should perform fine in most use cases. However, queries
  77. using this filter will generally perform full table scans, which can be slow
  78. on large tables. In those cases, using dedicated full text indexing tools
  79. might be appropriate.