📖 Scenario: You are setting up a search engine for a small online bookstore. You want to improve search results by creating a custom analyzer that lowercases text and removes common English stopwords.
🎯 Goal: Build an Elasticsearch index with a custom analyzer that uses the standard tokenizer, lowercases all text, and removes English stopwords.
📋 What You'll Learn
Create an index called bookstore with custom analyzer settings
Define a custom analyzer named my_custom_analyzer
Use the standard tokenizer in the analyzer
Add lowercase and stop filters to the analyzer
Set the stop filter to use English stopwords
Print the analyzer settings to verify
💡 Why This Matters
🌍 Real World
Custom analyzers help improve search quality by controlling how text is broken down and filtered before searching.
💼 Career
Knowing how to create and configure custom analyzers is important for roles involving search engine setup, data indexing, and improving user search experience.
Progress0 / 4 steps
1
Create the initial index settings with an empty analyzer
Create an index called bookstore with settings that include an empty analysis object.
Elasticsearch
Hint
Start by creating the settings key with an analysis object inside it.
2
Add a custom analyzer named my_custom_analyzer
Inside settings.analysis, add an analyzer object with a custom analyzer named my_custom_analyzer that uses the standard tokenizer and an empty filter list.
Elasticsearch
Hint
Define the analyzer key and inside it add my_custom_analyzer with the standard tokenizer and an empty filter list.
3
Add lowercase and stop filters to the analyzer
Modify the my_custom_analyzer to include the lowercase and stop filters in the filter list. Also, add a filter object in analysis defining the stop filter with stopwords set to _english_.
Elasticsearch
Hint
Add the lowercase and stop filters to the analyzer's filter list. Then define the stop filter with English stopwords in the filter object.
4
Print the analyzer settings to verify
Print the complete JSON settings for the bookstore index to verify the custom analyzer configuration.
Elasticsearch
Hint
Use print(settings) to display the full JSON settings object.
Practice
(1/5)
1. What is the main purpose of a custom analyzer in Elasticsearch?
easy
A. To control how text is broken down and processed before indexing
B. To store documents in the database
C. To create visualizations of search results
D. To manage user access permissions
Solution
Step 1: Understand the role of analyzers
Analyzers in Elasticsearch process text by breaking it into tokens and applying filters.
Step 2: Identify the purpose of custom analyzers
Custom analyzers let you define specific tokenizers and filters to control text processing.
Final Answer:
To control how text is broken down and processed before indexing -> Option A
Quick Check:
Custom analyzer = text processing control [OK]
Hint: Custom analyzers shape text before search indexing [OK]
Common Mistakes:
Confusing analyzers with data storage
Thinking analyzers create visualizations
Mixing analyzers with security settings
2. Which of the following is the correct way to define a custom analyzer with a lowercase filter in Elasticsearch settings?
easy
A. "analyzer": {"my_analyzer": {"type": "custom", "tokenizer": "standard", "filter": ["lowercase"]}}
B. "analyzer": {"my_analyzer": {"type": "standard", "tokenizer": "lowercase", "filter": ["custom"]}}
C. "analyzer": {"my_analyzer": {"type": "custom", "tokenizer": "lowercase", "filter": ["standard"]}}
D. "analyzer": {"my_analyzer": {"type": "filter", "tokenizer": "standard", "filter": ["lowercase"]}}
Solution
Step 1: Review custom analyzer syntax
A custom analyzer requires "type": "custom", a tokenizer, and filters as an array.
Step 2: Match correct tokenizer and filter
"standard" tokenizer and "lowercase" filter are valid and correctly placed in "analyzer": {"my_analyzer": {"type": "custom", "tokenizer": "standard", "filter": ["lowercase"]}}.
Filters must be provided as an array, e.g., ["lowercase"], not a string.
Step 2: Verify other parts
Tokenizer "standard" and analyzer type "custom" are correct; stopwords are optional.
Final Answer:
The filter should be an array, not a string -> Option C
Quick Check:
Filter must be array syntax [OK]
Hint: Filters always use array brackets [] in custom analyzers [OK]
Common Mistakes:
Using string instead of array for filters
Changing tokenizer to invalid types
Assuming stopwords are mandatory
5. You want to create a custom analyzer that removes English stopwords, lowercases text, and also applies a stemmer filter named english_stemmer. Which of the following is the correct way to define this analyzer in Elasticsearch settings?