Problem
Searching for words across millions of documents with LIKE scans is slow and cannot rank results by relevance. Users expect typo tolerance, stemming and fast filtering. A relational B-tree index cannot answer arbitrary term queries.
Also known as: full-text-search-index, search-index
Map terms to the documents that contain them so that text queries are answered without scanning every document.
Searching for words across millions of documents with LIKE scans is slow and cannot rank results by relevance. Users expect typo tolerance, stemming and fast filtering. A relational B-tree index cannot answer arbitrary term queries.
An inverted index flips the document-to-words relationship into word-to-documents. It is the core of Lucene, Elasticsearch, OpenSearch and Solr, and of the full-text features in many databases.
Indexing and querying with a tiny example.
doc1: "fast database engine"
doc2: "database index"
term postings
database [doc1, doc2]
engine [doc1]
fast [doc1]
index [doc2]
query "database AND index" -> [doc1,doc2] intersect [doc2] = [doc2]
Segments are immutable. Updates mark old versions deleted and write new documents, and merges later reclaim space.
Relevance scoring with BM25 weighs how often a term appears in a document against how rare it is across the corpus, with length normalization so long documents do not dominate. Filters and facets use separate doc-value structures, which are columnar, so sorting and aggregations do not need to load documents. Near-real-time visibility comes from a periodic refresh that opens new segments, typically every second, which is why a just-written document may not appear in search immediately.
Medium - memory and disk for index segments and caches
Medium - shard sizing, merges and reindexing
Medium - analyzers, scoring and mappings
Index drifts from the source of truth after missed update events
Hot shards or oversized shards degrade query latency
Mapping change requires a full reindex that overloads the cluster