Search without a search server
trunkcms 0.2.0 adds site search. The index is a JSON file written at build time, and ranking happens in the browser.
trunkcms 0.2.0 adds site search. The box is in the header of this page, use / to focus it.
How it works
Each build writes /search.json alongside the feeds and the sitemap. The browser downloads it the first time someone focuses the search box, then ranks results locally as they type. The server only serves the file so static exports get search too.
Turning it on
Tick Search box on the site on the admin Settings page, or add this to site.yaml:
search:
enabled: true
Search is off unless site.yaml turns it on, so existing sites don’t change when they upgrade. Initialize site turns it on for new ones.
What’s indexed
Post titles, bodies, summaries and tags, plus pages unless pages: false is set. A match in a tag is weighted ×3 and a match in a summary ×2.
Drafts and scheduled posts are left out, because the index is public. A scheduled post is added on the rebuild that publishes it.
Ranking
Results are ranked with BM25. A word that appears in only a few posts counts for more than one that appears in all of them, and repeating a word has diminishing returns. The last word of the query matches as a prefix, so webh finds webhook. A match in the title gets a boost, set by title_boost.
The built-in English stopword list holds only function words like the and which. It leaves out short words that are often topics, such as go, ai and js, and keep: adds more.
Size
On 300 posts of about 1,000 words each, the index is roughly 530 KB raw and 160 KB gzipped. It’s served with max-age=60 and an ETag, so a repeat visit gets a 304.
Themes
The built-in theme includes the box. A custom theme adds it with {{template "search" .}}, or overrides the search partial to change the markup. This site’s theme overrides it to add the icon and the / hint.
The search docs list every option and explain the ranking in more detail.