AI Hints
Home›Elasticsearch Tutorial›Highlighting and Suggestions
Elasticsearch · Lesson 82 of 120

Highlighting

Show why it matched.

⏱ 2 min read▶ Video at 1:17:15🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Good search pages show why each result matched, with the matching words in bold. Elasticsearch can return those snippets, called highlights.

  • Why did it match?
  • Bold the words
  • Highlights

Highlight the description

We search descriptions for windy, and ask for highlights on the description field. Pre and post tags wrap each match. Run it. Each hit comes with a snippet, with windy wrapped in bold tags.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits.highlight
{
  "query": { "match": { "description": "windy" } },
  "highlight": {
    "fields": { "description": {} },
    "pre_tags": ["<b>"], "post_tags": ["</b>"] },
  "size": 2
}
Output
{
  "hits": {
    "hits": [
      {
        "highlight": {
          "description": [
            "A waterproof jacket for cold mornings and <b>windy</b> ridges."
          ]
        }
      },
      {
        "highlight": {
          "description": [
            "A rugged jacket for cold mornings and <b>windy</b> ridges."
          ]
        }
      }
    ]
  }
}
Also uses _load
catalog

Highlight options

Fragment size sets the snippet length. Number of fragments sets how many snippets. You can highlight several fields at once. And the matched words in the snippet follow your analyzer, so jackets highlights jacket.

keyvalue
fragment_sizesnippet length
number_of_fragmentshow many snippets
Fieldsseveral at once
Analyzerstems match too

Summary

Highlight returns snippets with the matching words wrapped in tags, ready to show in bold. Next, fast suggestions as you type.

Key takeaways
  • highlight
  • pre_tags
  • Snippets
Next up: Completion Suggester — instant suggestions