[mod] migrate all key-value.html templates to KeyValue type

The engines now all use KeyValue results and return the results in a
EngineResults object.

The sqlite engine can return MainResult results in addition to KeyValue
results (based on engine's config in settings.yml),

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2025-03-05 17:50:22 +01:00 committed by Markus Heiser
parent af5dbdf768
commit f49b2c94a9
13 changed files with 160 additions and 172 deletions

View file

@ -37,6 +37,7 @@ Implementations
===============
"""
from __future__ import annotations
import re
@ -47,6 +48,8 @@ except ImportError:
# to use the engine
pass
from searx.result_types import EngineResults
engine_type = 'offline'
@ -63,7 +66,6 @@ key = None
paging = True
results_per_page = 20
exact_match_only = False
result_template = 'key-value.html'
_client = None
@ -74,7 +76,7 @@ def init(_):
def connect():
global _client # pylint: disable=global-statement
kwargs = {'port': port}
kwargs: dict[str, str | int] = {'port': port}
if username:
kwargs['username'] = username
if password:
@ -82,8 +84,8 @@ def connect():
_client = MongoClient(host, **kwargs)[database][collection]
def search(query, params):
results = []
def search(query, params) -> EngineResults:
res = EngineResults()
if exact_match_only:
q = {'$eq': query}
else:
@ -92,11 +94,10 @@ def search(query, params):
query = _client.find({key: q}).skip((params['pageno'] - 1) * results_per_page).limit(results_per_page)
results.append({'number_of_results': query.count()})
for r in query:
del r['_id']
r = {str(k): str(v) for k, v in r.items()}
r['template'] = result_template
results.append(r)
res.add(res.types.LegacyResult(number_of_results=query.count()))
for row in query:
del row['_id']
kvmap = {str(k): str(v) for k, v in row.items()}
res.add(res.types.KeyValue(kvmap=kvmap))
return results
return res