Build a Custom Elasticsearch Analyzer with Tokenizer and Filters
📖 Scenario: You are setting up a search engine for a small online bookstore. You want to create a custom analyzer in Elasticsearch that breaks text into words and processes them to improve search results.
🎯 Goal: Create a custom Elasticsearch analyzer using a tokenizer and filters. You will define the tokenizer and filters, then combine them into an analyzer, and finally test the analyzer to see the processed tokens.
📋 What You'll Learn
Create an index with a custom analyzer named my_custom_analyzer
Use the standard tokenizer in the analyzer
Add a lowercase filter named lowercase
Add a stop filter named stop with stop words the, and, is
Test the analyzer with the text "The quick Brown fox jumps over the lazy Dog and is happy"
💡 Why This Matters
🌍 Real World
Custom analyzers help improve search quality by controlling how text is broken down and filtered before searching.
💼 Career
Understanding analyzers is key for roles in search engineering, backend development, and data engineering working with Elasticsearch.
Progress0 / 4 steps
1
Create the index with a custom analyzer using the standard tokenizer
Write the JSON to create an Elasticsearch index named books with a custom analyzer called my_custom_analyzer that uses the standard tokenizer.
Elasticsearch
Hint
Define the my_custom_analyzer inside analysis.analyzer with the standard tokenizer.
2
Add lowercase and stop filters to the analyzer
Add the filters lowercase and stop to the my_custom_analyzer. Define the stop filter with stop words the, and, and is inside analysis.filter.
Elasticsearch
Hint
Define the stop inside analysis.filter and add both lowercase and stop to the analyzer's filter list.
3
Test the custom analyzer with sample text
Write the JSON query to analyze the text "The quick Brown fox jumps over the lazy Dog and is happy" using the my_custom_analyzer on the books index.
Elasticsearch
Hint
Use the _analyze API with the analyzer set to my_custom_analyzer and provide the sample text.
4
Display the tokens produced by the analyzer
Print the tokens returned by the analyzer for the text "The quick Brown fox jumps over the lazy Dog and is happy". The output should list tokens without the stop words and all in lowercase.
Elasticsearch
Hint
The tokens should be lowercase and exclude the, and, is.
Practice
(1/5)
1. What is the main role of a tokenizer in an Elasticsearch analyzer?
easy
A. To remove stop words from the text
B. To split text into smaller pieces called tokens
C. To convert tokens to lowercase
D. To index the tokens for searching
Solution
Step 1: Understand the tokenizer's function
A tokenizer breaks the input text into smaller parts called tokens, which are the basic units for searching.
Step 2: Differentiate tokenizer from filters
Filters modify tokens after tokenization, such as lowercasing or removing stop words, but tokenizers do the initial splitting.
Final Answer:
To split text into smaller pieces called tokens -> Option B
Quick Check:
Tokenizer = splits text [OK]
Hint: Tokenizer always splits text into tokens first [OK]
Common Mistakes:
Confusing tokenizer with filters like lowercase or stop word removal
Thinking tokenizer indexes tokens
Assuming tokenizer modifies tokens after splitting
2. Which of the following is the correct syntax to define a standard tokenizer in an Elasticsearch analyzer settings?
easy
A. "tokenizer": standard
B. "tokenizer": ["standard"]
C. "tokenizer": { "type": "standard" }
D. "tokenizer": { "name": "standard" }
Solution
Step 1: Recall Elasticsearch tokenizer syntax
Tokenizers are defined as objects with a type field specifying the tokenizer type.
Step 2: Compare options
"tokenizer": { "type": "standard" } correctly uses an object with type key, while others use incorrect formats like strings or arrays.
Final Answer:
"tokenizer": { "type": "standard" } -> Option C
Quick Check:
Tokenizer syntax uses type key in object [OK]
Hint: Tokenizer must be an object with type key [OK]
Common Mistakes:
Using string directly instead of object with type
Using array syntax for tokenizer
Using incorrect key names like name instead of type
What will be the output tokens for the input text: "The Quick Brown Fox"?
medium
A. ["quick", "brown", "fox"]
B. ["The", "Quick", "Brown", "Fox"]
C. ["quick", "brown"]
D. ["the", "quick", "brown", "fox"]
Solution
Step 1: Apply the whitespace tokenizer
The whitespace tokenizer splits text by spaces, so tokens are ["The", "Quick", "Brown", "Fox"].
Step 2: Apply lowercase and stop filters
The lowercase filter converts all tokens to lowercase: ["the", "quick", "brown", "fox"]. The stop filter removes common stop words like "the", leaving ["quick", "brown", "fox"].
Final Answer:
["quick", "brown", "fox"] -> Option A
Quick Check:
Lowercase + stop filter removes "the" [OK]
Hint: Stop filter removes common words after lowercase [OK]
Common Mistakes:
Ignoring stop word removal
Not applying lowercase filter before stop filter
Assuming tokenizer changes case
4. Identify the error in this analyzer filter configuration:
B. The lowercase filter must be a string, not an object
C. The stop filter syntax is incorrect
D. The filter list contains an invalid filter type
Solution
Step 1: Review each filter type
Lowercase and stop filters are valid and correctly defined. The stop filter uses a valid stopwords parameter.
Step 2: Identify invalid filter
The filter with type "unknown_filter" is not a valid Elasticsearch filter type and will cause an error.
Final Answer:
The filter list contains an invalid filter type -> Option D
Quick Check:
Invalid filter type causes error [OK]
Hint: Check filter types are valid Elasticsearch filters [OK]
Common Mistakes:
Assuming all filter types are valid
Thinking lowercase filter must be string
Ignoring unknown filter errors
5. You want to create a custom analyzer that splits text on whitespace, lowercases tokens, and removes English stop words. Which of the following analyzer settings is correct?
The tokenizer must be whitespace type, correctly defined as an object with type "whitespace".
Step 2: Confirm filter order and definition
Filters should lowercase first, then remove stop words. { "tokenizer": { "type": "whitespace" }, "filter": ["lowercase", { "type": "stop", "stopwords": "_english_" }] } correctly defines lowercase as string and stop filter with explicit stopwords parameter.
Step 3: Check other options
{ "tokenizer": "whitespace", "filter": ["lowercase", "stop"] } uses tokenizer as string which is allowed but less explicit; { "tokenizer": "standard", "filter": ["lowercase", "stop"] } uses standard tokenizer which is incorrect; { "tokenizer": { "type": "whitespace" }, "filter": ["stop", "lowercase"] } reverses filter order which may affect results.