liveBook · Manning

Inverted index

As explained in Elasticsearch in Action, Second Edition

An inverted index is an index that maps each token or term to the documents containing it.

An inverted index maps terms to the documents that contain them. Think of the index at the back of a printed book: look up a word, then follow its listed page numbers. In Elasticsearch, look up a search term, then use its associated document identifiers to retrieve matching documents. This lookup structure makes full-text retrieval fast. section 3.3 section 1.3.1

When a JSON document is indexed, each full-text field is analyzed before its terms are added to an inverted index. Analysis can remove markup, split text into tokens, and normalize those tokens. Elasticsearch creates this structure for full-text fields; numeric and geoshape data instead use BKD trees. section 3.3 section 3.7

Greeting documents

Given document 1 with greeting "Hello, WORLD" and document 2 with greeting "Hello, Mate", analysis produces lowercase terms. The index can associate hello with documents 1 and 2, world with document 1, and mate with document 2. Term frequency can also be stored alongside each term-document association. section 3.3 section 3.3

The terms stored in the index are the analyzed terms, not necessarily the original input. A title such as "The Godfather" becomes the separate lowercase tokens "the" and "godfather" under the standard analyzer. Query behavior must account for that representation. section 9.1.2

Why an exact term query can miss

A term query for "The Godfather" does not match a text-field index containing "the" and "godfather": term-level queries pass the supplied value through unchanged. A query for "godfather" can match because that exact token exists. For text search, match queries analyze search words, ordinarily using the same analyzer used at indexing time. section 9.2.2 section 10.5.3

Use an inverted index with these operational expectations:

  • Index documents before searching; Elasticsearch analyzes incoming document data and stores it for faster access. section 2.1.3
  • Choose analysis carefully because tokenization and normalization determine the terms queries can find. section 3.3
  • Reserve term-level queries on text for exact stored terms or enumerated values; avoid them for lengthy free text. section 9.1.2
Table 3.3 Tokenized words for the “Hello, World” document (view table figure)

Word

Frequency

Document ID

hello

1

1

world

1

1

Inspect the term, frequency, and document-ID entries produced from a single analyzed greeting field. section 3.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains tokenization, normalization, frequencies, and document associations, while deferring deeper analyzer internals.
Examples
Uses greeting documents and The Godfather title queries to contrast indexed tokens with query values.
What it assumes
Teaches the structure from simple documents and assumes no prior Elasticsearch knowledge.
What you can do after
Evaluate/understand why analyzed text searches match or fail against stored terms.
How it connects
Connects text analysis, analyzers, term queries, match queries, BKD trees, and relevance.

Also explained in

Compare how 5 books explain inverted index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.