Stage 4 · Provision
Design Case Studies
Design a Search Service
Indexing pipelines, Elasticsearch shards, freshness targets, and relevance ranking updates.
Requirements
A search service provides full-text search across millions of documents. It must support queries in < 200ms, index new documents within 60 seconds, handle 50K QPS, and provide relevant results ranked by quality. Search is read-heavy with a 100:1 read-to-write ratio.
Indexing Pipeline
Document Created/Updated
│
▼
CDC / Kafka ──► Indexing Pipeline
│
├── 1. Parse document (extract text, metadata)
├── 2. Tokenize text (split into terms)
├── 3. Apply analyzers (lowercase, stem, remove stop words)
├── 4. Generate embeddings (for semantic search)
├── 5. Write to Elasticsearch bulk API
└── 6. Update search cacheThe indexing pipeline transforms raw documents into searchable index entries. It handles text analysis, embedding generation, and bulk indexing for performance.
Elasticsearch Architecture
Cluster: search-production
│
├── Master Nodes (3)
│ └── Cluster state management, shard allocation
│
├── Data Nodes (10)
│ ├── Index: products (5 shards, 1 replica)
│ ├── Index: orders (3 shards, 1 replica)
│ └── Index: users (2 shards, 1 replica)
│
├── Coordinating Nodes (3)
│ └── Query routing, result aggregation
│
└── Ingest Nodes (2)
└── Pipeline processing, enrichmentSeparate node roles for performance. Master nodes handle cluster state. Data nodes store and search. Coordinating nodes aggregate results. Ingest nodes process documents.
Freshness Targets
| Freshness | Approach | Use Case |
|---|---|---|
| < 1 second | Real-time indexing, near-real-time search | Live auction, stock prices |
| < 1 minute | Near-real-time with refresh_interval=30s | Product catalog, news articles |
| < 1 hour | Batch indexing | Historical data, analytics |
| < 1 day | Daily reindex | Static content, documentation |
Relevance Ranking
- TF-IDF — Term frequency-inverse document frequency. Classic relevance algorithm.
- BM25 — Improved TF-IDF with field-length normalization. Elasticsearch default.
- Learning to Rank — ML model trained on click data for custom ranking.
- Semantic search — Vector embeddings for meaning-based search.
- Hybrid — Combine BM25 + semantic for best of both.
Search Optimization
- Shard routing — Use routing to co-locate related documents.
- Index caching — Cache frequently accessed field data.
- Query caching — Cache frequent query results.
- Prefiltering — Use filters (cached) before queries (uncached).
- Denormalization — Pre-compute joins to avoid runtime joins.
Filters are cached and do not affect scoring. Use filters for category, status, and date range. Use queries for full-text search. This improves performance and relevance.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.