How does Elasticsearch make "search for a word in millions of docs" fast?
Common in middleware/search-adjacent roles. Knowing the inverted-index principle separates users from designers.
Build time: analyzer tokenizes text, normalizes (lowercase, stem), then writes a posting list per term: term → sorted list of (docId, position).
Query time: look up term in the index (hash/btree), get posting list, for multi-term AND intersect lists, OR union.
Scoring: BM25 by default — term frequency in doc, inverse doc frequency, length normalization. Higher = more relevant.
Segments: Lucene writes immutable segments, merges periodically. Deletions are tombstones; updates = delete+insert.
Sharding: index split into shards by hash(_id). Each shard is a Lucene index. Query fans out to all shards, coordinator merges.
At index time, ES runs text through an analyzer (tokenize → lowercase → stem → stopwords → ngram maybe) and writes a posting list per token: term → list of (docId, positions). At query time, the token list is looked up (internal index is a finite state transducer + block trees for fast prefix), postings are intersected for AND or merged for OR, and candidates are scored by BM25 — basically TF × IDF with length normalization. That's why searching for a word is O(log vocab + |results|), not O(|docs|). Under the hood, Lucene writes immutable segments that get merged periodically — deletions are tombstones, updates are delete+insert. Sharding partitions by doc ID hash; queries fan out and the coordinator merges scored results. ES is great for full-text relevance, less great for high-write transactional workloads (eventually consistent, slow individual writes due to segment flush).
Analyzer choice dominates quality — Chinese needs IK or jieba, English default is usually fine.
Avoid deep pagination (`from + size > 10K`) — use `search_after` cursor instead.
HireMe AI (即答侠) includes the analyzer → posting-list → BM25 pipeline as one diagram so you can whiteboard fast.
ES wins on scale, relevance tuning, and aggregations. Postgres full-text (tsvector) is fine for small corpora and avoids operating a second store.
Mark old as deleted, write new as a new doc. Actual space reclaimed during segment merge, not immediately.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free