The inverted index

Elasticsearch Course · lesson 2 of 19 · 5 min read

The one data structure that makes full-text search fast, and what it does not do.

Open this lesson in the learning hub

Key points

  • A forward index maps a document to its text; the inverted index maps a term to its documents.
  • Terms are held sorted, so locating elasticsearch is a dictionary seek, not a scan.
  • Each term points at a postings list: matching doc ids, term frequency, and token positions.
  • Those positions are what makes match_phrase possible - order and adjacency are recorded.
  • Lucene writes this into immutable segments, so a delete only flips a bit in a liveDocs file.
  • Sorting and aggregating read a separate column store called doc values, not the inverted index.

Example

Documents
  1: "Spring Boot tuning"
  2: "Boot time and JVM tuning"
  3: "Elasticsearch tuning guide"

Inverted index (after the standard analyzer)
  and            -> [2 (pos 2)]      <- kept: the standard analyzer removes NO stopwords
  boot           -> [1 (pos 1), 2 (pos 0)]
  elasticsearch  -> [3 (pos 0)]
  guide          -> [3 (pos 2)]
  jvm            -> [2 (pos 3)]
  spring         -> [1 (pos 0)]
  time           -> [2 (pos 1)]
  tuning         -> [1 (pos 2), 2 (pos 4), 3 (pos 1)]

Matching reads the inverted index; sorting and aggregating read doc values - different structures.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Elasticsearch Course course, and every lesson in it is listed on the Elasticsearch Course contents page.