sanyam jain

How related posts work

planted 08 Sept 2026 · tended 22 Oct 2026 · 🌱 seedling

I recently built a small enhancement to this blog - "ability to see top 3 related posts".
Most blogs have prev-next buttons, tags, and archives, but who has the time to filter and find a needle in haystack? Also most of the time you don't even know what you are looking for.

The feature looks like this:

how related posts look, an example

Algorithm A (brute force) - Match word by word

If a blog's title contains hello, and another blog's title also contains hello, they might be related.
simple. easy peasy.
But what if we had namaste instead of hello in the other blog. Isn't namaste related to hello?

Algorithm B (transformers) - Use semantics of a word

  1. When I end up writing a blog, the blog goes through an algorithm called transformer
  2. The transformer converts the "title + blog content" into a decimal format.
  3. example: "hello world" -> 1.2, "gym" -> 5.1, "fitness"-> 5.9
  4. For every blog, we compute this number. The computation happens locally, and it's quick. We cache the computations. Only if the blog's hash changes do we recompute the number
  5. Now, if the numbers are closer, we know they are closely related. So a gym post will link to a blog that talks about fitness or habit building.

The transformer does the heavy lifting.

Going to write about how transformer works, but that's for another day.