{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "Dustin Edwards blog: Building on Cloudflare",
  "home_page_url": "https://dustinedwards.info/writing/series/building-on-cloudflare",
  "feed_url": "https://dustinedwards.info/writing/series/building-on-cloudflare/feed.json",
  "description": "Every part of Building on Cloudflare, in order.",
  "language": "en-US",
  "authors": [
    {
      "name": "Dustin Edwards",
      "url": "https://dustinedwards.info"
    }
  ],
  "items": [
    {
      "id": "https://dustinedwards.info/writing/site-search-on-d1",
      "url": "https://dustinedwards.info/writing/site-search-on-d1",
      "title": "Cloudflare D1 full-text search with SQLite FTS5",
      "content_text": "\nCan Cloudflare D1 run full-text search for a whole site? Yes. D1 supports SQLite's FTS5 extension, and this site's search runs on two FTS5 tables in its D1 database with no outside search service.\n\nOne table matches words exactly and the other matches word stems, and the two result lists are merged by rank. On November 14, 2026 the site's own timer reported a median of 64 ms per search over 25 requests.\n\n*Part 3 of [Building on Cloudflare](/writing/ten-years-on-cloudflare). Previous: [How this blog stores posts in git and serves them from D1](/writing/posts-in-git-served-from-d1). Next: [Adding an AI answer mode to site search with Cloudflare AI Search](/writing/ai-answer-mode-on-site-search).*\n\n## What gets indexed\n\nEverything searchable becomes a row in one table, `search_docs`, with the shape id, url, type, title, body and date. A post becomes one row for the whole post plus one row per heading.\n\nThose per-heading rows are why a result can link straight to the section that answers a query, using the heading's anchor. When a post and several of its own sections all match, the results show the best section under the post's title, so one post doesn't fill the page.\n\nThe two FTS5 tables are external-content tables over `search_docs`. External content means FTS5 keeps only its index and reads the text from `search_docs`, so the text is stored once. The sync path rewrites `search_docs` as a set and rebuilds both indexes with FTS5's `rebuild` command.\n\n## Why two FTS5 indexes?\n\nAn FTS5 tokenizer is set per table, so one table can't match prose by stem and names exactly. This site needs both, so it has two tables.\n\nProse wants stemming. A search for \"indexing\" should find a sentence with \"index.\" Names want the opposite. The Porter stemmer turns Edwards into \"edward\" and Harris into \"harri,\" which is wrong for a person's name. So one table uses `unicode61 remove_diacritics 2` and matches tokens as typed. The other uses `porter unicode61 remove_diacritics 2` and matches stems.\n\nUse `remove_diacritics 2`. Option 1, the default, misses some characters that need multi-byte handling, and readers type plain ASCII regardless.\n\n## How are two FTS5 result lists merged?\n\nBy reciprocal rank fusion, which scores each result by its position in each list and ignores the raw bm25 scores. Each result gets 1 / (k + rank) from every list it appears in, and the sums decide the order.\n\nThe raw scores can't be compared. bm25 depends on the tokenizer and on the average row length, and the two tables differ in both. bm25 also favors very short rows, and section rows are short, so a two-line section could outrank the post that answers the question.\n\nReciprocal rank fusion comes from Cormack, Clarke and Büttcher (2009). With k = 60, the paper's value, rank 1 is worth 0.0164 and rank 20 is worth 0.0125. That small gap means a result that appears in both lists usually beats one that tops only one list.\n\n:::diagram{title=\"How a search query is answered on dustinedwards.info\" alt=\"Flow diagram. A query string goes to a parser that separates terms, quoted phrases and filters such as tag:, type:, base: and a bare year. A query with text runs against two SQLite FTS5 indexes in D1, one exact with the unicode61 tokenizer and one stemmed with porter unicode61, and the two ranked lists are fused by reciprocal rank with k equal to 60. A query with only filters skips the indexes and runs plain SQL ordered by date. Both paths return results as HTML or, with an Accept header of application/json, as JSON.\"}\n```mermaid\nflowchart LR\n  q[Query string<br>/search?q=] --> p[Parser<br>terms, phrases, filters]\n  p -->|has text| exact[Exact index<br>unicode61]\n  p --> stemmed[Stemmed index<br>porter unicode61]\n  p -->|filters only| browse[Browse<br>plain SQL by date]\n  exact --> fuse[Fuse by rank<br>RRF, k = 60]\n  stemmed --> fuse\n  fuse --> results[Results<br>HTML or JSON]\n  browse --> results\n```\nText queries go to the two FTS5 indexes. Filter-only queries skip them.\n:::\n\n## Filter-only queries need their own path\n\nA query with no text skips FTS5 and runs plain SQL ordered by date. Without that branch, a click on a tag returns nothing, because there's no text to match.\n\nThe parser pulls out quoted phrases, `tag:`, `type:` and `base:` filters, and a bare four-digit year, which becomes a date filter. If anything is left as text, the query goes to the two indexes. If only filters remain, it's a listing, so the site returns whole posts and pages, newest first. On November 14, 2026, `tag:d1` returned 4 results and `2026` returned 14 this way. Both paths apply the same visibility rules, so drafts and future-dated posts stay out.\n\n## How fast is FTS5 search on D1?\n\nOn November 14, 2026, over 25 requests, the site reported a median of 64 ms and a 95th percentile of 388 ms. End to end over HTTPS from my test machine, the median was 207 ms.\n\n:::chart{type=\"bar\" x=\"stat\" y=\"took_ms,e2e_ms\" labels=\"Server tookMs,End to end\" y-unit=\"ms\" alt=\"Bar chart of search latency in milliseconds from 25 requests on November 14, 2026, five each for the queries d1, phage, cloudflare, rate limit and edwards. The time the site reports in its JSON tookMs field had a minimum of 36, a median of 64, a 95th percentile of 388 and a maximum of 423 milliseconds. End to end over HTTPS from my test machine the minimum was 113, the median 207, the 95th percentile 954 and the maximum 982 milliseconds.\" title=\"Search latency on dustinedwards.info, 25 requests, November 14, 2026\"}\n```csv\nstat,took_ms,e2e_ms\nMin,36,113\nMedian,64,207\np95,388,954\nMax,423,982\n```\nPurple bars are the server's own `tookMs`. Blue bars include the network round trip from my test machine.\n:::\n\n\n\nThe times were split into two groups. Fourteen requests took 36 to 66 ms on the server, and eleven took 216 to 423 ms. I haven't traced what the slower group was doing. The five queries were `d1`, `phage`, `cloudflare`, `rate limit` and `edwards`, five requests each, one after another. These are one day's numbers on a site built from 240 content files.\n\n## The same URL serves JSON\n\nSend `Accept: application/json` to `/search?q=...` and the same route returns JSON, with the parsed query, facet counts, results and `tookMs`. The response carries `Vary: Accept` so caches keep the two forms apart.\n\nThat makes the search page usable by scripts and AI tools with no separate API. The JSON also reports `askAvailable`, which tells a client whether the AI answer mode from [the next post](/writing/ai-answer-mode-on-site-search) is on.\n\n## FTS5 gotchas: COUNT(*) and DELETE\n\nBoth are documented SQLite behavior. A remote database makes them harder to spot.\n\nFirst, `COUNT(*)` on an external-content FTS5 table reads through to the content table, so it reports the content table's row count even when the index is empty. Count the index's `_docsize` shadow table instead. This site's `/health` route has an `fts-equality` check that compares `search_docs` with both indexes' `_docsize` counts.\n\nSecond, running `DELETE FROM` directly on an FTS5 table corrupts the index in a way that only shows up on a later write. Repair it with the FTS5 `rebuild` command, and delete through the content table.\n\nFTS5 tables also change how D1 backups work: `wrangler d1 export` fails on any database that has them.\n\n*Next: [Adding an AI answer mode to site search with Cloudflare AI Search](/writing/ai-answer-mode-on-site-search).*\n",
      "summary": "Site search on Cloudflare D1 with SQLite FTS5: two indexes (exact and Porter-stemmed), reciprocal rank fusion, section-level results and measured latency.",
      "date_published": "2026-07-28T00:00:00.000Z",
      "date_modified": "2026-12-04T18:35:55.000Z",
      "tags": [
        "cloudflare-d1",
        "fts5",
        "full-text-search",
        "site-search",
        "sqlite"
      ]
    }
  ]
}
