Home›Design›Search and AI retrieval
Design doc · Search and AI retrieval

Search and AI retrieval

Where should full-text search and vector search for AI features go?

Applications like this: site search, document search, semantic search, retrieval for a chat assistant. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.

The short answerStart in PostgreSQL: full-text search and pgvector, next to the data. Move to a dedicated search engine or vector database when the size, the query rate, or the ranking needs outgrow it.
1 · The workload

What the application asks of the database

DimensionThis application
WritesDocuments added and updated, each needing an embedding.
ReadsQueries by text and by similarity, often combined with filters.
Read to writeHigh.
ConsistencyLoose. A new document can take seconds to become searchable.
LatencyUnder a few hundred milliseconds, including the embedding of the query.
GrowthModerate to large. Embeddings are big: hundreds to thousands of numbers per item.
Connected

Real failures behind this design

These incidents are why the choices below matter. Each one is written up with its numbers and sources.

2 · What goes where

Each kind of data, its store, and why

ApplicationDocuments and metadataPostgreSQLSOURCE OF TRUTHEmbeddings, small to medium scalepgvector in the same databaseSOURCE OF TRUTHLarge scale or advanced rankingElasticsearch or OpenSearch, or a vector databaseCOPY

Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.

DataWhere it goesWhy
Documents and metadataPostgreSQLThe record, permissions, and filters.
Embeddings, small to medium scalepgvector in the same databaseNo second system, and filters and permissions are just SQL.
Large scale or advanced rankingElasticsearch or OpenSearch, or a vector databaseDistributed indexes, hybrid text and vector ranking, more tuning knobs.

How each family of store behaves under traffic: Database families.

3 · Data model

The core tables and the queries they serve

CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE chunks (
  id        bigint PRIMARY KEY,
  doc_id    bigint NOT NULL,
  tenant_id bigint NOT NULL,
  body      text NOT NULL,
  embedding vector(1024)
);
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops);

SELECT id, body FROM chunks
WHERE tenant_id = $1
ORDER BY embedding <=> $2
LIMIT 10;
4 · How it grows

Start simple, change one thing at a time

  1. PostgreSQL full-text and pgvectorOne database, no sync problem.
  2. Index outgrows memory, or ranking gets complexA search engine or vector database, fed by the same outbox or CDC pattern as other copies.
  3. Many teams and sourcesA shared search platform with its own ingestion pipeline.

Scale it: watch this design grow

Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.

5 · What breaks first

The failures to plan for

What happensWhat you seeWhat I do
HNSW index larger than memoryLatency jumps as the index pages come from diskSize memory to the index, or reduce dimensions, or quantize.
Embeddings from mixed model versionsResults that quietly get worseRecord the model per row and re-embed in one pass.
Permission filters applied after searchUsers see snippets of documents they cannot openFilter inside the query.
6 · Alternatives

What I considered and did not pick

Cost. Embeddings are large. Count vectors times dimensions times bytes per number, then add the index, before picking a server.

How this page was writtenFrom the engines' documentation and common practice. Measured on real servers: one lookup with and without an index, in the notebooks. Before you build: check the anti-patterns, size it with the capacity calculator, and write your own version from the design doc template. Want a second opinion? Submit it for review.
7 · Where to run it

On-prem, managed, EC2 or Kubernetes

StageWhere it runs
Where I would startManaged PostgreSQL with pgvector
When it growsA managed search or vector service once the index outgrows memory

For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.

8 · Estimate it

The numbers, worked live

The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.

Part 3 of 4 · Design · 24 pagesAll pages

Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.