How the search on this site works
The search console on the home page is the retrieval layer of a RAG system with no external API behind it. This is what runs when you press enter, why it is retrieval only, and why a few hundred dot products beat a vector database at this size.
- Written by
- Baraqix Engineering
- Published
- Read
- 7 min
- Filed under
- rag, search, build notes
Contents7 sections
Every AI agency website says it builds retrieval systems. Saying it costs nothing, so we put one on the home page instead. The search console there is the retrieval layer of a RAG system, running on the same small server as this site, indexing this site. This note is what happens when you press enter, and why. Everything below is true to the code as deployed.
Why the demo stops at retrieval
A RAG system has two halves. Retrieval finds the passages relevant to the question. Generation writes an answer from them. Almost every RAG failure we have debugged lived in the first half. The wrong passage ranked first. The right passage was split across two chunks so neither matched. The threshold was tuned on a sample folder and fell apart on the real corpus. Generation then wrote a confident paragraph on top of a bad retrieval, and that paragraph is what the user saw.
So the demo shows the half that matters. It ranks passages that already exist on this site and shows the similarity score for each. It never calls a language model to write anything, which is why the mechanics panel says the tool cannot invent a price, a timeline or a client. No component in the pipeline can produce text that is not already on a page.
There is a practical reason too. The site runs on a shared virtual server with three cores and six gigabytes of memory, next to PostgreSQL and the site itself, with zero external API spend. A small embedding model fits that budget. A generative model on CPU would saturate the cores and undercut the demo.
The model
The embedding model is all-MiniLM-L6-v2, run in Node through the transformers.js feature extraction pipeline with mean pooling and normalisation on. It produces a 384 dimension vector for any string, sits under a hundred megabytes on disk and around four hundred in memory with the runtime. It loads once at server start and stays resident. A model loaded per request would turn a thirty millisecond query into a multi second one and eventually run the box out of memory.
Because every vector is normalised to unit length, cosine similarity reduces to a dot product. The scoring function is a loop over 384 floats. That is the whole algorithm, and it is worth saying because people expect something more elaborate.
function cosineSimilarity(a: Float32Array, b: Float32Array): number {
let dot = 0;
for (let i = 0; i < a.length; i++) dot += a[i] * b[i];
// Vectors are already normalised, so the dot product is the cosine.
return dot;
}How the corpus is chunked
The index is built from the same structured content objects the pages render from, so it cannot drift from what a visitor reads. Each service, process step and belief, and the main passages on the home page, become one chunk carrying its source page, a section anchor and the text. Published notes from this blog are added as title plus excerpt plus flattened body, capped at twelve hundred characters so a long post does not dominate the index with one enormous vector.
The first version chunked purely by section and had a problem we could measure. The AI integration service description mentioned reading PDF and DOCX files, but it also covered agents, MCP, voice and the stack in one paragraph. When someone asked whether we could read their PDFs, that chunk scored 0.28 against a threshold of 0.35 and missed, even though the words were in it. A long chunk about six topics is a weak match for a question about one. The fix was a dedicated shorter chunk carrying the same claim already on the page, phrased closer to how a visitor asks. It scored 0.42. The rule for adding one is strict: it may restate a claim already on the site and may never introduce a new one. The demo is proof. It is not allowed to be a more generous copy of the marketing.
The general lesson is that chunk size is a retrieval parameter before it is a formatting decision. A chunk should be about one thing, sized so a plausible question about that thing lands inside it, and carry enough context that the result card makes sense on its own.
Why brute force wins at this size
The corpus is in the low hundreds of chunks. Scoring a query means a few hundred dot products of 384 floats, on the order of a hundred thousand multiply and add operations, which a CPU finishes in well under a millisecond. Almost all of the retrieval time in the mechanics panel is the embedding pass for your query. A vector database here would add a network hop, a second process to keep alive and a place for the index to drift from the pages, in exchange for making the fast part faster.
| Question | In memory brute force | pgvector or a vector database |
|---|---|---|
| Corpus size | Hundreds to a few thousand chunks | Tens of thousands and up |
| Scoring cost | Microseconds, a loop | Index lookup, approximate nearest neighbour |
| Filtering | Filter the array in code | WHERE clauses next to the vector search |
| Persistence | Rebuilt on deploy, lives in process memory | Survives restarts, shared between processes |
| Writers | One process rebuilds the whole index | Many writers, row level updates |
| Operational cost | None beyond the model | An extension or a service to run and monitor |
Brute force stays correct until one of three things happens: the corpus grows until a linear scan is noticeable, you need to filter by metadata inside the search instead of after it, or more than one process needs to write to the index. For a client with an archive in the hundreds of thousands, pgvector inside the PostgreSQL they already run is usually the next step, because it keeps vectors next to the rows they describe and reuses existing backups and access control. A separate vector database earns its place after that, when the index needs its own machine.
The request path
- Your query goes to POST /api/search. It must be between one and two hundred characters, and each address gets thirty searches a minute.
- If the index is empty, for example on the first request after a deploy, the server builds it before answering.
- The query is embedded with the same model and pooling as the corpus. Mixing models here is a classic silent failure.
- Every chunk is scored by dot product and sorted. Anything below 0.35 is dropped. The top five are returned with source page, anchor and score.
- The query, the top score, the result count and a hashed address are written to PostgreSQL. Reading that table weekly is the cheapest research into what prospects ask.
The index is a singleton on the Node global object. That sounds like a hack and it is a considered one. The development server reloads modules on every edit, and without the global the model would reload on every save. In production the same object is what lets the index survive across requests. One shared function rebuilds it on deploy and whenever a note is published, so a new post is searchable the moment it goes live.
What the mechanics panel shows
Under the results there is a mono panel. Chunks searched is the size of the index at that moment, so it rises when a note is published. Embedding dims is 384, the width of every vector. Retrieval time is measured from the start of the request handler to the point the results are ready, so it includes the query embedding. The model name is there so you can look it up. The method line says cosine similarity over an in memory index, and the generation line says none. A search box on its own is a claim. A search box with its mechanics visible is a demonstration.
What we would change for a client
For a real document corpus the first change is the chunker, splitting long documents into overlapping passages with the heading path attached. The second is an evaluation set, a few dozen real questions with the passage that should rank first, run on every change to the chunker or the threshold. The third is a hybrid keyword and vector score once the corpus is large enough that exact terms such as product codes start to miss. Generation comes last, only after retrieval precision is measured and acceptable.
Baraqix Engineering. Written by the engineers who did the work.
NOTE/02. First published .