Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

19.3 KB
Raw Blame
"""
Unified Search Routes — "Search everything" (FastAPI).

One search surface across every entity stored as a ``Document`` row
(notes, research reports, library downloads, user uploads):

* GET /library/search/              — the Search page
* GET /library/search/api/keyword   — ILIKE over the listed name + text_content
* GET /library/search/api/semantic  — FAISS search over the system
  collections (default_library, research_history, notes)

The two API legs return the SAME result-row contract so the page JS can
merge them mode-agnostically::

    {id, title, content_preview, source_type, url, similarity?}

``url`` is computed server-side (see ``_document_url``) so every future
consumer links results consistently.

Ported from the Flask ``web/routes/unified_search_routes.py`` blueprint.
"""

import math

from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse

from ...research_library.utils import escape_like, handle_api_error
from ..dependencies.auth import require_auth
from ..dependencies.rate_limit import limiter, _user_key
from ..template_config import templates

# MAX_SEARCH_LEN (the user-query cap into ILIKE + the embedding call) is
# shared with notes_routes via _search_constants so the two route modules
# can't diverge.
from ..routes._search_constants import MAX_SEARCH_LEN
from typing import Annotated

# Keyword-leg preview length: column-projected ``substr`` so the full
# text_content (which can be megabytes for extracted PDFs) is never
# hydrated into the response.
PREVIEW_LEN = 300
# Lead-in shown before the first content match when the preview window is
# anchored on it (see keyword_search).
PREVIEW_CONTEXT = 60
DEFAULT_LIMIT = 20
MAX_LIMIT = 50
DEFAULT_MIN_SIMILARITY = 0.25

# Minimum query length, matching the page JS's UNIFIED_SEARCH_MIN_QUERY:
# below 2 chars the keyword leg is an expensive full-content scan for a
# near-meaningless match set, and the client never sends one anyway.
MIN_SEARCH_LEN = 2

# System collections always searched by the semantic leg, keyed by
# ``Collection.collection_type``. These are lazily created elsewhere
# (library init / first note), so a missing one is SKIPPED rather than
# created — a read endpoint must not have write side effects. User
# collections join them whenever they have a current RAG index (see
# semantic_search) so the two legs cover the same corpus for anything
# the user actually indexed.
SEMANTIC_COLLECTION_TYPES = ("default_library", "research_history", "notes")

# Per-user search budget — same shape as the notes_search bucket (cheap
# DB/FAISS lookups typed through a debounced search box).
_unified_search_limit = limiter.shared_limit(
    "60 per minute", scope="unified_search", key_func=_user_key
)

router = APIRouter(prefix="/library/search", tags=["unified-search"])


def _document_url(source_type_name, document_id, research_id):
    """Canonical link for a search hit, by source type.

    * ``note`` → the note editor.
    * ``research_report`` with a ``research_id`` → the research results
      page (the report Document is a search-index copy of that run).
    * everything else (uploads, downloads, report copies whose run is
      gone) → the library document details page.
    """
    if source_type_name == "note":
        return f"/notes/{document_id}"
    if source_type_name == "research_report" and research_id:
        return f"/results/{research_id}"
    return f"/library/document/{document_id}"


def _validated_query_and_limit(request: Request):
    """Parse/validate the shared ``q`` and ``limit`` query params.

    Returns ``(query, limit, error_response)`` where ``error_response``
    is a ready ``JSONResponse`` when validation failed (and the other
    fields are None).
    """
    q = request.query_params.get("q", "")
    if len(q) > MAX_SEARCH_LEN:
        return (
            None,
            None,
            JSONResponse(
                {
                    "success": False,
                    "error": f"q exceeds maximum length ({MAX_SEARCH_LEN} chars)",
                },
                status_code=400,
            ),
        )
    q = q.strip()
    if not q:
        return (
            None,
            None,
            JSONResponse(
                {"success": False, "error": "Query is required"},
                status_code=400,
            ),
        )
    if len(q) < MIN_SEARCH_LEN:
        return (
            None,
            None,
            JSONResponse(
                {
                    "success": False,
                    "error": (
                        f"Query must be at least {MIN_SEARCH_LEN} characters"
                    ),
                },
                status_code=400,
            ),
        )
    try:
        limit = max(
            1,
            min(
                int(request.query_params.get("limit", DEFAULT_LIMIT)), MAX_LIMIT
            ),
        )
    except (ValueError, TypeError):
        return (
            None,
            None,
            JSONResponse(
                {"success": False, "error": "Invalid limit"}, status_code=400
            ),
        )
    return q, limit, None


# ============================================================================
# Page Route
# ============================================================================


@router.get("/")
def unified_search_page(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Render the unified Search page."""
    return templates.TemplateResponse(
        request=request,
        name="pages/unified_search.html",
        context={"active_page": "unified-search"},
    )


# ============================================================================
# API Routes
# ============================================================================


@router.get("/api/keyword")
@_unified_search_limit
def keyword_search(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Keyword leg: ILIKE over the listed name + text_content, ALL source types.

    The listed name is ``title or filename`` (``display_title`` below), so
    the filename is searched only when the title is NULL or empty, as for
    an uploaded document. A titled document is not matched by its stored
    filename. ILIKE folds case for ASCII letters only.

    Query params:
        q: Search query (required, capped at MAX_SEARCH_LEN)
        limit: Maximum results (default 20, clamped 1..50)

    Column-projected (id, title, filename, 300-char preview, source-type name,
    updated_at, research_id) — never hydrates full text_content.
    """
    try:
        q, limit, error = _validated_query_and_limit(request)
        if error:
            return error

        from sqlalchemy import and_, case, func, or_

        from ...database.models.library import Document, SourceType
        from ...database.session_context import get_user_db_session

        pattern = f"%{escape_like(q)}%"
        # Center the preview on the first content match instead of always
        # slicing the head: for a content hit deep in a long document, the
        # first 300 chars usually don't contain the matched text at all,
        # so the card gave no clue why the document matched. instr() takes
        # the raw query (wildcard escaping only applies to LIKE), folded
        # with lower() on both sides exactly like ilike; 0 = name-only
        # match → head preview. PREVIEW_CONTEXT chars of lead-in keep the
        # match readable in situ.
        match_pos = func.instr(func.lower(Document.text_content), func.lower(q))
        preview_start = case(
            (match_pos > PREVIEW_CONTEXT, match_pos - PREVIEW_CONTEXT),
            else_=1,
        )
        with get_user_db_session(username) as db:
            rows = (
                db.query(
                    Document.id,
                    Document.title,
                    Document.filename,
                    func.substr(
                        Document.text_content, preview_start, PREVIEW_LEN
                    ),
                    SourceType.name,
                    Document.updated_at,
                    Document.research_id,
                    match_pos,
                )
                .join(SourceType, Document.source_type_id == SourceType.id)
                .filter(
                    # Only surface completed documents, matching every
                    # library listing/search path (library_service filters
                    # status == 'completed' too). Without this, in-progress
                    # or failed downloads — partial/empty text, or a stale
                    # title — appear as hits that link to a document page
                    # with no usable content.
                    Document.status == "completed",
                    or_(
                        Document.title.ilike(pattern, escape="\\"),
                        # Results are listed as ``title or filename``, so
                        # the filename is searched exactly where it is
                        # the listed name.
                        and_(
                            or_(
                                Document.title.is_(None),
                                Document.title == "",
                            ),
                            Document.filename.ilike(pattern, escape="\\"),
                        ),
                        Document.text_content.ilike(pattern, escape="\\"),
                    ),
                )
                .order_by(Document.updated_at.desc())
                .limit(limit)
                .all()
            )

        results = []
        for (
            doc_id,
            title,
            filename,
            preview,
            source_type_name,
            updated_at,
            research_id,
            pos,
        ) in rows:
            preview = preview or ""
            # Mark the cut ONLY when the window truly opens mid-document, i.e.
            # preview_start = pos - PREVIEW_CONTEXT > 1, which is pos >
            # PREVIEW_CONTEXT + 1. At exactly pos == PREVIEW_CONTEXT + 1 the SQL
            # clamps preview_start to 1 (the document's first char), so the
            # preview isn't truncated and a leading "…" would be spurious.
            if pos and pos > PREVIEW_CONTEXT + 1:
                preview = "…" + preview
            # Fall back to filename (set by user uploads) when title is NULL,
            # so uploaded documents without an explicit title still surface a
            # useful name instead of the generic 'Untitled'. Filename is
            # already werkzeug-sanitized (spaces -> underscores) at upload
            # time, so what we return matches what the semantic leg already
            # shows. Final 'Untitled' fallback only fires when both title
            # AND filename are missing (defensive — every current code path
            # sets at least one of them).
            display_title = title or filename or "Untitled"
            results.append(
                {
                    "id": doc_id,
                    "title": display_title,
                    "content_preview": preview,
                    "source_type": source_type_name,
                    "updated_at": updated_at.isoformat()
                    if updated_at
                    else None,
                    "research_id": research_id,
                    "url": _document_url(source_type_name, doc_id, research_id),
                }
            )

        return {
            "success": True,
            "query": q,
            "results": results,
            "count": len(results),
        }

    except Exception as e:
        return handle_api_error("unified keyword search", e)


@router.get("/api/semantic")
@_unified_search_limit
def semantic_search(
    request: Request, username: Annotated[str, Depends(require_auth)]
):
    """Semantic leg: FAISS search over the system collections.

    Query params:
        q: Search query (required, capped at MAX_SEARCH_LEN)
        limit: Maximum results (default 20, clamped 1..50)
        min_similarity: Minimum similarity 0.0-1.0 (default 0.25)

    Searches the RAG index of every existing system collection
    (default_library, research_history, notes — missing ones are
    skipped, not created) and merges the hits across collections by
    similarity desc, one result per document (its best-scoring chunk).

    Infrastructure failures PROPAGATE as a 500 via ``handle_api_error``
    — an outage must not masquerade as "no matches" (repo rule: no
    silent fallbacks). The page's hybrid mode degrades per leg
    client-side, where the degradation is surfaced as a notice.
    """
    try:
        q, limit, error = _validated_query_and_limit(request)
        if error:
            return error
        try:
            min_similarity_raw = float(
                request.query_params.get(
                    "min_similarity", DEFAULT_MIN_SIMILARITY
                )
            )
            # Reject nan/inf explicitly: float("nan") does not raise, and
            # max(0.0, min(nan, 1.0)) silently collapses to 0.0 (the most
            # permissive threshold) instead of surfacing the bad input.
            if not math.isfinite(min_similarity_raw):
                raise ValueError(  # noqa: TRY301 — caught just below and mapped to a 400
                    "min_similarity must be a finite number"
                )
            min_similarity = max(0.0, min(min_similarity_raw, 1.0))
        except (ValueError, TypeError):
            return JSONResponse(
                {"success": False, "error": "Invalid min_similarity"},
                status_code=400,
            )

        from ...database.models.library import (
            Collection,
            Document,
            RAGIndex,
            SourceType,
        )
        from ...database.session_context import get_user_db_session

        # Resolve the searchable collections: the system collections that
        # exist, plus ANY other collection with a current RAG index. The
        # keyword leg covers every document, so scoping the semantic leg
        # to system collections only made the two modes cover different
        # corpora — documents living solely in an indexed user collection
        # were keyword-searchable but never AI-searchable. Unindexed
        # collections are harmless to include (the engine returns [] when
        # no current index exists), but filtering here avoids paying its
        # per-collection setup for collections that can't match.
        with get_user_db_session(username) as db:
            all_collections = db.query(
                Collection.id,
                Collection.name,
                Collection.collection_type,
            ).all()
            indexed_names = {
                name
                for (name,) in db.query(RAGIndex.collection_name)
                .filter(RAGIndex.is_current.is_(True))
                .all()
            }
        collections = [
            (collection_id, collection_name)
            for collection_id, collection_name, collection_type in all_collections
            if collection_type in SEMANTIC_COLLECTION_TYPES
            or f"collection_{collection_id}" in indexed_names
        ]

        if not collections:
            return {
                "success": True,
                "query": q,
                "results": [],
                "count": 0,
            }

        from ...web_search_engines.engines.search_engine_collection import (
            CollectionSearchEngine,
        )

        # Over-fetch chunk-level hits per collection: documents are
        # chunk-indexed, so one long document can occupy several top-k
        # slots before the per-document dedup below.
        fetch_k = limit * 3
        hits = []
        for collection_id, collection_name in collections:
            engine = CollectionSearchEngine(
                collection_id=collection_id,
                collection_name=collection_name,
                max_results=fetch_k,
                settings_snapshot={"_username": username},
            )
            # Engine failures propagate to handle_api_error below.
            raw_results = engine.search(q, limit=fetch_k)
            for r in raw_results or []:
                score = r.get("relevance_score", 0)
                if score < min_similarity:
                    continue
                meta = r.get("metadata", {}) or {}
                doc_id = meta.get("document_id") or meta.get("source_id")
                if not doc_id:
                    continue
                hits.append(
                    {
                        "id": doc_id,
                        "title": r.get("title", "Untitled"),
                        "content_preview": (r.get("snippet", "") or "")[
                            :PREVIEW_LEN
                        ],
                        "similarity": float(score),
                    }
                )

        # Merge across collections by similarity desc; keep each
        # document's best-scoring chunk only. Dedup the WHOLE pool (not
        # just the first `limit`) so the existence check below can backfill
        # from lower-ranked valid hits when a top hit turns out to be a
        # FAISS orphan — otherwise a handful of orphans in the top `limit`
        # silently shrink the result set even though enough valid matches
        # were fetched.
        hits.sort(key=lambda h: h["similarity"], reverse=True)
        candidates = []
        seen_doc_ids = set()
        for hit in hits:
            if hit["id"] in seen_doc_ids:
                continue
            seen_doc_ids.add(hit["id"])
            candidates.append(hit)

        # Enrich with the Document row (source type + research_id → url).
        # A candidate absent from the lookup is a FAISS orphan — the
        # document was deleted but its vectors linger until reindex — so
        # drop it rather than render a link that 404s, and only cut to
        # `limit` AFTER dropping orphans.
        results = []
        if candidates:
            with get_user_db_session(username) as db:
                rows = (
                    db.query(Document.id, SourceType.name, Document.research_id)
                    .join(SourceType, Document.source_type_id == SourceType.id)
                    .filter(
                        Document.id.in_([h["id"] for h in candidates]),
                        # Match the keyword leg: don't surface in-progress
                        # or failed documents even if their vectors exist.
                        Document.status == "completed",
                    )
                    .all()
                )
            doc_info = {
                doc_id: (source_type_name, research_id)
                for doc_id, source_type_name, research_id in rows
            }
            for hit in candidates:
                info = doc_info.get(hit["id"])
                if info is None:
                    continue
                source_type_name, research_id = info
                hit["source_type"] = source_type_name
                hit["url"] = _document_url(
                    source_type_name, hit["id"], research_id
                )
                hit["similarity"] = round(hit["similarity"], 3)
                results.append(hit)
                if len(results) >= limit:
                    break

        return {
            "success": True,
            "query": q,
            "results": results,
            "count": len(results),
        }

    except Exception as e:
        return handle_api_error("unified semantic search", e)