0
0
Elasticsearchquery~30 mins

Highlighting matched text in Elasticsearch - Mini Project: Build & Apply

Choose your learning style9 modes available
Highlighting Matched Text in Elasticsearch
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Extract the highlight field from the first hit in the search results and print it.