postgres_search.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. .. _postgres_search:
  2. ========================
  3. PostgreSQL search engine
  4. ========================
  5. This contrib module provides a search engine backend using
  6. `PostgreSQL full-text search capabilities <https://www.postgresql.org/docs/current/static/textsearch.html>`_.
  7. .. warning::
  8. | You can only use this module to index data from a PostgreSQL database.
  9. **Features**:
  10. - It supports all the search features available in Wagtail.
  11. - Easy to install and adds no external dependency or service.
  12. - Excellent performance for sites with up to 200 000 pages and stays decent for sites up to a million pages.
  13. - Faster to reindex than Elasticsearch, if you use PostgreSQL 9.5 or higher.
  14. **Drawbacks**:
  15. - Partial matching (``SearchField(partial_match=True)``) is not supported
  16. - ``SearchField(boost=…)`` is only partially respected as PostgreSQL only supports four different boosts.
  17. So if you use five or more distinct values for the boost in your site, slight inaccuracies may occur.
  18. - When :ref:`wagtailsearch_specifying_fields`, the index is not used,
  19. so it will be slow on huge sites.
  20. - Still when :ref:`wagtailsearch_specifying_fields`, you cannot search
  21. on a specific method.
  22. Installation
  23. ============
  24. Add ``'wagtail.contrib.postgres_search',`` anywhere in your ``INSTALLED_APPS``:
  25. .. code-block:: python
  26. INSTALLED_APPS = [
  27. ...
  28. 'wagtail.contrib.postgres_search',
  29. ...
  30. ]
  31. Then configure Wagtail to use it as a search backend.
  32. Give it the alias `'default'` if you want it to be the default search backend:
  33. .. code-block:: python
  34. WAGTAILSEARCH_BACKENDS = {
  35. 'default': {
  36. 'BACKEND': 'wagtail.contrib.postgres_search.backend',
  37. },
  38. }
  39. After installing the module, run ``python manage.py migrate`` to create the necessary ``postgres_search_indexentry`` table.
  40. You then need to index data inside this backend using
  41. the :ref:`update_index` command. You can reuse this command whenever
  42. you want. However, it should not be needed after a first usage since
  43. the search engine is automatically updated when data is modified.
  44. To disable this behaviour, see :ref:`wagtailsearch_backends_auto_update`.
  45. Configuration
  46. =============
  47. Language / PostgreSQL search configuration
  48. ------------------------------------------
  49. Use the additional ``'SEARCH_CONFIG'`` key to define which PostgreSQL
  50. search configuration should be used. For example:
  51. .. code-block:: python
  52. WAGTAILSEARCH_BACKENDS = {
  53. 'default': {
  54. 'BACKEND': 'wagtail.contrib.postgres_search.backend',
  55. 'SEARCH_CONFIG': 'english',
  56. }
  57. }
  58. As you can deduce, a PostgreSQL search configuration is mostly used to define
  59. rules for a language, English in this case. A search configuration consists
  60. in a compilation of algorithms (parsers & analysers)
  61. and language specifications (stop words, stems, dictionaries, synonyms,
  62. thesauruses, etc.).
  63. A few search configurations are already defined by default in PostgreSQL.
  64. You can list them using ``sudo -u postgres psql -c "\dF"`` in a Unix shell
  65. or by using this SQL query: ``SELECT cfgname FROM pg_catalog.pg_ts_config``.
  66. These already-defined search configurations are decent, but they’re basic
  67. compared to commercial search engines.
  68. If you want better support for your language, you will have to create
  69. your own PostgreSQL search configuration. See the PostgreSQL documentation for
  70. `an example <https://www.postgresql.org/docs/current/static/textsearch-configuration.html>`_,
  71. `the list of parsers <https://www.postgresql.org/docs/current/static/textsearch-parsers.html>`_,
  72. and `a guide to use dictionaries <https://www.postgresql.org/docs/current/static/textsearch-dictionaries.html>`_.
  73. Atomic rebuild
  74. --------------
  75. Like the Elasticsearch backend, this backend supports
  76. :ref:`wagtailsearch_backends_atomic_rebuild`:
  77. .. code-block:: python
  78. WAGTAILSEARCH_BACKENDS = {
  79. 'default': {
  80. 'BACKEND': 'wagtail.contrib.postgres_search.backend',
  81. 'ATOMIC_REBUILD': True,
  82. }
  83. }
  84. This is nearly useless with this backend. In Elasticsearch, all data
  85. is removed before rebuilding the index. But in this PostgreSQL backend,
  86. only objects no longer in the database are removed. Then the index is
  87. progressively updated, with no moment where the index is empty.
  88. However, if you want to be extra sure that nothing wrong happens while updating
  89. the index, you can use atomic rebuild. The index will be rebuilt, but nobody
  90. will have access to it until reindexing is complete. If any error occurs during
  91. the operation, all changes to the index are reverted
  92. as if reindexing was never started.