Character filters in Elasticsearch - Time & Space Complexity
When using character filters in Elasticsearch, it's important to know how the processing time changes as the input text grows.
We want to understand how the time to apply character filters scales with the size of the text.
Analyze the time complexity of the following character filter configuration and usage.
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"my_mapping": {
"type": "mapping",
"mappings": ["ph=>f", "qu=>q"]
}
},
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": ["my_mapping"]
}
}
}
}
}
This code sets up a character filter that replaces certain letter pairs before tokenizing the text.
Look at what happens when the filter runs on input text.
- Primary operation: Scanning the entire input text to find and replace matching character sequences.
- How many times: Once per character in the input, checking if a mapping applies.
As the input text gets longer, the filter must check more characters for replacements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the length of the input text.
Time Complexity: O(n)
This means the time to apply character filters grows linearly with the input size.
[X] Wrong: "Character filters run in constant time no matter how long the text is."
[OK] Correct: The filter must check each character or character pair to apply replacements, so longer text means more work.
Understanding how text processing scales helps you explain performance in search systems and text analysis tasks.
What if the character filter used multiple mappings with overlapping sequences? How would that affect the time complexity?
Practice
character_filter in Elasticsearch analyzers?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 BQuick Check:
Character filters = modify text pre-tokenizing [OK]
- Confusing character filters with tokenizers
- Thinking character filters sort or store data
- Assuming character filters work after tokenizing
mapping character filter in Elasticsearch?Solution
Step 1: Identify correct type and keys for mapping filter
Themappingfilter 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 AQuick Check:
Mapping filter syntax = type mapping + mappings array [OK]
- Using pattern_replace keys for mapping filter
- Wrong type name like 'replace' instead of 'mapping'
- Using 'pattern' and 'replacement' keys in mapping filter
{
"char_filter": {
"my_filter": {
"type": "pattern_replace",
"pattern": "\\d",
"replacement": "#"
}
}
}What will be the output text after applying
my_filter to the input "abc123def"?Solution
Step 1: Understand pattern_replace with pattern '\\d'
The pattern '\\d' matches any digit character (0-9). The replacement is '#'.Step 2: Apply replacement to input text
All digits '1', '2', '3' in "abc123def" are replaced by '#', resulting in "abc###def".Final Answer:
"abc###def" -> Option CQuick Check:
Digits replaced by '#' = "abc###def" [OK]
- Replacing only first digit instead of all
- Not escaping backslash in pattern
- Confusing pattern_replace with mapping filter
{
"char_filter": {
"bad_filter": {
"type": "mapping",
"mapping": ["ph=>f"]
}
}
}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 DQuick Check:
Key name must be 'mappings' for mapping filter [OK]
- Using 'mapping' instead of 'mappings'
- Mistaking type name
- Wrong replacement string format
Solution
Step 1: Check replacement of 'cat' with 'dog'
Using a mapping filter with "cat=>dog" is correct and simpler for exact string replacement.Step 2: Check digit removal filter
Removing digits with pattern_replace using pattern "[0-9]" and empty replacement is valid and concise.Step 3: Verify combined configuration
{ "char_filter": { "replace_cat": { "type": "mapping", "mappings": ["cat=>dog"] }, "remove_digits": { "type": "pattern_replace", "pattern": "[0-9]", "replacement": "" } } } correctly combines a mapping filter for 'cat' and a pattern_replace filter for digits removal.Final Answer:
{ "char_filter": { "replace_cat": { "type": "mapping", "mappings": ["cat=>dog"] }, "remove_digits": { "type": "pattern_replace", "pattern": "[0-9]", "replacement": "" } } } -> Option AQuick Check:
Mapping for 'cat', pattern_replace for digits removal [OK]
- Using pattern_replace for simple word mapping unnecessarily
- Using mapping filter to remove digits (inefficient)
- Incorrect regex patterns for digits
Ready to prove it?
Pass the test and this pattern moves unknown
