Elasticsearch Analyzers and Tokenizers Explained
Fast recall
Quick revision
Core answer
Whether you understand how analyzers affect what gets stored in the inverted index and why the same analyzer must be applied at both index time and query time.
Using different analyzers at index time and query time, causing queries to generate terms that do not match the stored indexed terms.
Explain the test endpoint -- POST /_analyze -- for debugging analyzer behavior, and describe use cases for custom analyzers such as autocomplete (edge_ngram tokenizer), case-insensitive exact match (keyword + lowercase filter), and synonym expansion.
Interview-ready guide
Interview answer
An analyzer is the pipeline that converts raw text into the indexed terms stored in the inverted index. The same analyzer runs at index time and query time — if they differ, the indexed tokens don't match the query tokens and searches return no results.
The three stages of analysis
- Character filter: pre-processes the raw text before tokenization — strips HTML tags, replaces special characters; optional stage, often omitted
- Tokenizer: splits text into tokens —
standardtokenizer splits on whitespace and punctuation, lowercases;edge_ngramtokenizer generates prefix tokens for autocomplete - Token filter: transforms the token stream — lowercase filter normalizes case; stop filter removes "the", "a", "is"; stemmer reduces "running" to "run" so all forms match
Built-in analyzers
standardis the default — splits on whitespace and punctuation, lowercases; good for general English text; doesn't stemenglishadds stemming and stop-word removal on top of standard — "running", "runs", "ran" all match the stemmed token "run"; recommended for English content where morphological matching is needed
Custom analyzers for autocomplete
- Edge n-gram tokenizer generates prefix tokens — indexing "spring" produces ["s", "sp", "spr", "spri", "sprin", "spring"]; which means
match: { name.autocomplete: "spri" }matches "spring" because "spri" is one of its indexed tokens - Define a custom analyzer in the index settings and apply it to a multi-field mapping:
name.keywordfor exact match,name.textfor full-text,name.autocompletefor prefix search
Testing analyzers
POST /_analyze { "analyzer": "english", "text": "Running Spring Boot applications" }shows the exact tokens produced — the most direct way to debug why a query does or doesn't match
Elasticsearch Analyzers and Tokenizers Explained
Direct Answer
An analyzer is a pipeline that transforms raw text into indexed terms. It consists of a character filter (pre-processing), a tokenizer (splits text into tokens), and token filters (post-processing: lowercase, stop words, stemming, synonyms). The tokenizer is the core component that determines where term boundaries are. Analyzers are applied both at index time (building the inverted index) and query time (parsing the search query), and both must use the same analyzer for consistent matching.
Code Example
// Built-in analyzer comparison
POST /_analyze
{
"analyzer": "standard",
"text": "Spring Boot Makes Java Easy!"
}
// Tokens: [spring, boot, makes, java, easy]
// (lowercased, punctuation removed, splits on whitespace)
POST /_analyze
{
"analyzer": "english",
"text": "Running faster than before"
}
// Tokens: [run, faster, befor]
// (stemming applied: running->run, before->befor)
// Custom analyzer for autocomplete (edge_ngram)
PUT /products
{
"settings": {
"analysis": {
"analyzer": {
"autocomplete_index": {
"tokenizer": "autocomplete_tokenizer",
"filter": ["lowercase"]
},
"autocomplete_query": {
"tokenizer": "standard",
"filter": ["lowercase"]
}
},
"tokenizer": {
"autocomplete_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20,
"token_chars": ["letter", "digit"]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "autocomplete_index",
"search_analyzer": "autocomplete_query"
}
}
}
}
// Index time: 'boot' -> [bo, boo, boot]
// Query time: 'boo' -> [boo] -> matches documents with 'boot'
// search_analyzer overrides analyzer for queries only
Common Mistakes & Pitfalls
Using different analyzers at index time and query time, causing queries to generate terms that do not match the stored indexed terms.
Check your understanding
Answer these without looking back at the article.
- How does the edge_ngram tokenizer enable autocomplete in Elasticsearch?
- What is the difference between stemming and lemmatization for search?
- How do you add synonym support to an Elasticsearch analyzer?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.