From 4918ea5ef6b6fce2608a780db0967a134b681632 Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 20 Mar 2026 14:08:26 -0600 Subject: [PATCH] Creating Zim search endpoint that searches all auto-search zim files. --- modules/zim/api.py | 13 +++++++++++++ modules/zim/lib.py | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/modules/zim/api.py b/modules/zim/api.py index 60ddd88a..bd804531 100644 --- a/modules/zim/api.py +++ b/modules/zim/api.py @@ -57,6 +57,19 @@ async def post_set_zim_auto_search(request: Request, zim_id: int, body: schema.Z return response.empty() +@zim_bp.post('/search') +@openapi.definition( + summary='Search all Zim files that have auto_search enabled', + body=schema.ZimSearchRequest, +) +@validate(schema.ZimSearchRequest) +async def search_all_zims(request: Request, body: schema.ZimSearchRequest): + session = request.ctx.session + results = lib.search_all_zims(session, body.search_str, tag_names=body.tag_names, offset=body.offset, + limit=body.limit) + return json_response({'zims': results}) + + @zim_bp.post('/search/') @openapi.definition( summary='Search all entries of a Zim', diff --git a/modules/zim/lib.py b/modules/zim/lib.py index d60d2ce8..5c9189b7 100644 --- a/modules/zim/lib.py +++ b/modules/zim/lib.py @@ -245,6 +245,28 @@ def headline_zim(session: Session, search_str: str, zim_id: int, tag_names: List return results +def search_all_zims(session: Session, search_str: str, tag_names: List[str] = None, offset: int = 0, + limit: int = 10) -> List[Dict]: + """Search all Zim files that have auto_search enabled. + + Returns a list of per-zim result dicts, each containing metadata and search results. + Each search entry includes `zim_id` so callers can fetch the full entry afterward.""" + zims = Zims.get_all(session) + all_results = [] + for zim in zims: + if not zim.auto_search: + continue + try: + result = headline_zim(session, search_str, zim.id, tag_names=tag_names, offset=offset, limit=limit) + if result.get('search'): + for entry in result['search']: + entry['zim_id'] = zim.id + all_results.append(result) + except Exception as e: + logger.error(f'Failed to search Zim {zim}', exc_info=e) + return all_results + + @cachetools.func.mru_cache(maxsize=1_000) def get_estimates(search_str: str) -> List[int]: zims = Zims.get_all()