📖 Scenario: You are setting up an Elasticsearch index for a book store. You want to clean up the text data by replacing certain characters before analysis.
🎯 Goal: Build an Elasticsearch index with a custom character filter that replaces the ampersand (&) with the word 'and' in book titles.
📋 What You'll Learn
Create an index called bookstore
Define a custom character filter named ampersand_filter that replaces '&' with 'and'
Add an analyzer called custom_analyzer that uses the ampersand_filter
Apply the custom_analyzer to the title field in the mapping
💡 Why This Matters
🌍 Real World
Cleaning and normalizing text data in search indexes to improve search accuracy and relevance.
💼 Career
Elasticsearch engineers and developers often create custom analyzers with character filters to handle special characters and improve search results.
Progress0 / 4 steps
1
Create the bookstore index with empty settings
Create an Elasticsearch index called bookstore with empty settings and mappings objects.
Elasticsearch
Hint
Use the PUT /bookstore API with an empty settings and mappings object.
2
Add a custom character filter ampersand_filter
In the settings of the bookstore index, add a char_filter named ampersand_filter that replaces the character '&' with the string 'and'.
Elasticsearch
Hint
Use the mapping type for the character filter and specify the mapping as &=>and.
3
Create a custom analyzer using the ampersand_filter
In the settings.analysis, add an analyzer named custom_analyzer that uses the ampersand_filter as a char_filter and the standard tokenizer.
Elasticsearch
Hint
Define a custom analyzer with the char_filter array including ampersand_filter and use the standard tokenizer.
4
Apply the custom_analyzer to the title field mapping
In the mappings.properties, define a title field of type text that uses the custom_analyzer analyzer.
Elasticsearch
Hint
Under mappings.properties, define the title field as text with the custom_analyzer.
Practice
(1/5)
1. What is the main purpose of a character_filter in Elasticsearch analyzers?
easy
A. To split text into tokens after analysis
B. To modify text before tokenizing by replacing or removing characters
C. To store documents in the index
D. To sort search results by relevance
Solution
Step 1: Understand character_filter role
Character filters act on raw text before tokenization to clean or replace characters.
Step 2: Differentiate from other components
Tokenizers split text into tokens, and other parts handle indexing or sorting, not character filters.
Final Answer:
To modify text before tokenizing by replacing or removing characters -> Option B
Quick Check:
Character filters = modify text pre-tokenizing [OK]
Hint: Character filters act before tokenizing text [OK]
Common Mistakes:
Confusing character filters with tokenizers
Thinking character filters sort or store data
Assuming character filters work after tokenizing
2. Which of the following is the correct syntax to define a mapping character filter in Elasticsearch?
easy
A. "char_filter": {"my_mapping": {"type": "mapping", "mappings": ["ph=>f", "qu=>kw"]}}
B. "char_filter": {"my_mapping": {"type": "pattern_replace", "pattern": "ph", "replacement": "f"}}
C. "char_filter": {"my_mapping": {"type": "mapping", "pattern": "ph", "replacement": "f"}}
D. "char_filter": {"my_mapping": {"type": "replace", "mappings": ["ph=>f"]}}
Solution
Step 1: Identify correct type and keys for mapping filter
The mapping filter uses "type": "mapping" and a "mappings" array with string replacements.
Step 2: Check syntax correctness
"char_filter": {"my_mapping": {"type": "mapping", "mappings": ["ph=>f", "qu=>kw"]}} correctly uses "type": "mapping" and "mappings" with array of strings like "ph=>f".
Final Answer:
"char_filter": {"my_mapping": {"type": "mapping", "mappings": ["ph=>f", "qu=>kw"]}} -> Option A
Quick Check:
Mapping filter syntax = type mapping + mappings array [OK]
Hint: Mapping filters use type 'mapping' and 'mappings' array [OK]
Common Mistakes:
Using pattern_replace keys for mapping filter
Wrong type name like 'replace' instead of 'mapping'
Using 'pattern' and 'replacement' keys in mapping filter
C. The replacement string 'ph=>f' is incorrect format
D. The key should be 'mappings' not 'mapping'
Solution
Step 1: Check correct key for mapping filter
The mapping filter requires the key 'mappings' (plural) for the array of replacements.
Step 2: Verify other parts
Type 'mapping' is valid, and the replacement string format "ph=>f" is correct.
Final Answer:
The key should be 'mappings' not 'mapping' -> Option D
Quick Check:
Key name must be 'mappings' for mapping filter [OK]
Hint: Use 'mappings' key (plural) for mapping filters [OK]
Common Mistakes:
Using 'mapping' instead of 'mappings'
Mistaking type name
Wrong replacement string format
5. You want to create a character filter that replaces all occurrences of 'cat' with 'dog' and removes digits from the text before tokenizing. Which configuration correctly combines these requirements?