Inverted index
A map from each word to the list of documents that contain it; the structure behind keyword search engines and BM25.
A book’s index points from a word to the pages it appears on; an inverted index does the same for documents. Each term gets a posting list of document ids, often with positions and counts. A query for two words intersects two lists instead of reading every document, so search stays fast on millions of documents.
It is the core of Lucene, and so of Elasticsearch, OpenSearch and Solr. In Postgres, full-text search uses a GIN index, short for generalized inverted index. The counts stored in it (how many documents contain a term, how often it appears) are exactly what BM25 needs to rank results.
What counts as a “term” is decided before indexing by the analyzer: lowercasing, stemming (“running” to “run”), stop words and language rules. A wrong analyzer for the language is a common reason search misses obvious matches. Updating a posting list is costlier than writing one row, so search indexes often lag a little behind the database.
Sources
- Apache Lucene: Lucene Core lucene.apache.org
- PostgreSQL: GIN Indexes postgresql.org
Related terms
- BM25: The standard keyword-search ranking formula: it scores documents by how often query words appear, weighting rare words more.
- Hybrid search: Combining keyword search (such as BM25) with vector search, so results match both exact terms and meaning.
- Database index: An extra data structure a database keeps next to a table so it can find matching rows without reading the whole table.
- N-gram: A run of n consecutive words or characters; the unit behind fuzzy text search, BLEU and ROUGE scores and the language models before neural networks.
- Chunking (RAG): Splitting documents into smaller passages before indexing them for search, so retrieval returns the relevant part instead of a whole file.
- HNSW (hierarchical navigable small world): A layered graph index that finds approximately nearest vectors fast; the default index in most vector databases, trading a little recall for speed.
