How the Edge N-gram Tokenizer Works
A practical guide to the edge n-gram tokenizer: how it turns words into leading-edge prefixes, which settings control the output, and how to check the result.
An edge n-gram tokenizer breaks text into prefixes: short pieces that always start at the beginning of a word. Search engines use it to make partial input such as "sea" match a stored word such as "search". This makes it a common building block for search-as-you-type and autocomplete. This guide explains what the tokenizer does to your text, which settings shape its output, and how to check that it behaves as you expect.
What the tokenizer does to your text
A tokenizer takes a stream of text and splits it into tokens, which are the units a search index stores and compares. An ordinary word tokenizer turns "quick fox" into two tokens, "quick" and "fox". An edge n-gram tokenizer goes one step further. It first divides the text into words, then emits a series of growing prefixes for each word, anchored at the word's first character. The original text is not changed. Only the index receives these extra tokens, and that is what lets a fragment typed by a user match a full word.
Key terms
- N-gram: a contiguous sequence of n characters taken from a word.
- Edge n-gram: an n-gram that must start at the leading edge, meaning the first character of the word. The tokenizer produces prefixes only, never fragments from the middle or end.
- Minimum gram length (often called min_gram): the length of the shortest prefix produced.
- Maximum gram length (often called max_gram): the length of the longest prefix produced. Characters beyond this length do not appear in any token.
- Token characters (often called token_chars): the character classes, such as letters or digits, that count as part of a word. Any character outside these classes acts as a word boundary.
A worked example
The table below applies the prefix rule by hand to the single word "search" under different length limits. It is an illustrative example derived from the definition, not output captured from a specific product.
| Input | Min length | Max length | Resulting tokens |
| search | 1 | 3 | s, se, sea |
| search | 2 | 5 | se, sea, sear, searc |
| search | 3 | 10 | sea, sear, searc, search |
| so | 3 | 10 | (no tokens: the word is shorter than the minimum length) |
Two effects in the table matter in practice. When the maximum length is shorter than the word, the full word never becomes a token, so in the second row "search" itself is not indexed. When a word is shorter than the minimum length, it produces no tokens at all and disappears from that index field.
How word boundaries are decided
The setting for token characters decides where words begin. In Elasticsearch's edge_ngram tokenizer, the defaults are a minimum length of 1, a maximum length of 2, and an empty list of token characters. An empty list means every character, spaces included, is kept as part of a single unit. Under those defaults, the input "Quick Fox" is treated as one string, and only its first prefixes are produced: "Q" and "Qu". The word "Fox" never gets its own prefixes. To get per-word prefixes, specify the character classes that form words, such as letters and digits. Spaces and punctuation then act as separators.
Other engines and libraries may use different setting names or defaults. Check the documentation for the version you use instead of assuming these values carry over.
Case, spaces and special characters
The tokenizer does not change capitalization. "Quick" yields "Q", "Qu", "Qui" and so on, with the capital letter kept. For case-insensitive matching, add a lowercase step after the tokenizer and use the same normalization when searching. The tokenizer also does not add or remove characters inside a word, so a hyphen, apostrophe or underscore is either part of the word or a boundary, depending on the configured token characters. For example, "e-mail" with letters as the only token characters becomes two words, "e" and "mail", and each gets its own prefixes. Invisible characters such as a non-breaking space (U+00A0) or a zero-width space (U+200B) can also act as boundaries or as word content, depending on how the engine classifies them. If your source text may contain them, test those cases directly.
Step by step: configure it and verify the output
The following is an example Elasticsearch configuration for autocomplete. It indexes prefixes of 2 to 10 characters from letters and digits and lowercases them. At search time it applies only lowercasing, so the user's query is not itself split into prefixes. Treat it as a template to adapt, not as a tested setup for your cluster.
PUT my-index
{
"settings": {
"analysis": {
"analyzer": {
"autocomplete": {
"tokenizer": "autocomplete_tokenizer",
"filter": ["lowercase"]
},
"autocomplete_search": {
"tokenizer": "lowercase"
}
},
"tokenizer": {
"autocomplete_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": ["letter", "digit"]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "autocomplete_search"
}
}
}
}
To verify the result, run sample text through the analyzer and inspect the tokens it returns:
POST my-index/_analyze
{
"analyzer": "autocomplete",
"text": "2 Quick Foxes"
}
Applying the rules by hand, you should expect: qu, qui, quic, quick, fo, fox, foxe, foxes. The digit "2" is a single character, shorter than the minimum length of 2, so it produces no token. Compare this expectation with the actual response. If the lists differ, review the token characters, the length limits and the filter order.
- 1. Pick representative inputs, including short words, long words, mixed case, punctuation and any non-English text you index.
- 2. Write down the tokens you expect from the prefix rule and your settings.
- 3. Run each input through the index-time analyzer and compare the result with your expectation.
- 4. Run the same inputs through the search-time analyzer to confirm that queries are normalized but not split into prefixes.
- 5. Run a few real queries and confirm that partial words match and unrelated words do not.
Why index time and search time are handled differently
Prefixes are normally generated only when documents are indexed. If a query were also split into edge n-grams, the query "fox" would become "fo" and "fox", and "fo" would match unrelated words such as "form" or "focus". Using a plain analyzer at search time keeps the query whole, so it matches only stored prefixes that equal it.
A related caveat concerns the maximum length. If a user types a word longer than the maximum, for example a 12-character term against a limit of 10, the full query term exists in no stored token and finds no match. Either raise the maximum length or shorten query terms at search time to the same limit. In Elasticsearch, the truncate token filter is one way to do this.
Trade-offs to keep in mind
- Index size: every word produces several tokens instead of one, so a wider range between the minimum and maximum lengths stores more data.
- Noise from short prefixes: a minimum length of 1 makes single letters match huge numbers of documents. A higher minimum reduces noise but stops very short input from matching.
- Prefix-only matching: the tokenizer finds "sea" in "search" but never "arch". Matching inside words needs a full n-gram tokenizer, which produces fragments from every position and grows the index further.
- Script-specific behavior: for scripts without spaces between words, or for text with combining marks and emoji, how the engine counts characters and finds word boundaries decides the output. Test these cases instead of assuming they split cleanly.
Gaps in this guide
No product-specific measurements were available for this article. It makes no claims about indexing speed, storage overhead or relevance quality, and the token lists above were derived by hand from the prefix rule rather than captured from a running system. The Elasticsearch defaults and setting names describe that engine's documented edge_ngram tokenizer. Other search engines and libraries may differ, so confirm the behavior of your own version with the analyze-and-compare steps above.