Highlighting matched text in Elasticsearch - Time & Space Complexity
When we ask about time complexity for highlighting matched text in Elasticsearch, we want to know how the work grows as the data or query size grows.
Specifically, how does Elasticsearch spend more time when it finds and highlights matches in documents?
Analyze the time complexity of the following Elasticsearch query with highlighting.
GET /my_index/_search
{
"query": {
"match": { "content": "search term" }
},
"highlight": {
"fields": { "content": {} }
}
}
This query searches for documents matching "search term" in the content field and highlights the matched parts in the results.
Look at what repeats as the query runs:
- Primary operation: Scanning each document's content to find matches and then marking matched text for highlighting.
- How many times: Once for each document that matches the query and for each highlighted field in that document.
As the number of matching documents grows, Elasticsearch must highlight more text snippets.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Highlighting in 10 documents |
| 100 | Highlighting in 100 documents (about 10 times more work) |
| 1000 | Highlighting in 1000 documents (about 100 times more work than 10) |
Pattern observation: The work grows roughly in direct proportion to the number of matched documents because each document's text must be scanned and highlighted.
Time Complexity: O(n)
This means the time to highlight matched text grows linearly with the number of documents matched by the query.
[X] Wrong: "Highlighting happens instantly and does not add time as more documents match."
[OK] Correct: Highlighting requires scanning matched text in each document, so more matches mean more work and more time.
Understanding how highlighting scales helps you explain performance trade-offs when searching and displaying results, a useful skill in real projects.
"What if we added highlighting on multiple fields instead of just one? How would the time complexity change?"
Practice
highlight section in an Elasticsearch query?Solution
Step 1: Understand the role of highlighting
Highlighting is used to emphasize matched words in search results so users can see why a document matched.Step 2: Compare with other query parts
Sorting, filtering, and updating are different functions not related to highlighting matched text.Final Answer:
To show matched words clearly in search results -> Option DQuick Check:
Highlight = show matched words [OK]
- Confusing highlight with sorting or filtering
- Thinking highlight updates documents
- Assuming highlight changes search relevance
content in an Elasticsearch query?Solution
Step 1: Recall correct highlight syntax
The correct syntax uses "highlight" with "fields" as an object mapping field names to options, like {"content": {}}.Step 2: Check each option
"highlight": {"fields": {"content": {}}} matches the correct structure. Options B, C, and D use incorrect keys or data types.Final Answer:
"highlight": {"fields": {"content": {}}} -> Option AQuick Check:
Highlight fields syntax = "highlight": {"fields": {"content": {}}} [OK]
- Using "field" instead of "fields"
- Putting fields inside an array instead of object
- Misplacing keys inside highlight
highlight section return for a match in the title field?
{
"query": {"match": {"title": "quick fox"}},
"highlight": {"fields": {"title": {}}}
}Solution
Step 1: Understand default highlight behavior
When highlight is enabled with empty options, Elasticsearch wraps matched words with default tags like <em> and </em>.Step 2: Analyze the query
The query matches both "quick" and "fox" in the title, so both words will be highlighted.Final Answer:
The matched words "quick" and "fox" wrapped in default highlight tags -> Option CQuick Check:
Default highlight tags wrap matched words [OK]
- Thinking highlight needs custom tags to work
- Assuming only first matched word is highlighted
- Believing missing tags cause errors
{
"query": {"match": {"description": "fast car"}},
"highlight": {"fields": ["description"]}
}Solution
Step 1: Check highlight fields syntax
The "fields" key must be an object mapping field names to options, not an array.Step 2: Verify other parts
"match" query is valid, "highlight" is correctly placed, and any field can be highlighted if indexed.Final Answer:
"fields" should be an object, not an array -> Option BQuick Check:
Highlight fields = object, not array [OK]
- Using array instead of object for fields
- Placing highlight inside query incorrectly
- Assuming some fields can't be highlighted
title and content fields with custom tags <strong> and </strong>. Which is the correct highlight section to achieve this?Solution
Step 1: Understand how to set custom tags
Custom tags are set globally using "pre_tags" and "post_tags" arrays outside the fields object.Step 2: Check each option's structure
"highlight": {"fields": {"title": {}, "content": {}}, "pre_tags": [""], "post_tags": [""]} correctly sets "fields" as an object and uses "pre_tags" and "post_tags" arrays. Options B, C, and D misuse keys or data types.Final Answer:
"highlight": {"fields": {"title": {}, "content": {}}, "pre_tags": [""], "post_tags": [""]} -> Option AQuick Check:
Custom tags use pre_tags/post_tags arrays [OK]
- Using arrays for fields instead of objects
- Setting tags inside each field incorrectly
- Using single strings instead of arrays for tags
Ready to prove it?
Pass the test and this pattern moves unknown
