.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.sqlite Namespace Reference

Functions

 init (engine_settings)
 
 sqlite_cursor ()
 
 search (query, params)
 

Variables

str engine_type = 'offline'
 
str database = ""
 
str query_str = ""
 
int limit = 10
 
bool paging = True
 
str result_template = 'key-value.html'
 

Detailed Description

SQLite is a small, fast and reliable SQL database engine.  It does not require
any extra dependency.

Example
=======

.. _MediathekView: https://mediathekview.de/

To demonstrate the power of database engines, here is a more complex example
which reads from a MediathekView_ (DE) movie database.  For this example of the
SQLite engine download the database:

- https://liste.mediathekview.de/filmliste-v2.db.bz2

and unpack into ``searx/data/filmliste-v2.db``.  To search the database use e.g
Query to test: ``!mediathekview concert``

.. code:: yaml

   - name: mediathekview
     engine: sqlite
     disabled: False
     categories: general
     result_template: default.html
     database: searx/data/filmliste-v2.db
     query_str:  >-
       SELECT title || ' (' || time(duration, 'unixepoch') || ')' AS title,
              COALESCE( NULLIF(url_video_hd,''), NULLIF(url_video_sd,''), url_video) AS url,
              description AS content
         FROM film
        WHERE title LIKE :wildcard OR description LIKE :wildcard
        ORDER BY duration DESC

Implementations
===============

Function Documentation

◆ init()

searx.engines.sqlite.init ( engine_settings)

Definition at line 56 of file sqlite.py.

56def init(engine_settings):
57 if 'query_str' not in engine_settings:
58 raise ValueError('query_str cannot be empty')
59
60 if not engine_settings['query_str'].lower().startswith('select '):
61 raise ValueError('only SELECT query is supported')
62
63
64@contextlib.contextmanager

◆ search()

searx.engines.sqlite.search ( query,
params )

Definition at line 83 of file sqlite.py.

83def search(query, params):
84 results = []
85
86 query_params = {
87 'query': query,
88 'wildcard': r'%' + query.replace(' ', r'%') + r'%',
89 'limit': limit,
90 'offset': (params['pageno'] - 1) * limit,
91 }
92 query_to_run = query_str + ' LIMIT :limit OFFSET :offset'
93
94 with sqlite_cursor() as cur:
95
96 cur.execute(query_to_run, query_params)
97 col_names = [cn[0] for cn in cur.description]
98
99 for row in cur.fetchall():
100 item = dict(zip(col_names, map(str, row)))
101 item['template'] = result_template
102 logger.debug("append result --> %s", item)
103 results.append(item)
104
105 return results

References searx.engines.sqlite.sqlite_cursor().

+ Here is the call graph for this function:

◆ sqlite_cursor()

searx.engines.sqlite.sqlite_cursor ( )
Implements a :py:obj:`Context Manager <contextlib.contextmanager>` for a
:py:obj:`sqlite3.Cursor`.

Open database in read only mode: if the database doesn't exist.  The default
mode creates an empty file on the file system.  See:

* https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
* https://www.sqlite.org/uri.html

Definition at line 65 of file sqlite.py.

65def sqlite_cursor():
66 """Implements a :py:obj:`Context Manager <contextlib.contextmanager>` for a
67 :py:obj:`sqlite3.Cursor`.
68
69 Open database in read only mode: if the database doesn't exist. The default
70 mode creates an empty file on the file system. See:
71
72 * https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
73 * https://www.sqlite.org/uri.html
74
75 """
76 uri = 'file:' + database + '?mode=ro'
77 with contextlib.closing(sqlite3.connect(uri, uri=True)) as connect:
78 connect.row_factory = sqlite3.Row
79 with contextlib.closing(connect.cursor()) as cursor:
80 yield cursor
81
82

Referenced by searx.engines.sqlite.search().

+ Here is the caller graph for this function:

Variable Documentation

◆ database

str searx.engines.sqlite.database = ""

Definition at line 45 of file sqlite.py.

◆ engine_type

str searx.engines.sqlite.engine_type = 'offline'

Definition at line 43 of file sqlite.py.

◆ limit

int searx.engines.sqlite.limit = 10

Definition at line 51 of file sqlite.py.

◆ paging

bool searx.engines.sqlite.paging = True

Definition at line 52 of file sqlite.py.

◆ query_str

str searx.engines.sqlite.query_str = ""

Definition at line 48 of file sqlite.py.

◆ result_template

str searx.engines.sqlite.result_template = 'key-value.html'

Definition at line 53 of file sqlite.py.