Why approximate search exists
Modern recommenders and semantic search rest on vector embeddings: every product and every shopper
is a point in a high-dimensional space. The task is to find the points nearest to a query.
Exact search — scanning every vector — is too slow on large catalogues:
Catalogue: 5,000,000 products
Embedding dimensionality: 128
Exact search: ~640 million operations per query → 500–2,000 ms
ANN (HNSW): under 5 ms at recall above 95%
ANN solves this by building index structures that discard most irrelevant vectors without ever
examining them.
The main algorithms
| Algorithm | Principle | Strength |
|---|---|---|
| HNSW | Hierarchical graph | High accuracy, dynamic inserts |
| IVF (inverted file) | Clustering plus in-cluster search | Memory efficiency on large datasets |
| LSH | Hashing by random projections | Simplicity, no GPU needed |
| ScaNN | Anisotropic quantisation | High speed, tuned for search |
FAISS is the most widely used library, supporting several algorithms and GPU acceleration.
Applications in e-commerce
Similar items. A product page needs 10–20 visually or semantically similar items in
milliseconds. An ANN index over product embeddings answers in real time.
Semantic search. A query such as a warm coat for autumn becomes an embedding, ANN searches the
product vectors, and relevant results come back with no exact word match.
Personalized recommendations. A two-tower model produces a user vector and item vectors. ANN
finds the items nearest to the user vector in O(log n) instead of O(n).
The accuracy–speed trade-off
ANN is tuned through index parameters. In HNSW the key ones are:
ef_construction— accuracy during index construction, which drives indexing timeef_search— accuracy during search, which drives latency
Raising them improves recall and slows search. In production the balance is usually set so that
recall at 10 stays at or above 95% with latency under 10 ms.
Important: when the catalogue changes, HNSW accepts incremental inserts. IVF indexes often
require a full rebuild — worth weighing when the catalogue updates frequently.