Replacing Magento's keyword search engine with vector similarity — achieving natural language product discovery at ~50ms latency on CPU.
Magento's default search is keyword-based: a query for "comfortable running shoes" only matches products with those exact words in the title or description. Natural language queries, synonyms, and intent-based searches all fail.
For catalog-heavy stores, this means customers who search naturally either get zero results or highly irrelevant matches — a direct conversion killer.
The engineering challenge: replacing the search layer without touching Magento core, while keeping it fast enough for real-time queries on modest hardware.
A FastAPI microservice handles embedding generation and vector similarity search independently from Magento. A lightweight Magento 2 module intercepts search queries, calls the AI service, and maps results back to Magento product IDs — with automatic fallback to native search on any service failure.
all-MiniLM-L6-v2 via SentenceTransformers — lightweight, fast, and effective for product catalog semantics. Produces 384-dimensional embeddings per product description.
FAISS IndexFlatIP (inner product / cosine similarity). Entire index loaded in memory at startup. For 2K products, index size is ~3MB — negligible memory footprint.
Product data (name, description, category, attributes) fetched from Magento DB → concatenated into a text document per product → embedded → stored in FAISS index with product ID mapping.
Incoming search query string sent to FastAPI → embedded using the same model → produces a query vector.
FAISS inner product search against the catalog index → top-k most similar products returned with similarity scores.
Product IDs returned to Magento module → Magento collection filtered to those IDs → rendered in standard search results page.
If the AI service returns an error or times out, the module falls back to Magento's native search engine transparently — no broken search page.
Magento search interface compliance
Implemented Magento's SearchInterface contract in PHP — the module plugs in cleanly via di.xml preference injection, no core file changes.
Index freshness on catalog updates
Reindex endpoint on the FastAPI service triggered by a Magento observer on product save/delete events.
Cold start latency
FAISS index and model loaded into memory at service startup — zero per-request I/O overhead. Docker container kept warm.
Search result ranking
FAISS cosine similarity score used directly for ranking. Products with identical similarity scores sorted by Magento's default position as tiebreaker.