Back to Systems
AI-Powered Ecommerce

AI Semantic Search
for Magento 2

Replacing Magento's keyword search engine with vector similarity — achieving natural language product discovery at ~50ms latency on CPU.

Problem

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.

Solution

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.

Architecture

System Design
AI Service (Python)
  • SentenceTransformers model loaded on startup
  • Product catalog ingested and embedded at index time
  • FAISS index built and persisted to disk
  • FastAPI exposes /search and /reindex endpoints
Magento Module (PHP)
  • Implements Magento search interface — no core override
  • Intercepts search query, calls AI service HTTP endpoint
  • Maps returned product IDs to Magento collection
  • Falls back to native search on timeout or error
Embedding Model

all-MiniLM-L6-v2 via SentenceTransformers — lightweight, fast, and effective for product catalog semantics. Produces 384-dimensional embeddings per product description.

Vector Index

FAISS IndexFlatIP (inner product / cosine similarity). Entire index loaded in memory at startup. For 2K products, index size is ~3MB — negligible memory footprint.

AI Pipeline

01
Offline: Catalog indexing

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.

02
Runtime: Query embedding

Incoming search query string sent to FastAPI → embedded using the same model → produces a query vector.

03
Runtime: Similarity search

FAISS inner product search against the catalog index → top-k most similar products returned with similarity scores.

04
Runtime: Result mapping

Product IDs returned to Magento module → Magento collection filtered to those IDs → rendered in standard search results page.

05
Fallback

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.

Key Outcomes

~50ms
Search latency on CPU
for 2,000+ product catalog
0
Core modifications
fully module-based, upgrade-safe
100%
Fallback coverage
native search on any AI failure

Challenges

Challenge

Magento search interface compliance

Solution

Implemented Magento's SearchInterface contract in PHP — the module plugs in cleanly via di.xml preference injection, no core file changes.

Challenge

Index freshness on catalog updates

Solution

Reindex endpoint on the FastAPI service triggered by a Magento observer on product save/delete events.

Challenge

Cold start latency

Solution

FAISS index and model loaded into memory at service startup — zero per-request I/O overhead. Docker container kept warm.

Challenge

Search result ranking

Solution

FAISS cosine similarity score used directly for ranking. Products with identical similarity scores sorted by Magento's default position as tiebreaker.

Tech Stack

AI Service
PythonFastAPISentenceTransformersFAISS
Magento Module
PHPMagento 2di.xmlObserver
Embedding Model
all-MiniLM-L6-v2
Infrastructure
DockerREST API

Future Improvements

  • Qdrant or Weaviate backend for persistent, scalable vector storage beyond in-memory FAISS
  • Hybrid search: combine BM25 keyword score + semantic vector score for best of both approaches
  • Query expansion: use LLM to rewrite queries before embedding for better coverage
  • Personalized re-ranking based on customer purchase history embeddings
  • Admin UI panel for index management, search testing, and similarity score visibility