SDE & MLE Prep

Search & Inverted Indexes

Inverted index: term → posting list of document ids (+ positions, frequencies). Queries intersect/union posting lists; ranking with BM25 (term frequency with saturation × inverse document frequency, normalized by document length), optionally followed by ML re-ranking.

doc 1 "red fox"   doc 2 "red panda"   ->   red: [1, 2]   fox: [1]   panda: [2]
query "red fox"  ->  [1, 2] ∩ [1]  ->  doc 1
  • Text analysis: tokenize, lowercase, stem/lemmatize, stop words, synonyms, edge n-grams for prefix search.
  • Elasticsearch / OpenSearch: an index is split into shards (each a Lucene index) with replicas; new documents become searchable on refresh (~1 s, near real time); immutable segments are merged in the background.
  • Keep the source of truth in the primary database and feed the index through CDC (e.g. Debezium → Kafka → indexer) so it can always be rebuilt; search is eventually consistent.
  • Partitioning: by document (every query fans out to all shards, gathers top-k — the default) vs by term (fewer shards per query, harder writes). Paginate with search_after cursors, not deep from/size.
  • Not a primary database: no multi-document transactions, eventual consistency, reindex to change mappings.