sql-engines.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. .. _sql engines:
  2. ===========
  3. SQL Engines
  4. ===========
  5. .. sidebar:: further read
  6. - `SQLite <https://www.sqlite.org/index.html>`_
  7. - `PostgreSQL <https://www.postgresql.org>`_
  8. - `MySQL <https://www.mysql.com>`_
  9. With the *SQL engines* you can bind SQL databases into SearXNG. The following
  10. Relational Database Management System (RDBMS) are supported:
  11. - :ref:`engine sqlite`
  12. - :ref:`engine postgresql`
  13. - :ref:`engine mysql_server`
  14. All of the engines above are just commented out in the :origin:`settings.yml
  15. <searx/settings.yml>`, as you have to set the required attributes for the
  16. engines, e.g. ``database:`` ...
  17. .. code:: yaml
  18. - name: ...
  19. engine: {sqlite|postgresql|mysql_server}
  20. database: ...
  21. result_template: {template_name}
  22. query_str: ...
  23. By default, the engines use the ``key-value`` template for displaying results /
  24. see :origin:`oscar <searx/templates/oscar/result_templates/key-value.html>` &
  25. :origin:`simple <searx/templates/simple/result_templates/key-value.html>`
  26. themes. If you are not satisfied with the original result layout, you can use
  27. your own template, set ``result_template`` attribute to ``{template_name}`` and
  28. place the templates at::
  29. searx/templates/{theme_name}/result_templates/{template_name}
  30. If you do not wish to expose these engines on a public instance, you can still
  31. add them and limit the access by setting ``tokens`` as described in section
  32. :ref:`private engines`.
  33. Configure the engines
  34. =====================
  35. The configuration of the new database engines are similar. You must put a valid
  36. SQL-SELECT query in ``query_str``. At the moment you can only bind at most one
  37. parameter in your query. By setting the attribute ``limit`` you can define how
  38. many results you want from the SQL server. Basically, it is the same as the
  39. ``LIMIT`` keyword in SQL.
  40. Please, do not include ``LIMIT`` or ``OFFSET`` in your SQL query as the engines
  41. rely on these keywords during paging. If you want to configure the number of
  42. returned results use the option ``limit``.
  43. .. _engine sqlite:
  44. SQLite
  45. ------
  46. .. sidebar:: info
  47. - :origin:`sqlite.py <searx/engines/sqlite.py>`
  48. .. _MediathekView: https://mediathekview.de/
  49. SQLite is a small, fast and reliable SQL database engine. It does not require
  50. any extra dependency. To demonstrate the power of database engines, here is a
  51. more complex example which reads from a MediathekView_ (DE) movie database. For
  52. this example of the SQlite engine download the database:
  53. - https://liste.mediathekview.de/filmliste-v2.db.bz2
  54. and unpack into ``searx/data/filmliste-v2.db``. To search the database use e.g
  55. Query to test: ``!mediathekview concert``
  56. .. code:: yaml
  57. - name: mediathekview
  58. engine: sqlite
  59. disabled: False
  60. categories: general
  61. result_template: default.html
  62. database: searx/data/filmliste-v2.db
  63. query_str: >-
  64. SELECT title || ' (' || time(duration, 'unixepoch') || ')' AS title,
  65. COALESCE( NULLIF(url_video_hd,''), NULLIF(url_video_sd,''), url_video) AS url,
  66. description AS content
  67. FROM film
  68. WHERE title LIKE :wildcard OR description LIKE :wildcard
  69. ORDER BY duration DESC
  70. Extra Dependencies
  71. ------------------
  72. For using :ref:`engine postgresql` or :ref:`engine mysql_server` you need to
  73. install additional packages in Python's Virtual Environment of your SearXNG
  74. instance. To switch into the environment (:ref:`searx-src`) you can use
  75. :ref:`searx.sh`::
  76. $ sudo utils/searx.sh shell
  77. (searx-pyenv)$ pip install ...
  78. .. _engine postgresql:
  79. PostgreSQL
  80. ----------
  81. .. _psycopg2: https://www.psycopg.org/install
  82. .. sidebar:: info
  83. - :origin:`postgresql.py <searx/engines/postgresql.py>`
  84. - ``pip install`` psycopg2_
  85. PostgreSQL is a powerful and robust open source database. Before configuring
  86. the PostgreSQL engine, you must install the dependency ``psychopg2``. You can
  87. find an example configuration below:
  88. .. code:: yaml
  89. - name: my_database
  90. engine: postgresql
  91. database: my_database
  92. username: searxng
  93. password: password
  94. query_str: 'SELECT * from my_table WHERE my_column = %(query)s'
  95. .. _engine mysql_server:
  96. MySQL
  97. -----
  98. .. sidebar:: info
  99. - :origin:`mysql_server.py <searx/engines/mysql_server.py>`
  100. - ``pip install`` :pypi:`mysql-connector-python <mysql-connector-python>`
  101. MySQL is said to be the most popular open source database. Before enabling MySQL
  102. engine, you must install the package ``mysql-connector-python``.
  103. The authentication plugin is configurable by setting ``auth_plugin`` in the
  104. attributes. By default it is set to ``caching_sha2_password``. This is an
  105. example configuration for quering a MySQL server:
  106. .. code:: yaml
  107. - name: my_database
  108. engine: mysql_server
  109. database: my_database
  110. username: searxng
  111. password: password
  112. limit: 5
  113. query_str: 'SELECT * from my_table WHERE my_column=%(query)s'
  114. Acknowledgment
  115. ==============
  116. This development was sponsored by `Search and Discovery Fund
  117. <https://nlnet.nl/discovery>`_ of `NLnet Foundation <https://nlnet.nl/>`_.