0
0
Elasticsearchquery~10 mins

Highlighting matched text in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Highlighting matched text
Send Search Query with Highlight
Elasticsearch Matches Documents
Highlight Matched Text in Fields
Return Results with Highlighted Snippets
Client Displays Highlighted Text
The search query is sent with highlight instructions, Elasticsearch finds matches, highlights matched text, and returns results with highlighted snippets.
Execution Sample
Elasticsearch
{
  "query": {
    "match": {"content": "apple"}
  },
  "highlight": {
    "fields": {"content": {}}
  }
}
This query searches for 'apple' in the 'content' field and highlights matched text in the results.
Execution Table
StepActionQuery PartResult/Effect
1Send search query{"match":{"content":"apple"},"highlight":{"fields":{"content":{}}}}Query received by Elasticsearch
2Match documentsmatch content: appleDocuments containing 'apple' found
3Highlight matched texthighlight fields: contentMatched 'apple' text wrapped with highlight tags
4Return resultsresults with highlightResults include original fields plus highlighted snippets
5Client displayshighlighted snippetsUser sees 'apple' highlighted in search results
💡 All matching documents processed and highlighted text returned to client
Variable Tracker
VariableStartAfter Step 2After Step 3Final
query{}{"match":{"content":"apple"}}{"match":{"content":"apple"},"highlight":{"fields":{"content":{}}}}Same query throughout
matched_docs[][doc1, doc2][doc1, doc2][doc1, doc2] with highlights
highlighted_textN/AN/A["<em>apple</em>"]["<em>apple</em>"]
Key Moments - 3 Insights
Why does the highlight only appear in the 'content' field?
Because the highlight section specifies only the 'content' field for highlighting, as shown in execution_table step 3.
What happens if no documents contain the word 'apple'?
No documents match in step 2, so no highlights are created or returned, ending the process early.
Does the original text change when highlighted?
No, the original text stays the same; highlight adds tags around matched words in the returned snippet only (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Elasticsearch find documents containing 'apple'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Action' and 'Result/Effect' columns in execution_table row for Step 2
According to variable_tracker, what is the value of 'highlighted_text' after Step 3?
A[]
B["apple"]
C["<em>apple</em>"]
DN/A
💡 Hint
Look at the 'highlighted_text' row under 'After Step 3' in variable_tracker
If the highlight field was changed to 'title' instead of 'content', what would happen?
AHighlighting would appear in the 'title' field instead of 'content'
BHighlighting would appear in both 'title' and 'content'
CNo highlighting would appear
DQuery would fail
💡 Hint
Refer to execution_table step 3 where highlight fields are specified
Concept Snapshot
Highlighting matched text in Elasticsearch:
- Add 'highlight' section in query
- Specify fields to highlight
- Elasticsearch wraps matched terms with tags
- Results include highlighted snippets
- Client shows highlighted text to user
Full Transcript
This visual execution shows how Elasticsearch highlights matched text. First, a search query with a highlight section is sent. Elasticsearch finds documents matching the query term 'apple' in the 'content' field. Then it wraps the matched text with highlight tags like <em>. The results returned include these highlighted snippets. Finally, the client displays the highlighted text to the user. Variables like matched_docs and highlighted_text change as the query runs. Key points include that only specified fields are highlighted and original text is unchanged. The quiz checks understanding of steps and variable states.