Search Systems
Reference: Sujeet Pro β Distributed Search Engine
Why Not Just Use SQL?
-- SQL LIKE is O(n) β full table scan, no ranking
SELECT * FROM products WHERE description LIKE '%bluetooth speaker%';
-- 1M products β scans all 1M rows, no relevance score
Elasticsearch advantages:
- Inverted index β O(1) term lookup
- Relevance scoring (TF-IDF, BM25)
- Fuzzy matching, stemming, synonyms
- Near real-time search (1s delay after index)
- Horizontal scaling built-in
Inverted Index
Documents:
Doc1: "bluetooth speaker portable"
Doc2: "wireless speaker home"
Doc3: "bluetooth headphones portable"
Inverted Index:
"bluetooth" β [Doc1, Doc3]
"speaker" β [Doc1, Doc2]
"portable" β [Doc1, Doc3]
"wireless" β [Doc2]
"headphones" β [Doc3]
Query "bluetooth speaker":
"bluetooth" β [Doc1, Doc3]
"speaker" β [Doc1, Doc2]
Intersection β Doc1 (highest score: matches both terms)
Elasticsearch Concepts
| Concept | SQL Equivalent |
|---|---|
| Index | Table |
| Document | Row |
| Field | Column |
| Mapping | Schema |
| Shard | Partition |
| Replica | Read replica |
Index Settings
PUT /products
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1,
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "stop", "snowball"]
}
}
}
},
"mappings": {
"properties": {
"name": { "type": "text", "analyzer": "my_analyzer" },
"name_keyword": { "type": "keyword" },
"price": { "type": "float" },
"category": { "type": "keyword" },
"description": { "type": "text" },
"tags": { "type": "keyword" },
"created_at": { "type": "date" }
}
}
}
Common Query Patterns
Full-Text Search with Boost
GET /products/_search
{
"query": {
"multi_match": {
"query": "bluetooth speaker",
"fields": ["name^3", "description", "tags^2"],
"type": "best_fields",
"fuzziness": "AUTO"
}
}
}
Boolean Query
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "speaker" } }
],
"filter": [
{ "range": { "price": { "gte": 50, "lte": 200 } } },
{ "term": { "category": "electronics" } },
{ "term": { "in_stock": true } }
],
"should": [
{ "match": { "tags": "featured" } }
]
}
},
"sort": [
{ "_score": "desc" },
{ "created_at": "desc" }
]
}
Faceted Search (Aggregations)
GET /products/_search
{
"query": { "match": { "name": "speaker" } },
"aggs": {
"by_category": {
"terms": { "field": "category", "size": 10 }
},
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 50 },
{ "from": 50, "to": 100 },
{ "from": 100, "to": 200 },
{ "from": 200 }
]
}
},
"avg_price": { "avg": { "field": "price" } }
}
}
Spring Data Elasticsearch
// Document mapping
@Document(indexName = "products")
@Setting(settingPath = "es-settings.json")
public class ProductDocument {
@Id
private String id;
@MultiField(
mainField = @Field(type = FieldType.Text, analyzer = "english"),
otherFields = @InnerField(suffix = "keyword", type = FieldType.Keyword)
)
private String name;
@Field(type = FieldType.Float)
private BigDecimal price;
@Field(type = FieldType.Keyword)
private String category;
@Field(type = FieldType.Date)
private Instant createdAt;
}
// Repository
public interface ProductSearchRepository
extends ElasticsearchRepository<ProductDocument, String> {
@Query("{\"multi_match\": {\"query\": \"?0\", \"fields\": [\"name^3\", \"description\"]}}")
Page<ProductDocument> search(String query, Pageable pageable);
}
// Service with custom query
@Service
public class ProductSearchService {
@Autowired private ElasticsearchOperations esOps;
public SearchResult<ProductDocument> search(ProductSearchRequest req) {
Query query = NativeQuery.builder()
.withQuery(q -> q.bool(b -> b
.must(m -> m.multiMatch(mm -> mm
.query(req.getKeyword())
.fields("name^3", "description", "tags^2")
.fuzziness("AUTO")
))
.filter(f -> f.range(r -> r
.field("price")
.gte(JsonData.of(req.getMinPrice()))
.lte(JsonData.of(req.getMaxPrice()))
))
))
.withPageable(PageRequest.of(req.getPage(), req.getSize()))
.build();
return esOps.search(query, ProductDocument.class);
}
}
Autocomplete / Search-as-You-Type
Completion Suggester
PUT /products
{
"mappings": {
"properties": {
"name_suggest": {
"type": "completion",
"analyzer": "simple",
"search_analyzer": "simple"
}
}
}
}
// Index document with suggestions
{
"name": "Bluetooth Speaker",
"name_suggest": {
"input": ["Bluetooth Speaker", "bluetooth speaker", "speaker"],
"weight": 100
}
}
// Query
GET /products/_search
{
"suggest": {
"product_suggest": {
"prefix": "blue",
"completion": {
"field": "name_suggest",
"size": 5,
"fuzzy": { "fuzziness": 1 }
}
}
}
}
Edge N-Gram (Type-ahead)
Better for partial word matching:
"analysis": {
"tokenizer": {
"edge_ngram_tokenizer": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
}
}
// "blue" β "b", "bl", "blu", "blue"
// Searching "blu" matches "blue", "bluetooth", etc.
Search Architecture
User Input β Search API
β
Query Parser (handle special chars, operators, quotes)
β
Query Builder (build ES query with filters, boosts)
β
Elasticsearch Cluster
(5 shards, 1 replica each = 10 nodes total for 100M docs)
β
Result Ranker (personalization, A/B test scoring)
β
Response Formatter
β
Client
Data Sync: DB β Elasticsearch
Option 1: Dual-write (write to DB + ES in same transaction)
Risk: partial failure β inconsistency
Option 2: CDC (Change Data Capture) via Debezium
DB binlog β Debezium β Kafka β ES Consumer
Reliable, near real-time (~1s lag)
Option 3: Batch sync (nightly full reindex)
Simple, but stale data during day
// Debezium + Kafka β ES indexer
@KafkaListener(topics = "dbserver1.public.products")
public void onProductChange(DebeziumMessage msg) {
if ("DELETE".equals(msg.getOp())) {
productSearchRepository.deleteById(msg.getBefore().getId());
} else {
ProductDocument doc = mapper.toDocument(msg.getAfter());
productSearchRepository.save(doc);
}
}
Relevance Tuning
| Technique | Effect |
|---|---|
| Field boosting | Name matches > description matches |
| Freshness boost | Recent products scored higher |
| Popularity boost | High-view products ranked up |
| Personalization | User's history influences ranking |
| Synonym expansion | "TV" matches "television" |
| Stemming | "running" matches "run", "runs" |
| Fuzzy matching | "speker" matches "speaker" |
Distributed Search Engine Internals (Lucene, FST, BM25, WAND & Sharding)
Reference: Sujeet Pro β Distributed Search Engine
When designing a hyperscale search engine (Elasticsearch, OpenSearch, Apache Solr, or custom Lucene clusters) over billions of documents, a Staff/Principal engineer must explain how the storage engine compresses inverted indexes into RAM, achieves 1-second Near Real-Time (NRT) visibility, prunes millions of postings via Block-Max WAND, and executes distributed Scatter-Gather queries.
1. Inverted Index Internals: FST Term Dictionary, FOR Delta Encoding & Roaring Bitmaps
A production inverted index does not store a naive HashMap<String, List<Integer>> in memory (which would immediately trigger OutOfMemoryError at 100M+ unique terms). Instead, Apache Lucene splits the index into three ultra-compressed physical layers:
[1. Term Index (FST in RAM)] [2. Term Dictionary (.tim)] [3. Posting Lists (.doc / .pos)]
ββββββββββββββββββββββββββββ βββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββ
β Finite State Transducer β β On-Disk Sorted Terms β β Compressed DocIDs + Term Freqs β
β "bl" βββΊ Block Offset β ββββΊ β β’ "bluetooth" (DF: 42k) β βββΊ β β’ FOR Delta: [102, +3, +14, +2] β
β "sp" βββΊ Block Offset β β β’ "speaker" (DF: 91k) β β β’ Roaring Bitmap (Filter Cache) β
ββββββββββββββββββββββββββββ βββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββ
- Term Index via Finite State Transducer (FST in Heap RAM):
- Storing every unique string term in RAM is prohibitively expensive. Lucene builds a Finite State Transducer (FST)βa directed acyclic word graph (automaton) that shares both common prefixes and common suffixes across terms while mapping each prefix to the disk block offset of the
.timTerm Dictionary. - Memory Footprint: Compresses the in-memory term index by 10xβ40x, allowing a shard with billions of documents to keep its entire FST prefix index in just a few dozen megabytes of JVM heap.
- Storing every unique string term in RAM is prohibitively expensive. Lucene builds a Finite State Transducer (FST)βa directed acyclic word graph (automaton) that shares both common prefixes and common suffixes across terms while mapping each prefix to the disk block offset of the
- Posting List Compression via Frame-of-Reference (FOR) Delta Encoding:
- A Posting List stores sorted monotonic Document IDs (
[1004, 1007, 1015, 1020, ...]) plus Term Frequencies (tf), Positions (posfor phrase queries), and Offsets. - Instead of storing 32-bit integers (4 bytes each), Lucene computes Delta Gaps between consecutive IDs (
[1004, 3, 8, 5, ...]), groups deltas into blocks of 128 integers, and packs every delta in the block using the bit-width of the maximum delta in that block (Frame of Reference [FOR] / SIMD-BP128 bit-packing). If the max delta in a 128-doc block is12, every DocID takes only 4 bits instead of 32 bits!
- A Posting List stores sorted monotonic Document IDs (
- Roaring Bitmaps for Filter Caching & Set Intersections:
- When executing
bool.filterclauses (e.g.,in_stock = true AND category = "electronics"), Lucene caches the matching Document ID set as a Roaring Bitmap inside the OS/Node Filter Cache:- Divides the 32-bit DocID space into chunks of integers (keyed by the upper 16 bits).
- Sparse Chunk ( docs): Stored as a sorted
uint16[]array (2 bytes per doc). - Dense Chunk ( docs): Stored as a fixed (
8 KB) bitset enabling single-instruction CPU AVX-512 bitwiseAND/ORintersections. - Contiguous Runs: Compressed via Run-Length Encoding (RLE).
- When executing
2. Lucene Immutable Segments, Tiered Merging & Near Real-Time (NRT) Lifecycle
Why are Lucene segments strictly immutable, and how does Elasticsearch make newly indexed documents searchable within 1 second without calling expensive disk fsync on every write?
Incoming Document Write (PUT /products/_doc/1)
β
ββββββββββββββββββββββββββββββββββββββββΊ [1. In-Memory IndexBuffer]
β β
β (Every 1s: Refresh)
β βΌ
β [2. OS Page Cache Segment] βββ Searchable in 1s (NRT)!
βΌ β
[3. Translog (WAL)] (Every 30m or Translog Full: Flush / Lucene Commit)
(fsync every 5s or per-request) βΌ
ββββββββββββββββββββββββββββββββββββββββΊ [4. Physical Disk Segment (.cfs/.doc) + Truncate Translog]
- Why Immutability? Rewriting compressed FSTs and delta-encoded posting lists in-place when a single document changes is physically impossible without corrupting bit-packed offsets. Therefore, Lucene writes new documents into new, immutable mini-inverted indexes called Segments:
- Lock-Free Concurrent Reads: Because segments never mutate, search threads require zero locks and rely heavily on the Linux OS Page Cache (
mmap). - Deletes & Updates: Deleting a document merely flips a bit in a per-segment
.liv(Live Docs Bitset) file. Updating a document atomically marks the old doc bit as deleted in.livand appends the new version into a new segment.
- Lock-Free Concurrent Reads: Because segments never mutate, search threads require zero locks and rely heavily on the Linux OS Page Cache (
- The 4-Stage Near Real-Time (NRT) Indexing Lifecycle:
- Stage 1 β
IndexBuffer+TranslogAppend: Incoming documents are parsed/analyzed in the JVMIndexBufferand simultaneously appended to an append-only Write-Ahead Log (translog) for crash durability. At this moment, the document is NOT yet searchable. - Stage 2 β
Refresh(Default every1s): Once per second, theIndexBufferis written into a new immutable Lucene segment inside the Linux OS Page Cache (without waiting for a slow diskfsync!) and opened by anIndexReader. The document is now searchable within 1 second (NRT)! - Stage 3 β
Translog fsync: Configured either per-request (index.translog.durability: request) or asynchronously every5s(async) to guarantee zero data loss on power failure. - Stage 4 β
Flush/ LuceneCommit: Every 30 minutes (or whentranslogexceeds 512 MB), all in-memory OS page cache segments arefsynced to physical disk, a new commit point (segments_N) is written, and the oldtranslogis truncated.
- Stage 1 β
- Tiered Segment Merging (Background Compaction):
- Creating a new segment every 1 second produces thousands of tiny segments, slowing down search (since a query must check every segment) and wasting file descriptors.
- A background
TieredMergePolicymerges similarly sized small segments into larger segments, physically purging tombstoned documents marked in.livand reclaiming disk space.
3. Relevance Scoring: Okapi BM25 Math & Block-Max WAND Early Termination
β Why BM25 Replaced Classic TF-IDFβ
In classic TF-IDF, if the word "speaker" appears 50 times in a spammy product description, its Term Frequency () score is higher than a concise document mentioning "speaker" once. Okapi BM25 (Best Matching 25) fixes this via asymptotic TF saturation and document length normalization:
Where:
- : Inverse Document Frequency ( = total docs in shard, = docs containing term ). Rare terms (
"sennheiser") carry massive weight; common terms ("the") approach 0. - : Raw Term Frequency of term in document .
- (TF Saturation Parameter, default ): Controls how quickly additional occurrences of a term hit diminishing returns. Even as , the term's TF factor asymptotically caps at .
- (Document Length Normalization, default ): Penalizes long documents () that match terms purely by verbosity, while setting disables length normalization (useful for short SKU titles).
β‘ WAND (Weak AND) & Block-Max WAND (Lucene 8+ Dynamic Pruning)β
If a user searches "bluetooth speaker" and 5,000,000 documents match, evaluating the floating-point BM25 formula on all 5 million documents to return only the Top results wastes 99.9% of CPU cycles:
- WAND (Broder et al.): Maintains the current minimum score in the Top- Min-Heap and stores the Maximum Possible Impact Score () for each query term across the entire segment. By sorting posting cursors by current
DocIDand summing until the sum exceeds , WAND skips (advance(pivotDocId)) millions of documents that cannot mathematically beat . - Block-Max WAND (BMW): Global segment is often skewed by one single outlier document. Block-Max WAND stores the upper-bound BM25 impact score inside every 128-document FOR block. During traversal, if the sum of block-level max scores for the current 128-doc range is , Lucene skips the entire 128-document block without decompressing it, accelerating Top-10 queries by 3xβ10x!
4. Distributed Partitioning: Document-Based vs. Term-Based & Scatter-Gather Execution
How should a 10-billion-document inverted index be partitioned across 100 cluster nodes?
| Partitioning Strategy | How It Works | Query Execution Flow | Pros & Cons |
|---|---|---|---|
| Document-Based Partitioning (Local Index β Elasticsearch / OpenSearch Default) | Documents are sharded by hash(doc_id) % N. Each shard holds a complete, self-contained Local Inverted Index for its subset of documents. | Scatter-Gather: Coordinator broadcasts query to all shards in parallel; each shard computes its local Top-; coordinator merges into global Top-. | Pros: Fast single-shard document indexing; fault isolation (1 dead shard = results still available). Cons: Every query hits all shards; IDF skew on small shards. |
| Term-Based Partitioning (Global Index) | The inverted index is partitioned by Term: Shard 1 holds posting lists for terms "a".."f", Shard 2 holds "g".."m", etc. | Coordinator routes query only to the shards holding the specific query terms ("bluetooth" shard + "speaker" shard). | Pros: High throughput for single-term queries; exact global IDF. Cons: Catastrophic Multi-Term Network Join (must ship multi-megabyte posting lists across the network to intersect AND queries); indexing 1 doc with 200 words writes to all shards! |
The Two-Phase Scatter-Gather Protocol (QUERY_THEN_FETCH vs. DFS_QUERY_THEN_FETCH)β
In Document-Based Partitioning, fetching full JSON _source payloads from all shards would saturate network bandwidth. Instead, Elasticsearch executes a Two-Phase Scatter-Gather:
- Phase 1 β Query Phase:
- Client sends
GET /products/_search?size=10to any node (which acts as the Coordinating Node). - Coordinating Node fans out the lightweight query to a primary or replica of all shards.
- Each shard executes Block-Max WAND over its local segments and returns only the Top 10
(doc_id, _score)pairs (just ~160 bytes per shard!).
- Client sends
- Phase 2 β Fetch Phase:
- The Coordinating Node merges the lightweight score tuples in a Priority Queue, picks the true Global Top 10, and sends a targeted
GET _sourcerequest only to the specific shards holding those 10 winning documents.
- The Coordinating Node merges the lightweight score tuples in a Priority Queue, picks the true Global Top 10, and sends a targeted
- The IDF Skew Problem &
DFS_QUERY_THEN_FETCH:- By default (
QUERY_THEN_FETCH), each shard calculates using only its local shard document count, which can skew rankings if rare terms cluster unevenly on one shard. - Setting
search_type=dfs_query_then_fetchadds a pre-round trip (Distributed Frequency Search [DFS]) that gathers term frequencies across all shards first to compute the exact Global IDF before scoringβat the cost of an extra network RTT.
- By default (
Interview Questions
Q: Why is Elasticsearch (or search engine) better than SQL LIKE for full-text search?
A: Search engines use analyzers, inverted indexes, and ranking algorithms optimized for linguistic matching. SQL LIKE is limited, often scans heavily, and lacks relevance scoring.
Q: What is an inverted index? How does it enable fast search?
A: An inverted index maps terms to posting lists of matching document IDs. Queries become fast set operations on postings instead of scanning full documents.
Q: How do you keep Elasticsearch in sync with your primary database?
A: Publish DB changes through CDC or outbox events and update Elasticsearch asynchronously with retries. Keep reindex/replay tooling for drift recovery.
Q: What is BM25 and how does it improve over TF-IDF?
A: BM25 is a probabilistic ranking function that better normalizes term frequency and document length. It usually produces more relevant rankings than plain TF-IDF defaults.
Q: How would you design an autocomplete/search-as-you-type feature?
A: Index edge n-grams or use completion suggester with popularity signals and typo tolerance. Cache top prefixes and debounce client queries.
Q: What is faceted search and how do you implement it?
A: Faceted search shows aggregated counts by attributes (brand, price range, category) alongside results. Implement with filtered aggregations over indexed keyword/numeric fields.
Q: How do you handle Elasticsearch going down while still accepting writes to your primary DB?
A: Keep DB as source of truth, queue indexing events durably, and replay when cluster recovers. Degrade gracefully by serving fallback search modes if needed.
Q: How would you scale Elasticsearch to handle 1 billion product documents?
A: Plan shard count by index size/throughput, use index lifecycle tiers, and separate ingest from query workloads. Optimize mappings, refresh intervals, and replica strategy for SLA.
Q: What are the trade-offs of Elasticsearch's near real-time (~1s) indexing?
A: NRT improves throughput by batching refreshes but introduces short visibility delay after writes. Lower refresh intervals reduce delay but increase indexing overhead.
Q: How do you tune relevance in search results?
A: Tune analyzers, field boosts, synonym handling, and business signals (freshness/popularity). Evaluate with offline judgments and online A/B metrics like CTR and conversion.
Case Study: Hotel Search at Scale
From the freeCodeCamp Design Software Systems Like a Senior Engineer course.
- Index one document per hotel with a
geo_point, amenities, rating, and a denormalised price and availability summary. - Query with a
geo_distancefilter plustermsfilters for amenities and arangefilter for price. Sort by relevance blended with price or rating. - Date availability is the hard filter. Keep a precomputed per-night availability summary in the index, and treat it as approximate.
- Feed the index from
inventory.changedevents (outbox or CDC). A lag of a few seconds is acceptable. - Search is a hint, booking is the truth. The booking path re-checks the OLTP database at hold time, so staleness costs a rare "just sold out" message, never a double booking.
See also: OpenSearch Architecture Deep Dive & Splunk-to-OpenSearch Migration Guide, OpenSearch Query, Search & Aggregation Guideline, OpenSearch Indexing, Schema Design & Day-2 Operations Guideline, Senior Engineer Design Course, Inventory Reservation System.
Senior/Staff Interview Playbook: How to Ace Distributed Search Engine Interviews
Reference: Sujeet Pro β Distributed Search Engine
1. The Senior Framing Strategy (First 60 Seconds)
When asked to "Design a Distributed Search Engine (like Elasticsearch, E-Commerce Product Search, or Twitter Search)", establish these four architectural pillars in the first 60 seconds:
- State Non-Negotiable Invariants Upfront:
- Search is an Eventually Consistent Read Projection (Never the System of Record): The OLTP database owns transactional truth; the search cluster ingests mutations asynchronously via CDC (Debezium + Kafka) with a 1-second Near Real-Time (NRT) visibility SLA.
- Immutable Segment Architecture: Inverted index segments are written once and never mutated in placeβenabling lock-free concurrent reads cached directly in the Linux OS Page Cache (
mmap).
- Back-of-the-Envelope Capacity & Memory Physics: Never allocate 100% of server RAM to the JVM Heap! Explicitly state the 50% JVM Heap / Compressed Oops Rule: on a 64 GB RAM search node, cap JVM Heap at 30β31 GB (to keep 64-bit object pointers compressed to 32-bit
CompressedOops) for the FST Term Index, Aggregation Heap, and IndexBuffer, leaving the remaining 32+ GB of physical RAM to the Linux OS Page Cache to hold memory-mapped.doc/.timinverted index segments. Also size shard counts so each shard stays between 20 GB and 50 GB. - Proactive Failure-Mode Identification: Call out the four production failure modes before the interviewer asks:
- Scatter-Gather Tail-Latency Amplification: Fanning a query out to 100 shards means global latency equals the slowest shard ( of 100 shards of a single shard)βmitigated via Adaptive Replica Selection (ARS) and custom Routing Keys (
_routing=tenant_id). - Deep Pagination OOM (
from: 100000, size: 10): Forcing every shard to sort and return 100,010 tuples to the coordinating node (solved viasearch_after+ Point-in-Time [PIT] cursor pagination). - High-Frequency Refresh Thrashing: Refreshing on every write creates thousands of tiny segments and starves disk I/O with background merges.
- Unbounded Mapping Explosion: Dynamic JSON fields creating 50,000+ mapping attributes and crashing the cluster state manager.
- Scatter-Gather Tail-Latency Amplification: Fanning a query out to 100 shards means global latency equals the slowest shard ( of 100 shards of a single shard)βmitigated via Adaptive Replica Selection (ARS) and custom Routing Keys (
- Day-2 Operational Realism: Mention Zero-Downtime Alias Reindexing (
products_v1 -> products_v2behind read aliasproducts_read) for mapping changes, Tiered Segment Merging, and Block-Max WAND pruning for sub-50ms P99 query SLAs.
2. Classic Interviewer Traps & Principal Rebuttals
Trap 1: "Why does Elasticsearch take 1 full second (refresh_interval: 1s) to make a document searchable? Why don't we just fsync the inverted index to disk on every POST /doc so search is strongly consistent and instant?"β
- Junior/Mid Answer: "Disk
fsyncis slightly slow, so Elasticsearch waits 1 second to batch writes to disk." - Principal Rebuttal:
"The 1-second
refreshdoes NOT evenfsyncsegments to disk! Constructing an inverted index segment (sorting terms lexicographically, building the Finite State Transducer [FST], and delta-encoding posting lists with Frame-of-Reference [FOR]) is CPU- and memory-intensive. If we generated andfsynced a new immutable Lucene segment on every single document write, the node would create thousands of tiny segment files per second, exhausting file descriptors and crushing disk IOPS.Instead, Lucene decouples Search Visibility (
refresh) from Disk Durability (translog+flush):- Every write appends to an append-only Translog (WAL) (which handles crash durability) and enters an in-memory
IndexBuffer. - Every 1 second (
refresh), theIndexBufferis compiled into a new immutable segment written only to the Linux OS Page Cache (zero diskfsync), making it immediately readable byIndexReader. - Only during a periodic
flush(Lucenecommit) are those OS page cache segments actuallyfsynced to physical disk and thetranslogtruncated. For bulk backfills, we temporarily setrefresh_interval: 30sor-1to increase indexing throughput by 5x."
- Every write appends to an append-only Translog (WAL) (which handles crash durability) and enters an in-memory
Trap 2: "To scale our search engine to 10 billion documents, shouldn't we partition the index by Term (Shard 1 gets 'aβd', Shard 2 gets 'eβh') so a query only touches the shards for the words in the query instead of all 100 shards?"β
- Junior/Mid Answer: "Yes! Term-based partitioning is much more scalable because a 2-word query only hits 2 shards instead of broadcasting to all 100 shards."
- Principal Rebuttal:
"While Term-Based Partitioning (Global Index) looks attractive on paper, it fails catastrophically for interactive multi-term search due to Cross-Network Posting List Joins and Write Amplification:
- Read Path Network Bottleneck: Suppose a user searches
'bluetooth AND speaker'. Shard A holds the posting list for'bluetooth'(5 million DocIDs) and Shard B holds'speaker'(8 million DocIDs). To intersect them, Shard A must serialize and transmit megabytes of posting lists over the network to Shard B or the coordinator on every query, destroying sub-50ms latency SLAs. - Write Path Fanout: Indexing a single document containing 300 unique words requires a distributed write transaction across nearly all 100 term shards!
With Document-Based Partitioning (Local Index), each shard holds a complete inverted index for its local document slice. Every shard intersects
'bluetooth AND speaker'locally in RAM/CPU cache using Roaring Bitmaps and Block-Max WAND, returning only the top 10(doc_id, score)tuples (~160 bytes) to the coordinator." - Read Path Network Bottleneck: Suppose a user searches
Trap 3: "What happens when a user clicks 'Page 5,000' (from: 50000, size: 10) on a 20-shard Elasticsearch cluster, and how do you fix it?"β
- Junior/Mid Answer: "Each shard skips the first 50,000 results and returns 10 results, which is fast because the index is sorted."
- Principal Rebuttal:
"A shard cannot skip its first 50,000 local results because the coordinator doesn't know which shard holds the globally top-ranked documents! If all 50,000 globally highest-scoring documents happen to reside on Shard 3, skipping on Shard 3 would lose the actual results.
Consequently, in a 20-shard cluster,
from: 50000, size: 10forces every single shard to score, sort, and return its Top 50,010 tuples to the Coordinating Node. The Coordinating Node must allocate a priority queue in JVM heap for tuples just to discard 1,000,190 of them and return 10! That is why Elasticsearch enforces a hard guard atindex.max_result_window = 10000.In production, we ban deep
from/sizeoffset pagination and usesearch_afterwith a Point-in-Time (PIT) snapshot: the client passes the sort tuple[_score, _id]of the last item on the current page. Each shard filters strictly for entries after that cursor and returns only 10 items ( items merged at the coordinator), keeping latency constant at regardless of page depth."
Trap 4: "If a query term like 'wireless' matches 20 million documents on a shard, does Lucene compute the floating-point BM25 score for all 20 million documents?"β
- Junior/Mid Answer: "Yes, it iterates through the posting list for
'wireless', calculates BM25 for each matching document, and keeps the top 10 in a min-heap." - Principal Rebuttal:
"Scoring 20 million documents in floating-point math on every query would blow past our 50ms SLA. Modern Lucene (8.0+) uses Block-Max WAND (Weak AND) whenever
track_total_hitsis not forced totrue(default caps exact count at 10,000).Every 128-document Frame-of-Reference (FOR) block in the posting list stores a precomputed Block-Max Impact Score (the maximum possible BM25 contribution of any document in that block, given max
tfand min doc length). During query evaluation, Lucene compares the sum of block-max upper bounds against the current 10th-best score in the Top-10 Min-Heap. If a 128-document block cannot mathematically beat , Lucene skips the entire compressed block without even decoding its DocIDs, routinely pruning 90%+ of matching postings."
3. Quick Reference β Mid-Level vs. Principal Candidate Cheat Sheet
| Concept / Scenario | Mid-Level Candidate Says | Principal Candidate Demonstrates |
|---|---|---|
| Inverted Index Memory Layout | "Maps each word in a hash map to a list of document IDs." | "Details the 3-tier physical layout: FST (Finite State Transducer) prefix index in JVM heap .tim Term Dictionary FOR (Frame-of-Reference) 128-block delta-encoded postings + Roaring Bitmaps in OS Page Cache." |
| Near Real-Time (NRT) Writes | "Elasticsearch indexes documents in 1 second." | "Traces the 4-stage lifecycle: IndexBuffer + Translog WAL 1s Refresh (writes immutable segment to OS Page Cache without fsync) 5s Translog fsync Periodic Flush/Commit + Tiered Segment Merging." |
| Relevance Ranking & Pruning | "Uses TF-IDF or BM25 to score documents." | "Writes the BM25 equation explaining TF saturation () and length normalization (), paired with Block-Max WAND early termination to skip non-competitive 128-doc blocks." |
| Sharding & Query Execution | "Split the index into shards and query them." | "Contrasts Document Partitioning (Local Index) vs. Term Partitioning (Global Index), detailing two-phase Scatter-Gather (QUERY_THEN_FETCH vs. DFS_QUERY_THEN_FETCH)." |
| JVM Heap & Hardware Sizing | "Give Elasticsearch as much JVM Heap RAM as possible." | "Enforces the JVM Heap CompressedOops boundary (50% RAM rule), dedicating the remaining 50%+ RAM to the Linux OS Page Cache for memory-mapped Lucene segments." |
