Bird
Raised Fist0
Elasticsearchquery~20 mins

Character filters in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Character Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this character filter configuration?
Given the following Elasticsearch analyzer configuration using a mapping character filter, what will be the output tokens for the input text "foo-bar baz"?
Elasticsearch
{
  "analysis": {
    "char_filter": {
      "my_mapping": {
        "type": "mapping",
        "mappings": ["- => ""]"]
      }
    },
    "analyzer": {
      "my_analyzer": {
        "type": "custom",
        "char_filter": ["my_mapping"],
        "tokenizer": "whitespace"
      }
    }
  }
}
A["foobar", "baz"]
B["foo-bar", "baz"]
C["foo", "bar", "baz"]
D["foo", "-bar", "baz"]
Attempts:
2 left
💡 Hint
Think about what the mapping character filter does to the dash (-) character before tokenization.
🧠 Conceptual
intermediate
1:30remaining
Which character filter type removes HTML tags from input text?
In Elasticsearch, which character filter type is designed to remove HTML tags from the input text before tokenization?
Amapping
Bkeyword_marker
Chtml_strip
Dpattern_replace
Attempts:
2 left
💡 Hint
Think about the filter that cleans HTML content.
❓ Predict Output
advanced
2:00remaining
What error does this character filter configuration cause?
Consider this Elasticsearch character filter configuration snippet. What error will Elasticsearch raise when trying to create this analyzer?
Elasticsearch
{
  "analysis": {
    "char_filter": {
      "bad_filter": {
        "type": "mapping",
        "mappings": "- >"
      }
    },
    "analyzer": {
      "test_analyzer": {
        "type": "custom",
        "char_filter": ["bad_filter"],
        "tokenizer": "standard"
      }
    }
  }
}
AIllegalArgumentException because 'mappings' must be an array
BElasticsearchParseException due to invalid mapping syntax
CNo error, analyzer created successfully
DNullPointerException during analysis
Attempts:
2 left
💡 Hint
Check the syntax of the mapping string.
🚀 Application
advanced
1:30remaining
How many tokens are produced by this analyzer?
Given this analyzer configuration with a pattern_replace character filter that removes digits, how many tokens will be produced from the input "abc123 def456 ghi789"?
Elasticsearch
{
  "analysis": {
    "char_filter": {
      "remove_digits": {
        "type": "pattern_replace",
        "pattern": "\\d",
        "replacement": ""
      }
    },
    "analyzer": {
      "digit_remover": {
        "type": "custom",
        "char_filter": ["remove_digits"],
        "tokenizer": "whitespace"
      }
    }
  }
}
A6
B0
C1
D3
Attempts:
2 left
💡 Hint
Digits are removed before tokenization, but spaces remain.
🔧 Debug
expert
2:30remaining
Why does this analyzer produce unexpected tokens?
An Elasticsearch analyzer uses this character filter configuration but produces tokens with underscores instead of spaces. What is the cause?
Elasticsearch
{
  "analysis": {
    "char_filter": {
      "underscore_to_space": {
        "type": "mapping",
        "mappings": ["_ => \\u0020"]
      }
    },
    "analyzer": {
      "custom_analyzer": {
        "type": "custom",
        "char_filter": ["underscore_to_space"],
        "tokenizer": "whitespace"
      }
    }
  }
}
AThe mapping syntax requires double backslashes for unicode escapes
BThe mapping character filter does not support unicode escapes
CThe mapping uses an incorrect unicode escape sequence for space
DThe tokenizer is ignoring the character filter
Attempts:
2 left
💡 Hint
Check how JSON strings handle backslashes.

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

  1. Step 1: Understand character_filter role

    Character filters act on raw text before tokenization to clean or replace characters.
  2. Step 2: Differentiate from other components

    Tokenizers split text into tokens, and other parts handle indexing or sorting, not character filters.
  3. Final Answer:

    To modify text before tokenizing by replacing or removing characters -> Option B
  4. 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

  1. Step 1: Identify correct type and keys for mapping filter

    The mapping filter uses "type": "mapping" and a "mappings" array with string replacements.
  2. 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".
  3. Final Answer:

    "char_filter": {"my_mapping": {"type": "mapping", "mappings": ["ph=>f", "qu=>kw"]}} -> Option A
  4. 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
3. Given this character filter configuration:
{
  "char_filter": {
    "my_filter": {
      "type": "pattern_replace",
      "pattern": "\\d",
      "replacement": "#"
    }
  }
}

What will be the output text after applying my_filter to the input "abc123def"?
medium
A. "abc1#3def"
B. "abc123def"
C. "abc###def"
D. "abc#23def"

Solution

  1. Step 1: Understand pattern_replace with pattern '\\d'

    The pattern '\\d' matches any digit character (0-9). The replacement is '#'.
  2. Step 2: Apply replacement to input text

    All digits '1', '2', '3' in "abc123def" are replaced by '#', resulting in "abc###def".
  3. Final Answer:

    "abc###def" -> Option C
  4. Quick Check:

    Digits replaced by '#' = "abc###def" [OK]
Hint: Pattern '\d' matches all digits, replaced by '#' [OK]
Common Mistakes:
  • Replacing only first digit instead of all
  • Not escaping backslash in pattern
  • Confusing pattern_replace with mapping filter
4. Identify the error in this character filter definition:
{
  "char_filter": {
    "bad_filter": {
      "type": "mapping",
      "mapping": ["ph=>f"]
    }
  }
}
medium
A. Character filters cannot use arrays
B. The type 'mapping' is invalid
C. The replacement string 'ph=>f' is incorrect format
D. The key should be 'mappings' not 'mapping'

Solution

  1. Step 1: Check correct key for mapping filter

    The mapping filter requires the key 'mappings' (plural) for the array of replacements.
  2. Step 2: Verify other parts

    Type 'mapping' is valid, and the replacement string format "ph=>f" is correct.
  3. Final Answer:

    The key should be 'mappings' not 'mapping' -> Option D
  4. 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?
hard
A. { "char_filter": { "replace_cat": { "type": "mapping", "mappings": ["cat=>dog"] }, "remove_digits": { "type": "pattern_replace", "pattern": "[0-9]", "replacement": "" } } }
B. { "char_filter": { "replace_cat": { "type": "pattern_replace", "pattern": "cat", "replacement": "dog" }, "remove_digits": { "type": "mapping", "mapping": ["0=>", "1=>", "2=>", "3=>", "4=>", "5=>", "6=>", "7=>", "8=>", "9=>"] } } }
C. { "char_filter": { "replace_cat": { "type": "mapping", "mapping": ["cat=>dog"] }, "remove_digits": { "type": "pattern_replace", "pattern": "\\d", "replacement": "" } } }
D. { "char_filter": { "replace_cat": { "type": "pattern_replace", "pattern": "cat", "replacement": "dog" }, "remove_digits": { "type": "pattern_replace", "pattern": "d", "replacement": "" } } }

Solution

  1. Step 1: Check replacement of 'cat' with 'dog'

    Using a mapping filter with "cat=>dog" is correct and simpler for exact string replacement.
  2. Step 2: Check digit removal filter

    Removing digits with pattern_replace using pattern "[0-9]" and empty replacement is valid and concise.
  3. 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.
  4. Final Answer:

    { "char_filter": { "replace_cat": { "type": "mapping", "mappings": ["cat=>dog"] }, "remove_digits": { "type": "pattern_replace", "pattern": "[0-9]", "replacement": "" } } } -> Option A
  5. Quick Check:

    Mapping for 'cat', pattern_replace for digits removal [OK]
Hint: Use mapping for exact words, pattern_replace for patterns [OK]
Common Mistakes:
  • Using pattern_replace for simple word mapping unnecessarily
  • Using mapping filter to remove digits (inefficient)
  • Incorrect regex patterns for digits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard