sql-engines.rst 4.8 KB

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