Highlighting Search Results with Elasticsearch
Last Updated :
23 Jul, 2025
One powerful open-source and highly scalable search and analytics web application that can effectively carry out efficiently retrieving and displaying relevant information from vast datasets is Elasticsearch.
It’s also convenient that Elasticsearch can highlight the text matches, which allows users to navigate to the proper search result quickly. In this article, you will find a simple and detailed tutorial on how to highlight search results in Elasticsearch with information about the key concepts and their usage, along with useful examples.
Main Concept and Syntax
Elasticsearch is another name for specifically enhancing the certain areas of the search results that best match the query. This feature is quite helpful especially when used in searches because it allows users to easily distinguish the parts of the returned documents relevant to their search criteria.
Basic Syntax
The highlighting feature can be implemented using the _search endpoint with the highlight parameter in the query. The basic syntax for a search query with highlighting looks like this:
GET /<index>/_search
{
"query": {
"match": {
"<field>": "<search_term>"
}
},
"highlight": {
"fields": {
"<field>": {}
}
}
}
Parameters:
- <index>: The name of the index you are searching in.
- <field>: The field in the document where the search term is expected.
- <search_term>: The term you are searching for.
Let's dive into some practical examples to better understand how highlighting works.
Step 1: Set Up Elasticsearch
1.1 Install Elasticsearch
First, download and install Elasticsearch. You can download it from the official Elasticsearch website. Follow the installation instructions for your operating system.
1.2 Start Elasticsearch
After installation, start the Elasticsearch service. On most systems, you can start it using the following command:
elasticsearch
Make sure Elasticsearch is running by opening your browser and navigating to http://localhost:9200. You should see a JSON response indicating that Elasticsearch is up and running.
Step 2: Create an Index and Add Documents
2.1 Create an Index
Open a terminal or command prompt and use the following command to create an index named articles:
curl -X PUT "localhost:9200/articles"
2.2 Add Documents
Add some sample documents to the articles index. Here are a couple of example documents:
Example 1: Basic Highlighting
Suppose we have an index article with documents containing field content. We want to search for the term "Elasticsearch" and highlight the matches in the content field.
Query
GET /articles/_search
{
"query": {
"match": {
"content": "Elasticsearch"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}
Output:
{
"hits": {
"hits": [
{
"_source": {
"content": "Elasticsearch is a powerful search engine..."
},
"highlight": {
"content": [
"<em>Elasticsearch</em> is a powerful search engine..."
]
}
}
]
}
}
In this example, the term "Elasticsearch" is wrapped in <em> tags, indicating the highlighted text. The default highlighting tag is <em>, but this can be customized.
Example 2: Customizing Highlighting Tags
You can customize the tags used for highlighting by specifying the pre_tags and post_tags parameters.
Query
GET /articles/_search
{
"query": {
"match": {
"content": "search"
}
},
"highlight": {
"fields": {
"content": {
"pre_tags": ["<strong>"],
"post_tags": ["</strong>"]
}
}
}
}
Output:
{
"hits": {
"hits": [
{
"_source": {
"content": "Elasticsearch is a powerful search engine..."
},
"highlight": {
"content": [
"Elasticsearch is a powerful <strong>search</strong> engine..."
]
}
}
]
}
}
Here, the term "search" is highlighted using <strong> tags instead of the default <em> tags.
Example 3: Highlighting Multiple Fields
You can highlight multiple fields in the documents by specifying them in the fields parameter.
Query
GET /articles/_search
{
"query": {
"multi_match": {
"query": "Elasticsearch",
"fields": ["title", "content"]
}
},
"highlight": {
"fields": {
"title": {},
"content": {}
}
}
}
Output:
{
"hits": {
"hits": [
{
"_source": {
"title": "Introduction to Elasticsearch",
"content": "Elasticsearch is a powerful search engine..."
},
"highlight": {
"title": [
"Introduction to <em>Elasticsearch</em>"
],
"content": [
"<em>Elasticsearch</em> is a powerful search engine..."
]
}
}
]
}
}
In this example, the term "Elasticsearch" is highlighted in both the title and content fields.
Conclusion
Highlighting results in Elasticsearch improves the overall functionality of the system because users can easily know where to find what they are looking for within a document. Another option you may use is the highlight parameter with a possibility to define what is enclosed into tags and how the matched terms are going to be highlighted: This feature can be a powerful tool when implemented into any search application that uses Elasticsearch.
As you have seen, Elasticsearch makes it easy for you to improve the usability and utility of your search applications by enabling highlighting.