Skip to content

perf(admin): serve the content-list search filter from the FTS5 index - #1857

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
swissky:perf/admin-search-fts
Jul 10, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
swissky:perf/admin-search-fts

Conversation

@swissky

@swissky swissky commented Jul 7, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Fixes #1517 (milestone 1.0): the admin content-list search box generated

lower("slug") like lower(?) escape '\' or lower("title") like lower(?) escape '\'

with a %term% pattern — doubly unindexable (leading wildcard + lower() wrapping the column) — so every keystroke scanned all non-deleted rows in the locale, twice (page fetch + the count for the pagination denominator). On D1 these are primary reads with a terrible rows-read/rows-returned ratio.

As the issue suggests, the filter now uses the FTS layer EmDash already ships. When the collection has search enabled and the index covers the display columns, the q filter becomes:

id IN (SELECT id FROM _emdash_fts_<slug> WHERE _emdash_fts_<slug> MATCH '"term"*')
OR slug GLOB 'term*'

Both sides are index-backed. EXPLAIN QUERY PLAN before/after on a seeded table:

OLD:  SCAN ec_posts
NEW:  MULTI-INDEX OR
        SCAN _emdash_fts_posts VIRTUAL TABLE INDEX 0:M3   (inverted index)
        SEARCH ec_posts USING INDEX idx_ec_posts_slug (slug>? AND slug<?)

Design notes:

  • Routing is conservative. useFts is set by the handler only when: SQLite (FTS5 is SQLite-only), search_config.enabled, every non-slug display column (title/name) is in the searchable-field set (otherwise FTS would miss matches LIKE finds), and the FTS table exists. Everything else — Postgres, search disabled, uncovered columns — keeps the existing LIKE fallback, unchanged.
  • The MATCH expression cannot syntax-error. A new buildFtsPrefixMatch helper quotes every term (interior quotes doubled) and treats FTS operators as plain words — unlike the public search API's escapeQuery, which intentionally passes operators through. A list filter box is not a search-syntax field.
  • Slug lookups keep working. The slug is not in the FTS index, so the filter OR's an index-served slug GLOB 'term*' prefix (GLOB metacharacters escaped via character classes; GLOB's case-sensitive BINARY comparison is what lets SQLite use the ordinary slug index, and slugs are lowercase by construction).
  • Semantics change on FTS-enabled collections from arbitrary substring to token-prefix matching (need head finds "Needle Headline"; mid-word eedle no longer matches). That's standard search behavior and what the FTS index can serve; the count and the page use the same filter, so the pagination denominator stays consistent.
  • Index integrity is already handled elsewhere: boot-time verifyAndRepairAll plus migration 039.

Tests: unit tests for the two expression builders; integration tests for the FTS path (title token prefix, case-insensitivity, multi-word, slug prefix, filtered total, literal quotes, operators as plain words, soft-delete sync) and for the LIKE fallback when a display column isn't covered by the index. The existing content-list-search.test.ts (both dialects) passes unchanged.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (n/a — no admin UI changes)
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: (n/a — fixes the triaged milestone-1.0 issue Admin content-list search filter does an unindexable full-table scan #1517, which already outlines this approach)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Cursor + Fable 5

Screenshots / test output

✓ tests/unit/search/match.test.ts (6 tests)
✓ tests/integration/content/content-list-search-fts.test.ts (10 tests)
✓ tests/integration/content + tests/integration/search + content-handlers (90 tests total)

@changeset-bot

changeset-bot Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7bfffbf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1857

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1857

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1857

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1857

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1857

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1857

emdash

npm i https://pkg.pr.new/emdash@1857

create-emdash

npm i https://pkg.pr.new/create-emdash@1857

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1857

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1857

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1857

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1857

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1857

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1857

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1857

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1857

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1857

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1857

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1857

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1857

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1857

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1857

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1857

commit: 7bfffbf

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is a focused, well-justified performance fix for #1517. It routes the admin content-list q filter through the existing SQLite FTS5 index (when search is enabled and covers the display columns) while preserving the unindexable LIKE fallback for Postgres, search-disabled collections, and uncovered display columns. The approach fits EmDash's architecture: handlers decide the strategy and the repository applies it, SQL identifiers are validated before interpolation, and user input is always passed as a parameter (the MATCH expression is constructed from escaped terms and bound as a value).

I read the diff, the full changed files, the existing search/query code, the FTS manager, and the route wrappers. I traced the query paths (page + count both use the same applySearchFilter, locale filtering is applied before the search predicate) and checked AGENTS.md conventions: SQL safety, locale filtering, API envelope, changeset, and tests are all in place. The behavioral change from arbitrary substring to token-prefix + slug-prefix matching is documented and intentional.

I did not run the test suite or linters (no shell available), but the provided tests cover the new matchers, the FTS path, soft-delete sync, and the LIKE fallback. I found no blocking issues, no security issues, and no convention violations that would require changes.

@github-actions github-actions Bot added review/approved Approved; no new commits since overlap and removed review/needs-review No maintainer or bot review yet labels Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

The admin list filter ran lower(col) LIKE '%term%' — doubly
unindexable (leading wildcard + lower() around the column) — so every
keystroke scanned all non-deleted rows in the locale, twice (page
fetch + count). On D1 these reads route to the primary.

When the collection has search enabled and the FTS index covers the
display columns, the filter now runs a token-prefix MATCH against
_emdash_fts_<slug> OR'd with an index-served slug GLOB prefix. Both
sides are index-backed (MULTI-INDEX OR in the query plan, no table
scan). Collections without search, uncovered display columns, and
Postgres keep the LIKE fallback.

Fixes emdash-cms#1517
@swissky
swissky force-pushed the perf/admin-search-fts branch from a91d419 to 7bfffbf Compare July 9, 2026 18:50
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed needs-rebase review/approved Approved; no new commits since labels Jul 9, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Thanks

@ascorbic
ascorbic merged commit 77e8968 into emdash-cms:main Jul 10, 2026
45 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jul 10, 2026
@swissky
swissky deleted the perf/admin-search-fts branch September 10, 2026 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/needs-rereview New commits since the last review size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Admin content-list search filter does an unindexable full-table scan

2 participants