Semantic Search
Hybrid vector + lexical product search with a soft lexical fallback — embeddings from Gemini or local Ollama, and an opt-in pgvector/Postgres path for large catalogs.
Cartwright product search is hybrid: it ranks by vector cosine-similarity and a lexical boost, so a query like "warm waterproof jacket for hiking" finds the right products even when none of those exact words appear in the title. When embeddings aren't ready, it falls back softly to pure lexical search — there is no hard dependency and no regression.
How ranking works
Each product gets an embedding built from its name, brand, category, description, and attributes — one searchable haystack. At query time:
- The query is embedded with the same model.
- Candidates are scored by cosine similarity (semantic) plus a lexical boost for direct token matches.
- Results are ordered by the combined score.
The same path powers both the storefront search API (/api/products/search) and the agent-callable products.search tool, so a human and an AI agent get identically ranked results.
Embedding providers
Embeddings are produced by lib/ai/embeddings.ts, which routes by your AI configuration:
| Provider | Model | Notes |
|---|---|---|
| Google Gemini | gemini-embedding-2, asked for 768 dimensions | Default when a Gemini key is set. Override with GEMINI_EMBED_MODEL: an override runs with that model's own dimensions, so with pgvector the column must be that size. |
| Ollama (local) | nomic-embed-text | On-device fallback — no cloud calls. |
| none | — | Returns null → search uses lexical-only ranking. |
Backfill or re-embed your whole catalog after a model change:
pnpm embeddings:backfillNew and updated products are re-embedded on a best-effort hook, so the index stays current as you edit the catalog.
Engine releases up to and including v0.67.0 default to text-embedding-004, which Google shut down on 14 January 2026. On those releases every embedding call fails and search runs on lexical ranking alone, with only a line in the server log. The release after v0.67.0 moves the default to gemini-embedding-2; after updating, run pnpm embeddings:backfill once, because vectors are stored per model. To fix an older engine without updating, set GEMINI_EMBED_MODEL=gemini-embedding-2 and run the backfill: the engine then stores that model's full 3072 dimensions, which works on the default Turso/SQLite path (cosine in TypeScript over vectorJson) but not with the vector(768) pgvector column. When you later update and remove the override, run the backfill again: the new default stores its vectors under its own id (google:gemini-embedding-2@768), so the 3072-dimension rows are replaced rather than compared.
Embeddings are stored in ProductEmbedding.vectorJson — portable JSON that works on every database driver, including the default Turso/libSQL.
Scaling: pgvector / Postgres (opt-in)
The default ranking runs in TypeScript over the embedding table — perfect for typical catalogs. For large catalogs (tens of thousands of products), an opt-in path pushes the nearest-neighbour search down into Postgres + a pgvector HNSW index, so search scales logarithmically instead of linearly.
- Same ranking formula as the TS path → identical results.
- Gated behind
DATABASE_DRIVER=postgres; the default Turso/SQLite path always fires first. - Dual-write: the portable
vectorJsonand a nativevector(768)column.
See Postgres & Supabase for the full setup (pnpm pgvector:setup, Supabase notes).