wrolpi/modules/videos/video/api.py
Roland 73fafcd645
Search: fast ABC mode by default, opt-in deep search
The file_group.textsearch tsvector indexes all four text columns;
d_text (captions, document text, zip contents) is ~90% of the data,
making ts_rank searches take seconds on large libraries.  Add a
textsearch_abc generated column (title/author/description only, ~8x
smaller) and default all searches to it, with deep=true opting into the
full column.

Database:
- Migration adds the generated column and a GIN index (rewrites the
  table; a few minutes on large libraries).

Backend:
- FilesSearchRequest, VideoSearchRequest, ArchiveSearchRequest, and
  DocSearchRequest accept `deep`; each search function picks the column.
- /api/search_file_estimates returns both counts in one index-bound
  query (COUNT FILTER on the abc subset): `file_groups` (fast) and
  `file_groups_deep`.

UI (one coherent story across the count surfaces):
- The typing dropdown always suggests the deep count (what is findable).
- The tab badge shows the active mode's count so it matches the result
  list.
- The gap is reconciled by a hint offering deep search: count-aware on
  the global search ("No results in fast search. N results available in
  deep search." / "N more results available..."), generic on the module
  pages (no deep estimate there).  The hint resets pagination.
- "Search Depth" toggle in the search filter modals, persisted as
  ?deep=true.

Tests that relied on d_text matches now pass deep=True, with fast-mode
assertions alongside.  Also skip flaky test_download_channel on
CircleCI.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 10:32:16 -06:00

75 lines
2.5 KiB
Python

from http import HTTPStatus
from sanic import Blueprint
from sanic.request import Request
from sanic_ext import validate
from sanic_ext.extensions.openapi import openapi
from wrolpi.api_utils import json_response
from wrolpi.common import logger
from wrolpi.errors import InvalidOrderBy
from wrolpi.schema import JSONErrorResponse
from . import lib
from .. import schema
video_bp = Blueprint('Video', '/api/videos')
logger = logger.getChild(__name__)
@video_bp.get('/<file_group_id:int>')
@openapi.description('Get Video information')
@openapi.response(HTTPStatus.OK, schema.VideoResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
def video_get(request: Request, file_group_id: int):
skip_viewed = request.args.get('skip_viewed', '').lower() == 'true'
video, previous_video, next_video = lib.get_video_for_app(file_group_id, skip_viewed=skip_viewed)
return json_response({'file_group': video, 'prev': previous_video, 'next': next_video})
@video_bp.get('/<file_group_id:int>/comments')
@openapi.description('Get Video comments')
@openapi.response(HTTPStatus.OK, schema.VideoCommentsResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
def video_get_comments(_: Request, file_group_id: int):
video = lib.get_video(file_group_id)
return json_response({'comments': video.get_comments()})
@video_bp.get('/<file_group_id:int>/captions')
@openapi.description('Get Video captions')
@openapi.response(HTTPStatus.OK, schema.VideoCaptionsResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
def video_get_captions(_: Request, file_group_id: int):
video = lib.get_video(file_group_id)
return json_response({'captions': video.get_caption_chunks()})
@video_bp.post('/search')
@openapi.definition(
summary='Search Video titles and captions',
body=schema.VideoSearchRequest,
)
@openapi.response(HTTPStatus.OK, schema.VideoSearchResponse)
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
@validate(schema.VideoSearchRequest)
async def search_videos(_: Request, body: schema.VideoSearchRequest):
if body.order_by not in lib.VIDEO_ORDERS:
raise InvalidOrderBy('Invalid order by')
file_groups, videos_total = lib.search_videos(
body.search_str,
body.offset,
body.limit,
body.channel_id,
body.order_by,
body.tag_names,
body.headline,
body.censored,
body.deep,
)
ret = {'file_groups': list(file_groups), 'totals': {'file_groups': videos_total}}
return json_response(ret)