.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 51 of file sqlite.py.

51def init(engine_settings):
52 if 'query_str' not in engine_settings:
53 raise ValueError('query_str cannot be empty')
54
55 if not engine_settings['query_str'].lower().startswith('select '):
56 raise ValueError('only SELECT query is supported')
57
58
59@contextlib.contextmanager

◆ search()

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

Definition at line 78 of file sqlite.py.

78def search(query, params):
79 results = []
80
81 query_params = {
82 'query': query,
83 'wildcard': r'%' + query.replace(' ', r'%') + r'%',
84 'limit': limit,
85 'offset': (params['pageno'] - 1) * limit,
86 }
87 query_to_run = query_str + ' LIMIT :limit OFFSET :offset'
88
89 with sqlite_cursor() as cur:
90
91 cur.execute(query_to_run, query_params)
92 col_names = [cn[0] for cn in cur.description]
93
94 for row in cur.fetchall():
95 item = dict(zip(col_names, map(str, row)))
96 item['template'] = result_template
97 logger.debug("append result --> %s", item)
98 results.append(item)
99
100 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 60 of file sqlite.py.

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

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 44 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 46 of file sqlite.py.

◆ paging

bool searx.engines.sqlite.paging = True

Definition at line 47 of file sqlite.py.

◆ query_str

str searx.engines.sqlite.query_str = ""

Definition at line 45 of file sqlite.py.

◆ result_template

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

Definition at line 48 of file sqlite.py.