📖 Scenario: You are building a search feature for a small online bookstore. When users search for a word, you want to show the book titles with the searched word highlighted so it is easy to spot.
🎯 Goal: Create an Elasticsearch query that searches for the word magic in book titles and highlights the matched word in the results.
📋 What You'll Learn
Create an Elasticsearch index called books with a title field.
Add a document with the title 'The Magic of Thinking Big'.
Write a search query that looks for the word magic in the title field.
Add highlighting to the query so the matched word magic is wrapped in <em> tags in the results.
💡 Why This Matters
🌍 Real World
Highlighting matched text helps users quickly see why a search result was returned, improving user experience in search applications.
💼 Career
Understanding Elasticsearch highlighting is useful for roles involving search engine development, backend engineering, and data retrieval optimization.
Progress0 / 4 steps
1
Create the Elasticsearch index and add a book document
Create an Elasticsearch index called books with a title field. Then add a document with title set to 'The Magic of Thinking Big'.
Elasticsearch
Hint
Use PUT to create the index with mappings, then POST to add the document.
2
Set up the search query to find the word 'magic'
Write a search query that looks for the word magic in the title field of the books index.
Elasticsearch
Hint
Use the match query on the title field with the word magic.
3
Add highlighting to the search query
Add a highlight section to the search query that highlights matches in the title field using <em> tags.
Elasticsearch
Hint
Use highlight with fields and specify pre_tags and post_tags for the title field.
4
Display the search results with highlighted matched text
Run the search query and print the highlight section from the first hit to show the matched word magic wrapped in <em> tags.
Elasticsearch
Hint
Extract the highlight field from the first hit in the search results and print it.
Practice
(1/5)
1. What is the main purpose of the highlight section in an Elasticsearch query?
easy
A. To filter documents before searching
B. To sort the search results by relevance
C. To update documents in the index
D. To show matched words clearly in search results
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 D
Quick Check:
Highlight = show matched words [OK]
Hint: Highlight section shows matched words in results [OK]
Common Mistakes:
Confusing highlight with sorting or filtering
Thinking highlight updates documents
Assuming highlight changes search relevance
2. Which of the following is the correct syntax to add highlighting for the field content in an Elasticsearch query?
easy
A. "highlight": {"fields": {"content": {}}}
B. "highlight": {"field": "content"}
C. "highlight": {"content": {"fields": {}}}
D. "highlight": {"fields": ["content"]}
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 A
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 B
Quick Check:
Highlight fields = object, not array [OK]
Hint: Highlight fields must be an object, not a list [OK]
Common Mistakes:
Using array instead of object for fields
Placing highlight inside query incorrectly
Assuming some fields can't be highlighted
5. You want to highlight matched text in both title and content fields with custom tags <strong> and </strong>. Which is the correct highlight section to achieve this?
D. "highlight": {"fields": {"title": {}, "content": {}}, "tags": ["", ""]}
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.