Testing analyzers (_analyze API) in Elasticsearch - Time & Space Complexity
When testing analyzers with the _analyze API, we want to know how the processing time changes as the input text grows.
We ask: How does the analyzer's work increase when the text gets longer?
Analyze the time complexity of the following code snippet.
POST /_analyze
{
"analyzer": "standard",
"text": "The quick brown fox jumps over the lazy dog"
}
This code sends a text to Elasticsearch's _analyze API using the standard analyzer to break it into tokens.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The analyzer processes each character and splits the text into tokens.
- How many times: Once for each character and token in the input text.
As the input text gets longer, the analyzer spends more time processing each character and creating tokens.
| Input Size (n characters) | Approx. Operations |
|---|---|
| 10 | About 10 character checks and token splits |
| 100 | About 100 character checks and token splits |
| 1000 | About 1000 character checks and token splits |
Pattern observation: The work grows roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the analyzer's work grows linearly with the length of the input text.
[X] Wrong: "The analyzer processes tokens instantly, so input size doesn't affect time."
[OK] Correct: Each character and token must be checked and split, so longer text takes more time.
Understanding how text length affects analyzer performance helps you explain search speed and indexing behavior clearly.
"What if we changed the analyzer to a more complex one with multiple token filters? How would the time complexity change?"
Practice
/_analyze API in Elasticsearch?Solution
Step 1: Understand the role of
The/_analyze/_analyzeAPI shows how text is processed by an analyzer, including tokenization and filtering.Step 2: Compare with other API functions
Updating mappings, deleting documents, or creating indexes are done by other APIs, not/_analyze.Final Answer:
To see how input text is broken into tokens by an analyzer -> Option CQuick Check:
Analyze API = tokenizes text [OK]
/_analyze shows token breakdown [OK]- Confusing
/_analyzewith index creation APIs - Thinking it modifies data instead of analyzing text
- Assuming it deletes or updates documents
/_analyze API?Solution
Step 1: Check JSON syntax for
The body must be valid JSON with keys as strings and values properly quoted. { "analyzer": "standard", "text": "Hello World" } uses correct JSON syntax./_analyzerequest bodyStep 2: Identify errors in other options
{ "analyzer": "standard" "text": "Hello World" } misses a comma between keys. { "text": "Hello World", "analyzer": standard } lacks quotes around "standard". { analyzer: "standard", text: "Hello World" } uses unquoted keys, invalid in JSON.Final Answer:
{ "analyzer": "standard", "text": "Hello World" } -> Option AQuick Check:
Valid JSON with quoted keys and values = { "analyzer": "standard", "text": "Hello World" } [OK]
- Missing commas between JSON fields
- Not quoting string values
- Using unquoted keys in JSON
/_analyze API with the standard analyzer:{ "analyzer": "standard", "text": "Quick brown fox" }What is the expected list of tokens returned?
Solution
Step 1: Understand standard analyzer behavior
The standard analyzer lowercases text and splits on whitespace and punctuation, so "Quick brown fox" becomes tokens "quick", "brown", "fox".Step 2: Check options for token case and splitting
["quick", "brown", "fox"] shows all lowercase separate tokens. ["Quick", "brown", "fox"] keeps uppercase 'Quick' which standard analyzer lowercases. Options that combine words are incorrect.Final Answer:
["quick", "brown", "fox"] -> Option BQuick Check:
Standard analyzer lowercases and splits words [OK]
- Expecting tokens to keep original case
- Thinking tokens combine multiple words
- Confusing analyzer output with raw text
/_analyze request:{ "analyzer": "whitespace", "text": "Hello, World!" }But the output tokens include punctuation marks like "Hello," instead of just "Hello". What is the likely cause?
Solution
Step 1: Recall whitespace analyzer behavior
The whitespace analyzer splits text only on spaces and does not remove punctuation, so tokens keep punctuation marks.Step 2: Eliminate other options
The standard analyzer was used instead of whitespace is incorrect because the standard analyzer removes punctuation. The text was not provided in the request body is invalid because text is provided. The analyzer automatically removes punctuation by default is false; whitespace analyzer does not remove punctuation.Final Answer:
Using the whitespace analyzer which only splits on spaces, not removing punctuation -> Option AQuick Check:
Whitespace analyzer keeps punctuation in tokens [OK]
- Expecting punctuation removal with whitespace analyzer
- Confusing analyzer names
- Omitting text in request body
/_analyze request body will correctly show tokens for the text "The quick brown fox and the lazy dog"?Solution
Step 1: Use the custom analyzer name in the
To test a custom analyzer, specify its name under the "analyzer" key with the text to analyze. { "analyzer": "my_custom_analyzer", "text": "The quick brown fox and the lazy dog" } does this correctly./_analyzerequestStep 2: Understand why other options are incorrect
{ "analyzer": "my_custom_analyzer", "text": "The quick brown fox and the lazy dog", "stopwords": ["the", "and"] } incorrectly tries to add stopwords in the request body, which is not supported here. { "tokenizer": "standard", "filter": ["lowercase", "stop"], "text": "The quick brown fox and the lazy dog" } tries to define tokenizer and filters inline, which is invalid for/_analyzewithout full analyzer definition. { "analyzer": "standard", "filter": ["lowercase", "stop"], "text": "The quick brown fox and the lazy dog" } uses standard analyzer, not the custom one.Final Answer:
{ "analyzer": "my_custom_analyzer", "text": "The quick brown fox and the lazy dog" } -> Option DQuick Check:
Use custom analyzer name directly in/_analyze[OK]
/_analyze [OK]- Trying to redefine filters in
/_analyzerequest - Using standard analyzer instead of custom
- Adding unsupported keys like stopwords in request body
Ready to prove it?
Pass the test and this pattern moves unknown
