.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
mongodb.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""MongoDB_ is a document based database program that handles JSON like data.
3Before configuring the ``mongodb`` engine, you must install the dependency
4pymongo_.
5
6Configuration
7=============
8
9In order to query MongoDB_, you have to select a ``database`` and a
10``collection``. Furthermore, you have to select a ``key`` that is going to be
11searched. MongoDB_ also supports the option ``exact_match_only``, so configure
12it as you wish.
13
14Example
15=======
16
17Below is an example configuration for using a MongoDB collection:
18
19.. code:: yaml
20
21 # MongoDB engine
22 # Required dependency: pymongo
23
24 - name: mymongo
25 engine: mongodb
26 shortcut: md
27 exact_match_only: false
28 host: '127.0.0.1'
29 port: 27017
30 enable_http: true
31 results_per_page: 20
32 database: 'business'
33 collection: 'reviews' # name of the db collection
34 key: 'name' # key in the collection to search for
35
36Implementations
37===============
38
39"""
40
41import re
42
43try:
44 from pymongo import MongoClient # type: ignore
45except ImportError:
46 # import error is ignored because the admin has to install pymongo manually
47 # to use the engine
48 pass
49
50from searx.result_types import EngineResults
51
52
53engine_type = 'offline'
54
55# mongodb connection variables
56host = '127.0.0.1'
57port = 27017
58username = ''
59password = ''
60database = None
61collection = None
62key = None
63
64# engine specific variables
65paging = True
66results_per_page = 20
67exact_match_only = False
68
69_client = None
70
71
72def init(_):
73 connect()
74
75
76def connect():
77 global _client # pylint: disable=global-statement
78 kwargs: dict[str, str | int] = {'port': port}
79 if username:
80 kwargs['username'] = username
81 if password:
82 kwargs['password'] = password
83 _client = MongoClient(host, **kwargs)[database][collection]
84
85
86def search(query, params) -> EngineResults:
87 res = EngineResults()
88 if exact_match_only:
89 q = {'$eq': query}
90 else:
91 _re = re.compile('.*{0}.*'.format(re.escape(query)), re.I | re.M)
92 q = {'$regex': _re}
93
94 query = _client.find({key: q}).skip((params['pageno'] - 1) * results_per_page).limit(results_per_page)
95
96 res.add(res.types.LegacyResult(number_of_results=query.count()))
97 for row in query:
98 del row['_id']
99 kvmap = {str(k): str(v) for k, v in row.items()}
100 res.add(res.types.KeyValue(kvmap=kvmap))
101
102 return res