.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
demo_offline.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Within this module we implement a *demo offline engine*. Do not look to
3close to the implementation, its just a simple example. To get in use of this
4*demo* engine add the following entry to your engines list in ``settings.yml``:
5
6.. code:: yaml
7
8 - name: my offline engine
9 engine: demo_offline
10 shortcut: demo
11 disabled: false
12
13"""
14
15import json
16
17from searx.result_types import EngineResults
18from searx.enginelib import EngineCache
19
20engine_type = 'offline'
21categories = ['general']
22disabled = True
23timeout = 2.0
24
25about = {
26 "wikidata_id": None,
27 "official_api_documentation": None,
28 "use_official_api": False,
29 "require_api_key": False,
30 "results": 'JSON',
31}
32
33# if there is a need for globals, use a leading underline
34_my_offline_engine: str = ""
35
36CACHE: EngineCache
37"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
38seconds."""
39
40
41def init(engine_settings):
42 """Initialization of the (offline) engine. The origin of this demo engine is a
43 simple json string which is loaded in this example while the engine is
44 initialized."""
45 global _my_offline_engine, CACHE # pylint: disable=global-statement
46
47 CACHE = EngineCache(engine_settings["name"]) # type:ignore
48
49 _my_offline_engine = (
50 '[ {"value": "%s"}'
51 ', {"value":"first item"}'
52 ', {"value":"second item"}'
53 ', {"value":"third item"}'
54 ']' % engine_settings.get('name')
55 )
56
57
58def search(query, request_params) -> EngineResults:
59 """Query (offline) engine and return results. Assemble the list of results
60 from your local engine. In this demo engine we ignore the 'query' term,
61 usual you would pass the 'query' term to your local engine to filter out the
62 results.
63 """
64 res = EngineResults()
65 count = CACHE.get("count", 0)
66
67 for row in json.loads(_my_offline_engine):
68 count += 1
69 kvmap = {
70 'query': query,
71 'language': request_params['searxng_locale'],
72 'value': row.get("value"),
73 }
74 res.add(
75 res.types.KeyValue(
76 caption=f"Demo Offline Engine Result #{count}",
77 key_title="Name",
78 value_title="Value",
79 kvmap=kvmap,
80 )
81 )
82 res.add(res.types.LegacyResult(number_of_results=count))
83
84 # cache counter value for 20sec
85 CACHE.set("count", count, expire=20)
86 return res