Creating Zim search endpoint that searches all auto-search zim files.

This commit is contained in:
Roland 2026-03-20 14:08:26 -06:00 • committed by lrnselfreliance
parent e6aaab1016
commit 4918ea5ef6
2 changed files with 35 additions and 0 deletions

View file

@ -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/<zim_id:[0-9]+>')
@openapi.definition(
summary='Search all entries of a Zim',

View file

@ -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()