Tokenizers break text into smaller pieces called tokens. This helps search engines understand and find words better.
Tokenizers (standard, whitespace, pattern) in Elasticsearch
PUT /my_index
{
"settings": {
"analysis": {
"tokenizer": {
"my_standard_tokenizer": {
"type": "standard"
},
"my_whitespace_tokenizer": {
"type": "whitespace"
},
"my_pattern_tokenizer": {
"type": "pattern",
"pattern": ","
}
}
}
}
}The standard tokenizer splits text by words and removes punctuation.
The whitespace tokenizer splits text only by spaces, keeping punctuation as part of tokens.
The pattern tokenizer splits text using a regular expression pattern you provide.
PUT /test_index
{
"settings": {
"analysis": {
"tokenizer": {
"my_standard": {
"type": "standard"
}
}
}
}
}PUT /test_index
{
"settings": {
"analysis": {
"tokenizer": {
"my_whitespace": {
"type": "whitespace"
}
}
}
}
}PUT /test_index
{
"settings": {
"analysis": {
"tokenizer": {
"my_pattern": {
"type": "pattern",
"pattern": ","
}
}
}
}
}This example shows how each tokenizer breaks the text differently.
POST /_analyze
{
"tokenizer": "standard",
"text": "Hello, world! Welcome to Elasticsearch."
}
POST /_analyze
{
"tokenizer": "whitespace",
"text": "Hello, world! Welcome to Elasticsearch."
}
POST /_analyze
{
"tokenizer": {"type": "pattern", "pattern": ","},
"text": "apple,banana,orange"
}
The standard tokenizer is the default and works well for most text.
The whitespace tokenizer is useful when punctuation should stay with words.
The pattern tokenizer is powerful for custom splitting but needs a correct regex pattern.
Tokenizers split text into tokens for searching.
standard removes punctuation, whitespace splits by spaces only, pattern splits by your pattern.
Choose the tokenizer based on how you want to break your text.
Practice
Solution
Step 1: Understand whitespace tokenizer behavior
The whitespace tokenizer splits text only at spaces and keeps punctuation inside tokens.Step 2: Compare with other tokenizers
The standard tokenizer removes punctuation, and the pattern tokenizer splits by regex patterns, so they behave differently.Final Answer:
whitespace tokenizer -> Option BQuick Check:
Spaces split tokens only = whitespace tokenizer [OK]
- Confusing standard tokenizer with whitespace tokenizer
- Thinking pattern tokenizer splits only on spaces
- Assuming keyword tokenizer splits text
Solution
Step 1: Identify correct JSON string escaping
In JSON, double quotes inside strings must be escaped with a backslash, so the comma pattern must be inside escaped quotes.Step 2: Check each option for proper escaping
{ "tokenizer": "pattern", "pattern": "," } correctly shows the pattern as a string with comma inside quotes.Final Answer:
{ "tokenizer": "pattern", "pattern": "," } -> Option DQuick Check:
Proper JSON escaping for pattern string = { "tokenizer": "pattern", "pattern": "," } [OK]
- Not escaping quotes inside JSON strings
- Using single quotes instead of double quotes
- Missing commas or braces in JSON
"Hello, world! Welcome to Elasticsearch.", what tokens does the standard tokenizer produce?Solution
Step 1: Understand standard tokenizer behavior
The standard tokenizer splits text by word boundaries and removes punctuation from tokens.Step 2: Apply tokenizer to given text
It splits "Hello," into "Hello", "world!" into "world", and removes punctuation marks like commas and exclamation points.Final Answer:
["Hello", "world", "Welcome", "to", "Elasticsearch"] -> Option CQuick Check:
Standard tokenizer removes punctuation = ["Hello", "world", "Welcome", "to", "Elasticsearch"] [OK]
- Including punctuation in tokens with standard tokenizer
- Confusing output with whitespace tokenizer
- Assuming pattern tokenizer output without pattern
\W+ but it does not split the text as expected. What is the likely issue?Solution
Step 1: Check pattern escaping in JSON
Regex patterns with backslashes must be double escaped in JSON strings, so \W+ should be written as \\W+.Step 2: Understand tokenizer requirements
If pattern is not escaped properly, the tokenizer won't interpret it as intended, causing wrong splits.Final Answer:
The pattern is not properly escaped in JSON -> Option AQuick Check:
Regex backslash must be double escaped in JSON [OK]
- Using single backslash in JSON regex pattern
- Confusing tokenizer types
- Assuming pattern tokenizer needs enabling
Solution
Step 1: Identify tokenizer that splits on specific characters
The pattern tokenizer allows splitting text by a custom regex pattern, so you can specify commas and semicolons.Step 2: Confirm pattern matches commas and semicolons only
The pattern[,;]matches commas or semicolons, splitting tokens at those characters but keeping spaces and other punctuation inside tokens.Final Answer:
Use a pattern tokenizer with pattern[,;]-> Option AQuick Check:
Pattern tokenizer with custom split chars = Use a pattern tokenizer with pattern[,;][OK]
- Using whitespace tokenizer which splits on spaces
- Using standard tokenizer which removes punctuation
- Using wrong regex pattern for splitting
Ready to prove it?
Pass the test and this pattern moves unknown
