sql-engines.rst 4.9 KB

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