猫のBLOG

Notes, essays and marginalia

Essays

How the search stays honest

The search backend is a few hundred lines of Perl sitting behind a Unix socket. It does three things: clean the query, ask MariaDB, fill in list.html.

Cleaning first

The query is truncated, stripped of control characters, and escaped on the way out. Page numbers are clamped. Anything under two characters never reaches the database at all.

Then the query

MariaDB's full-text index is asked first with every word required:

MATCH(title, summary, body_text) AGAINST ('+perl* +nginx*' IN BOOLEAN MODE)

If that finds nothing, the same words are tried as alternatives, and if the words are too short for the server's index, a plain LIKE catches them. Three attempts, all with placeholders, none of them built by pasting the query into a string.

Then the template

The result page uses list.html - the very file the static build uses for category and tag pages. The masthead, menu, sidebar and footer are the same bytes. A reader cannot tell which pages were written last night and which one was made for them a moment ago.

Each worker keeps a small cache of rendered pages, and nginx can keep another one in front of it. A popular query costs the database nothing.