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