2
0

postgres_search.rst 4.8 KB

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