Skip to main content

Module search

Module search 

Source
Expand description

Database full-text search backend.

Admin + API search has been client-side LIKE substring matching — no relevance ranking, poor scaling. This adds a Postgres full-text path: to_tsvector / ts_rank with websearch_to_tsquery (which safely parses visitor query syntax — quotes, OR, -term).

Dialect-gated. search_page_ids returns Some(ranked_ids) only when the pool is Postgres (rustango::sql::Pool::as_postgres); on SQLite / MySQL (or a PG query error) it returns None, and the caller falls back to the existing substring filter. So this is purely additive — non-PG deployments are unchanged.

v1 indexes the canonical text (title + seo_title + seo_description) on the fly — no stored column required, correct without a migration. A GIN index + extension-body indexing (via the task queue) are follow-ups for scale.

Structs§

SearchDoc
One page’s searchable text, handed to an external SearchBackend to (re)index. page_id is the document id.

Traits§

SearchBackend
An external full-text backend (Elasticsearch, …). When one is installed via set_backend, it takes priority over the built-in Postgres path: search goes to the backend, and pages are indexed / removed through it. Object-safe so it can live behind Arc<dyn …>.

Functions§

backend
The installed external backend, if any.
did_you_mean
Fuzzy “did you mean?” suggestion: the published page title most trigram-similar to query above threshold (0.0–1.0), or None. Postgres + the pg_trgm extension only — None on any other dialect, when pg_trgm isn’t installed, on a query error, or when the closest title is just the query itself.
ensure_pg_fts_index
Create the GIN index that makes search_page_ids scale — an expression index over the exact to_tsvector(...) the query uses, so the planner can use it instead of a seq scan. Idempotent (IF NOT EXISTS); Postgres-only (no-op + Ok on other dialects). crate::seed::ensure_seeded runs it for every tenant at boot.
ranked_public_page_ids
Ranked page ids for a public search: the installed external backend (set_backend) when there is one, else search_page_ids. None means “no ranking here” and the caller keeps its substring match.
search_page_ids
Page ids matching query, ranked by relevance (best first), capped at limit. Some only on Postgres; None on any other dialect or on a query error → the caller uses its substring fallback.
search_page_ids_all_statuses
Like search_page_ids but spans every status (drafts / scheduled / expired too) — for the admin explorer’s search, which must surface unpublished pages. Postgres-only (the public index is public-status-only); None elsewhere → admin keeps its substring path.
set_backend
Install an external search backend (e.g. Elasticsearch). Call once at startup. Without it, search uses the built-in Postgres FTS path (search_page_ids) / substring fallback.