Skip to content
Book a call

Search on a small blog, without a search service

· 4-minute read · Measurement and growth · by Direct Softworks

A few dozen articles do not need an index server. They need folded text, AND matching and a ranking rule you can explain in one sentence. Ours is about thirty lines.

When we rebuilt the blog index, we wanted a search box on it. The default answer to "add search" is a service: a hosted index, an API key, a crawler for your own content, a monthly bill, and a widget that sends every keystroke to a third party.

For a blog with a few dozen articles, that is a lot of machinery for finding a word in a list. Ours is about thirty lines of matching code. It runs entirely in the reader's browser and makes no request at all.

Ship the index with the page

The server already has every post when it renders the index. So while it builds the page, it also builds a small search record per post: the title, folded, and a second folded string made of the excerpt, the topic, the section headings and the opening paragraphs. The comment beside that code puts the cost at about a kilobyte a post. Shipping every full article instead would send the whole archive on every visit.

The matching then runs in the browser against that data. No index server, no network, no cost per query, and nothing a reader types ever leaves their machine.

Fold everything, on both sides

Readers rarely type diacritics, and Romanian text is full of them. So the search compares folded text on both sides: lower case, with combining marks removed after Unicode NFD decomposition. "facturare" finds "Facturare", and "sef" finds "ȘEF".

Romanian makes this sharper than most languages, because s-comma and t-comma exist twice in Unicode: the correct comma-below letters and the older cedilla ones. Under NFD, the comma-below ș becomes s followed by U+0326, and the cedilla form (U+015F) becomes s followed by U+0327. Strip every mark by its Unicode category rather than by a list of letters, and both arrive at a plain s. The search never needs to know which spelling an article or a reader used.

Queries go through the same fold, then split on whitespace with punctuation trimmed from the ends of each word, so "e-Factura," becomes e-factura and keeps its hyphen.

Every word must match, and the title counts three times

A search box should narrow as you type more, so every term has to appear somewhere in the post. That is AND matching, and it is what people expect from a box on a page: adding "anaf" to "factura" should leave fewer results, not more. OR matching would put the post that mentions both first, then list everything that mentions either, which reads as noise.

Matching is by substring, so "fact" already finds "factura" while the reader is still typing.

Ranking is one rule. A term found in the title scores three; a term found only in the rest scores one; the scores add up. The article about a word outranks the one that mentions it. When two posts score the same, the newer comes first, because the list was already sorted that way and JavaScript's sort is stable.

That is the whole ranking. No stemming, no synonyms, no typo tolerance. For a few dozen articles written in a consistent vocabulary, a rule you can explain in one sentence is easier to trust than one you cannot.

Highlighting without lying

Results mark the matched words in each title and excerpt. The matching happened on folded text, but the marks have to land in the original, with its capitals and diacritics intact. The code reuses the positions found in the folded copy, which is only correct while folding kept the string the same length.

For ordinary Romanian text it does: a precomposed ă is one character in and one character out. But a letter typed as a base character plus a separate combining mark folds one character shorter, and every position after it would be off by one. So the function compares the two lengths first, and if they differ it returns no highlights at all. No marks is better than marks in the wrong place. Overlapping hits, such as "fact" and "factura" in one query, are merged into one span.

Shareable without hiding the page

A search should be a link you can send. The query and the topic go into the address as ?q= and ?topic=, written with history.replaceState as the reader types and read back once after the page loads.

They are deliberately not read with Next.js's useSearchParams. That hook would put the list behind a Suspense boundary, and the boundary's fallback is what a static prerender writes into the HTML, which is how our contact page once served a crawler 98 characters. Reading the address directly after hydration keeps every article in the server-rendered page.

When to stop doing it this way

This design has a ceiling. Somewhere in the hundreds of posts, a kilobyte each becomes a payload worth noticing, and searching the full text of every article is something it cannot do at all. The next step at that point is an index built at deploy time and fetched the first time someone uses the box, still with no service behind it.

Until then, the right search is the one with no moving parts: folded strings, a loop and a number. It is tested by a plain Node script with no framework, it costs nothing per query, and it cannot go down separately from the page it sits on.

More notes