Not one word in common. No synonyms file, no tags, no LLM writing an answer. Just Postgres, a column of numbers, and an ORDER BY.
Some context first. This is part 2 of a series where I learn AI engineering by building everything by hand. In part 1 I wrote an MCP server, an MCP client and an agent loop in plain Java, with no AI frameworks, so a local Qwen model running in Ollama could read and write my notes file.
That agent has a catch, though. Its read tool hands the model the whole notes file, pasted into the prompt. Fine for a short to-do list. Point it at a wiki with hundreds of pages, and the prompt gets huge: slow, expensive, the model starts missing things, and eventually it doesn't fit at all. What the agent really needs is to pull in only the few paragraphs that matter for the question. To find those, it has to search by meaning.
That piece is the embedding, and it's the foundation under semantic search, recommendations and most of the "add your own data to AI" stuff.
Here's the plan for this post. We take a handful of short notes, turn each one into numbers with an embedding model, store them in Postgres, then search them by meaning instead of by exact words. No chat model, no MCP, no agent: just an embedding model and a database. By the end, things to buy finds Grocery list on your own machine.
To see why that's worth doing, start with how search usually works.
Keyword search matches words, not meaning
Classic search (SQL LIKE, MySQL FULLTEXT, Postgres full-text search, even Elasticsearch out of the box) works on words. Split the text into words, clean them up a bit (lowercase, calling → call), and find documents that contain the same ones.
That's great when you know the exact word. It falls over when you don't:
| You search for | The note says | Keyword search |
|---|---|---|
things to buy | Grocery list: rice, lentils, ginger | ❌ no shared words |
reset my password | Account recovery steps | ❌ |
car | my vehicle insurance renewal | ❌ |
Humans read meaning, not spelling. To search the way humans read, we need a way to turn meaning into something a computer can compare. That's what an embedding is.
What an embedding is: text in, numbers out
An embedding is a list of numbers that represents the meaning of a piece of text. You get one by sending text to an embedding model.
I run mine locally with Ollama:
ollama pull nomic-embed-text curl http://localhost:11434/api/embed -d '{ "model": "nomic-embed-text", "input": "things to buy" }' The response, trimmed:
{ "model": "nomic-embed-text", "embeddings": [[0.0123, -0.0456, 0.0331, 0.0718, -0.0092, ...]] } That inner list has 768 numbers. Every piece of text you send, whether it's three words or three paragraphs, comes back as exactly 768 numbers. That fixed length is what makes texts comparable.
- An embedding model is not a chat model. It's a neural network, but a small one (
nomic-embed-textis about 270 MB, versus 4.7 GB for the Qwen model I chat with). It can't write a sentence. Text goes in, numbers come out, and that's its entire job. - Nobody chose what the numbers mean. There's no "number 42 = food". The model invented its own 768 features during training. You can't read them, but you can compare them, and comparing them is the whole point.
Direction is meaning, length is "how much"
768 numbers are impossible to picture, so let's cheat. Pretend the model only produced 3 numbers, and each one meant a topic: [food, tech, travel].
| Text | Embedding |
|---|---|
| "pasta recipe" | [0.9, 0.1, 0.0] |
| a 5-page article on Italian cooking | [2.7, 0.3, 0.0] |
| "setting up Docker" | [0.0, 0.9, 0.1] |
| "best ramen in Sapporo" | [0.6, 0.0, 0.7] |
Now picture each embedding as an arrow from the origin, pointing somewhere in that 3D space. Two things describe an arrow: which way it points, and how long it is.
- Direction = what the text is about. "Pasta recipe" and the cooking article point the same way: mostly food, a bit of tech (recipe sites, I guess), no travel. "Setting up Docker" points somewhere else entirely.
- Length = how much, not what. The article's numbers are three times bigger because it's longer and says more about food. It's not about a different topic, just more of the same one.
- Mixed topics point in between. "Best ramen in Sapporo" sits between food and travel, which is exactly what it is.
That is what stood out for me: similar meaning = similar direction. Real embeddings work the same way, just in 768 dimensions with features nobody can name.
How close is close? Cosine
If meaning is direction, we need a number that says how alike two directions are. For text, that number is cosine distance. It measures only the angle between two arrows and ignores their length:
- Same direction → distance 0 → same meaning.
- Different direction → bigger distance → different meaning.
Why ignore length? Search "pasta recipe" and the 5-page cooking article points the same way, so cosine calls it a near-perfect match. A ruler-style distance would call it far away just for being longer, which is backwards for search.
Other measures exist (Euclidean, inner product) for other kinds of data, like map coordinates or recommendation scores. You don't have to pick: your embedding model's docs tell you which one it was trained for, and for text models that's almost always cosine. Just make sure you use the same model for what you store and what you search with.
Semantic search in Postgres with pgvector
You don't need a special vector database to try this. Postgres with the pgvector extension stores embeddings as a normal column type, and a search is a normal SELECT. The whole flow:
A chunk is a piece of a bigger document, roughly a paragraph, so each vector stands for one idea. In this post each short note is its own chunk.
I run the official pgvector/pgvector:pg17 Docker image. Setup:
CREATE EXTENSION IF NOT EXISTS vector; CREATE TABLE notes ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, title text NOT NULL, body text NOT NULL, embedding vector(768) -- 768 must match the model's output ); For each note, a small script asks Ollama for the embedding and stores it. pgvector accepts the vector as plain text:
INSERT INTO notes (title, body, embedding) VALUES ('Grocery list', 'rice, lentils, ginger, chili', '[0.0123, -0.0456, ...]'); At search time, the request is embedded the same way, then:
SELECT title, 1 - (embedding <=> $1) AS similarity -- $1 = the query's vector FROM notes ORDER BY embedding <=> $1 -- <=> is cosine distance LIMIT 3; title | similarity -------------------+----------- Grocery list | 0.71 Dinner plans | 0.48 Ollama setup | 0.39 That's it. Semantic search is an ORDER BY on a distance. The <=> operator is cosine distance (0 = same direction), so 1 - distance gives a similarity score where higher is better.
One honest note: results always come back sorted, even when nothing is relevant. Search for "quantum physics" in a grocery wiki and you'll still get three rows, so keep an eye on the scores.
What embeddings are bad at
Embeddings are fuzzy by design. That's perfect for "things to buy", but bad when the exact word matters: error codes (ERR_4021 vs ERR_4022 look nearly identical), invoice IDs, or "notes NOT about Docker" (Docker notes still rank first). Keyword search handles those, which is why real search systems often run both, called "hybrid search".
Wrap-up
The whole idea in five lines:
- An embedding model turns text into a fixed-length list of numbers, a vector.
- Think of each vector as an arrow. Direction is meaning; length is mostly noise.
- Cosine distance measures the angle between arrows. For text, it's the default.
- Use the same model for what you store and what you search with.
- Semantic search is just
ORDER BY embedding <=> query LIMIT k. No chat model involved.
What clicked for me is how unmagical it is. "Understands meaning" turns out to be arrows and angles, and a database that can sort by an angle.
Next up
So far, nothing in this post talks back. It's just search. In part 3 the agent from part 1 finally gets to use it: I'll add a new search tool to my Java MCP server that embeds the question, runs this exact pgvector query and hands back the closest chunks. Then the agent can stop reading the whole notes file and just ask for the few paragraphs it needs.
Co-written with Claude, who patiently answered "but what does that mean?" about forty times.



