liveBook · Manning

N-gram

As explained in Elasticsearch in Action, Second Edition

An n-gram is a contiguous sequence of a specified number of characters or words derived from text.

An n-gram is a contiguous sequence of a chosen number of characters or words. A sliding window produces overlapping sequences from the original text. section 7.7.2

Use the window size and position to distinguish related forms:

  • Character bi-grams from “coffee” include “co”, “of”, “ff”, “fe”, and “ee”. section 7.7.2
  • Character tri-grams from “coffee” include “cof”, “off”, “ffe”, and “fee”. section 7.7.2
  • Edge n-grams anchor every sequence at the beginning: “c”, “co”, “cof”, “coff”, “coffe”, and “coffee”. section 7.7.2
  • Word shingles form word sequences such as [“elasticsearch”, “in”] and [“in”, “action”]. section 4.6.6

Elasticsearch’s ngram tokenizer emits overlapping character sequences, while edge_ngram emits prefix sequences. Configure min_gram and max_gram to control their sizes. section 7.7.2

Typeahead for book titles

Mapping a title as search_as_you_type creates n-gram subfields automatically. A multi_match query with bool_prefix searches the main title and its _2gram and _3gram subfields to suggest titles as users type. section 4.6.6

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains character windows, edge anchoring, word shingles, tokenizer configuration, and analysis output without covering broader relevance tuning.
Examples
Uses “coffee” and “Bond” for character tokens, plus book titles for word shingles and typeahead search.
What you can do after
Apply it via Elasticsearch tokenizers and search_as_you_type mappings to inspect tokens and implement prefix-oriented search.
How it connects
Connects n-grams with tokenizers, edge n-grams, shingles, analyzers, search_as_you_type, and multi_match queries.

Also explained in

Compare how 4 books explain n-gram →

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.