Indexing accelerates data retrieval in ML systems. Three types of indexing are commonly used.
Used for similarity search over embeddings. Essential for recommendation systems, semantic search, RAG, and similarity-based ML tasks.
| Index Type | Search Speed | Recall | Memory |
|---|---|---|---|
| Flat (brute force) | Slow (O(N)) | 100% | Highest |
| IVF (Inverted File) | Fast (O(log N)) | ~90% | Moderate |
| HNSW | Fastest (O(log N)) | ~99% | High |
| PQ (Product Quantization) | Fast | ~85% | Low (compressed) |
# HNSW parameters
M = 16 # connections per node (higher = more accurate, more memory)
ef_construction = 200 # build quality (higher = better recall, slower build)
ef = 50 # search quality (higher = better recall, slower search)
Classic information retrieval structure. Maps terms to documents. Used for keyword-based search and as a component in hybrid search systems.
# Inverted index structure
"machine" → [doc1, doc5, doc9, doc23]
"learning" → [doc1, doc3, doc5, doc12]
"system" → [doc2, doc5, doc7, doc9]
Query: "machine learning system"
Scoring: TF-IDF or BM25 on matching documents
Top result: doc5 (contains all three terms)
Provides O(1) lookups for exact key matches. Used for feature store lookups, cache keys, and model registry lookups.
| Component | Index Type | Why |
|---|---|---|
| Vector DB (semantic search) | HNSW | Best accuracy-speed trade-off for embeddings |
| Feature store (online) | Hash (Redis) | O(1) lookups by entity ID |
| Search (keyword) | Inverted index | Efficient term-document matching |
| Prediction cache | Hash | Fast exact match on request hash |
| Model registry search | Inverted + metadata | Find models by name, tag, metric |