How I built related posts with free embeddings stored in MariaDB

"A free local model picks what you read next." beside a diff where three old Keep reading picks are struck out and two related posts about securing WordPress are added, with mean grade 0.32 to 1.49.

On October 30, 2026 I started replacing the related posts block under each post on this blog, the one called Keep reading. Under “Secure Your WordPress Site” it suggested a LiteSpeed Cache webinar, an AMP talk and a WordCamp recap. Under “Upgraded to Ubuntu 15.04” it suggested a post about HTTPS and one called “Everybody Loves Progress”.1

The block wasn’t broken. It showed the newest posts from the post’s first category, which is what I’d told it to do.2 But Misc held 199 posts and Tooling held 174, so the same few recent posts sat under hundreds of old ones, whatever those were about.

The short version: a free embedding model runs in a small Node job on my server, the vectors live in plain blob columns next to WordPress’s own tables, and the job re-ranks every post once an hour. It’s been live since October 31. This post is for my own notes, so a year from now I still know why each piece is the way it is.

Keeping it inside the database I already had

I tried OpenAI embeddings first and didn’t want to pay for them. Ollama runs models locally, but I wanted this running on the server, next to the site. And I didn’t want a second database to look after, Postgres or Elasticsearch, only for related posts. So the question became whether the MariaDB that WordPress already uses could hold the whole thing.

At this size the database doesn’t have to do anything clever. I picked nomic-embed-text-v1.5, which is Apache 2.0 and turns a piece of text into 768 numbers.3 Its 8-bit version is a 137 MB file,4 and transformers.js runs it in Node, so there’s no Python anywhere.5 Each vector is packed into a 3,072-byte blob, with no vector column type and no vector index. When the worker ranks, it loads all 490 post vectors into memory and compares every post with every other one, about 240,000 dot products. On my local copy the whole corpus is 1,267 vectors and about 9 MB of tables.

The only database trouble was naming. vector is a reserved word from MariaDB 11.7 and rank is reserved in MySQL 8, so the columns are called embedding and position.2 Production and my local stack both run MariaDB, local on 11.8. The extra vectors beyond one per post come from sections: the worker also embeds each part of a post between its h2 and h3 headings, for a chatbot I might build later. The related posts block only uses the one vector per post.

The prototype fused two rankings, and I kept that. For each post the worker takes the 20 posts whose vectors are closest by cosine similarity, and the 20 that score best on BM25, the keyword scoring most search engines use, queried with the post’s title and tags.6 Reciprocal rank fusion merges them. Each list gives a post 1 / (60 + rank) points and the points add up, so a post near the top of both lists wins. It’s nine lines:7

export function fuse( lists, k = 60 ) {
	const scores = new Map();
	for ( const list of lists ) {
		list.forEach( ( id, i ) => {
			scores.set( id, ( scores.get( id ) ?? 0 ) + 1 / ( k + i + 1 ) );
		} );
	}
	return scores;
}

The top 8 go into a table and the block shows 4.6 As of November 5, 2026, “Secure Your WordPress Site” points to Simple Way to Secure Your WordPress, What To Do When Your WordPress Blog Got Hacked, Secure Your WordPress Blog and WordPress Development FAQ. “Upgraded to Ubuntu 15.04” gets the Jaunty Jackalope and Karmic Koala post, Ubuntu 16.04 and Android Lollipop, a Pidgin update and the Ubuntu 10.10 release post.

To put a number on it, I had 30 posts graded: 230 blind pairs, new picks mixed with old ones, each scored 0 (unrelated), 1 (loosely related) or 2 (a reader would want this next) by another model that didn’t know which system made the pair. The old block averaged 0.32, and 75% of its picks got a 0. The new one averaged 1.49, with 9% zeros and 58% twos.1 A model was the judge, so I read that as a rough guide.

It still misses. Under the ModSecurity error post, the third pick was “Free Web Proxy Server To Bypass Access Filter”, with a score of 0.753, right in the band where the good picks sit. My plan had been a score floor that drops weak picks and fills the gap from the old category list. No floor worked. At 0.75, 104 of the 490 posts would fall back to category picks, and those graded worse than the weak matches they’d replace. So the floor is 0.65, a safety net that sends no current post to the fallback.1

An hourly job with a 1 GB budget

Everything that needs WordPress stays in the plugin. When I save a post, it strips the blocks to text, splits it into sections, hashes the result and queues it, and an unchanged hash means nothing happens.2 The worker runs once an hour, takes a lock so two runs can’t overlap, embeds only the text that changed, re-ranks every post and exits. With nothing queued it exits in about half a second without loading the model.8 It logs in as its own database user with grants on the corpus tables only, so it can’t touch wp_posts. Each vector also records the model that made it, so switching models makes every vector stale and the next run re-embeds the lot.2

Getting it running was a build, a backfill and a switch:8,9

docker build -t ik2-embedder:dev services/embedder
wp ik2 corpus enqueue --all
node src/run.mjs
wp ik2 corpus status
wp ik2 corpus related on

The new picks stay off until related on, and related off puts the category picks back without a redeploy.9

“Free” here means no API fees on a server I already pay for. What it costs is memory, and the worker container is capped at 1 GB. The first full run on my laptop embedded 490 posts and 777 sections in 171 seconds and peaked at 605 MB.1 Two choices keep it under the cap. Embedding eight inputs at a time pushed my prototype to 1.6 GB, so the worker embeds one at a time.2 Turning off ONNX Runtime’s CPU memory arena saves about 145 MB for around 8% more time.10 The image is about 450 MB, and the model is 137 MB of that.8

What I want to improve next

I haven’t run any of this on MySQL itself, and I don’t have OpenAI or Ollama picks over the same 490 posts, so I can’t say how they’d compare. The memory and time numbers come from my laptop, and I have no click data on the new picks yet. Most of my list for the next round comes out of those gaps:

  • Rethink the score floor. It’s checked against cosine similarity while the list is ordered by the fused rank, so a high floor pulls picks 5 to 8 up into the block. A floor on the fused rank, or no floor at all, may work better.1
  • Try MariaDB’s own vector type. It has a VECTOR column and VEC_DISTANCE_COSINE from 11.7, and it’s generally available from 11.8,11 so the comparison could move into SQL instead of loading every vector into Node. I’d check that production’s MariaDB is new enough first.
  • Find out whether anyone clicks. Until then, a model’s grades are the only sign the picks are better.
  • Get told when a run fails. There’s no alert yet, so a broken hourly job only shows up when I look at the last runs.
  • Add pages, projects and talks. The tables already record what kind of object each row is, so that needs no schema change.2

The code is in the ik2 repo, and the eval report lists every graded pair.

If your related posts block keeps showing the same few posts and you have a few hundred of them, an hourly job like this is small enough to try. With thousands of posts, look at the every-pair comparison first, since the work grows with the square of the post count.

Sources