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§
- Search
Doc - One page’s searchable text, handed to an external
SearchBackendto (re)index.page_idis the document id.
Traits§
- Search
Backend - 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 behindArc<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
queryabovethreshold(0.0–1.0), orNone. Postgres + thepg_trgmextension only —Noneon any other dialect, whenpg_trgmisn’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_idsscale — an expression index over the exactto_tsvector(...)the query uses, so the planner can use it instead of a seq scan. Idempotent (IF NOT EXISTS); Postgres-only (no-op +Okon other dialects).crate::seed::ensure_seededruns 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, elsesearch_page_ids.Nonemeans “no ranking here” and the caller keeps its substring match. - search_
page_ ids - Page ids matching
query, ranked by relevance (best first), capped atlimit.Someonly on Postgres;Noneon any other dialect or on a query error → the caller uses its substring fallback. - search_
page_ ids_ all_ statuses - Like
search_page_idsbut 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);Noneelsewhere → 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.