.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
redis_server.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Redis is an open source (BSD licensed), in-memory data structure (key value
3based) store. Before configuring the ``redis_server`` engine, you must install
4the dependency redis_.
5
6Configuration
7=============
8
9Select a database to search in and set its index in the option ``db``. You can
10either look for exact matches or use partial keywords to find what you are
11looking for by configuring ``exact_match_only``.
12
13Example
14=======
15
16Below is an example configuration:
17
18.. code:: yaml
19
20 # Required dependency: redis
21
22 - name: myredis
23 shortcut : rds
24 engine: redis_server
25 exact_match_only: false
26 host: '127.0.0.1'
27 port: 6379
28 enable_http: true
29 password: ''
30 db: 0
31
32Implementations
33===============
34
35"""
36
37import redis # pylint: disable=import-error
38
39engine_type = 'offline'
40
41# redis connection variables
42host = '127.0.0.1'
43port = 6379
44password = ''
45db = 0
46
47# engine specific variables
48paging = False
49result_template = 'key-value.html'
50exact_match_only = True
51
52_redis_client = None
53
54
55def init(_engine_settings):
56 global _redis_client # pylint: disable=global-statement
57 _redis_client = redis.StrictRedis(
58 host=host,
59 port=port,
60 db=db,
61 password=password or None,
62 decode_responses=True,
63 )
64
65
66def search(query, _params):
67 if not exact_match_only:
68 return search_keys(query)
69
70 ret = _redis_client.hgetall(query)
71 if ret:
72 ret['template'] = result_template
73 return [ret]
74
75 if ' ' in query:
76 qset, rest = query.split(' ', 1)
77 ret = []
78 for res in _redis_client.hscan_iter(qset, match='*{}*'.format(rest)):
79 ret.append(
80 {
81 res[0]: res[1],
82 'template': result_template,
83 }
84 )
85 return ret
86 return []
87
88
89def search_keys(query):
90 ret = []
91 for key in _redis_client.scan_iter(match='*{}*'.format(query)):
92 key_type = _redis_client.type(key)
93 res = None
94
95 if key_type == 'hash':
96 res = _redis_client.hgetall(key)
97 elif key_type == 'list':
98 res = dict(enumerate(_redis_client.lrange(key, 0, -1)))
99
100 if res:
101 res['template'] = result_template
102 res['redis_key'] = key
103 ret.append(res)
104 return ret
init(_engine_settings)