mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 02:48:38 -04:00
Creating Zim search endpoint that searches all auto-search zim files.
This commit is contained in:
parent
e6aaab1016
commit
4918ea5ef6
2 changed files with 35 additions and 0 deletions
|
|
@ -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',
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue