To highlight matches in Elasticsearch results, add a highlight object to the search request and list the fields to highlight. The elasticsearch highlight feature returns a highlight section for each hit with text fragments in which the matching terms are wrapped in <em> tags by default. You then render those fragments in your search results page.
Highlighting works on the same query you use for searching, so it shows the user why a document matched. This guide covers the basics, custom tags, fragment control and the available highlighters.
Assume an index articles with title and body text fields:
A hit in the response looks like this:
The highlight section is separate from _source. The original document is unchanged, and the highlighted fragments are an array of strings because a field can have several matching passages.
You can replace the default <em> tags with your own, and control how many fragments are returned and how long they are:
The main options are:
| Option | What it does |
|---|---|
pre_tags, post_tags | Tags inserted before and after each match |
fragment_size | Approximate fragment length in characters (default 100) |
number_of_fragments | Maximum fragments per field (default 5); 0 returns the whole field with highlights |
no_match_size | Characters to return from the start of the field when nothing matched |
order | Set to score to put the best fragments first |
Setting number_of_fragments to 0 is useful for short fields such as a title, where you want the entire text with the matching words marked.
Highlight tags are inserted into text that may contain HTML from your own documents. If the indexed text contains markup, the result mixes it with your tags. To avoid injecting stored HTML into your page, set encoder to html:
With this setting, special characters in the stored text are escaped, while your highlight tags are kept as real tags. Always render the fragments with a method that expects that, and never insert unescaped user text into a page.
Elasticsearch ships with three highlighter types, chosen with the type option:
unified: the default. It uses the Lucene unified highlighter, splits text into sentences, and scores passages with BM25. It is the right choice for most cases.plain: re-analyses the field text in memory with a simple algorithm. It works without extra index data but is slower on large fields and suited only to simple queries.fvh (fast vector highlighter): needs the field mapped with term_vector set to with_positions_offsets. It handles very large fields and supports options the others lack, at the cost of a bigger index.For the unified highlighter, the field's original text must be available, normally from _source, or stored with "store": true. To make highlighting faster on large fields, you can index offsets in the postings by mapping the field with "index_options": "offsets".
Highlighting only works on text that is analysed in a way that matches the query. Highlighting a keyword field works for exact matches, but fuzzy or stemmed queries mark the actual words from the original text, not the stemmed form.
Highlighting follows the query terms, but it only considers queries that contribute terms. Clauses in filter or must_not produce no highlights. If you need a filter-like condition to highlight too, put it in must, or give highlight_query to specify a different query for highlighting:
Remember that the search request only returns documents matching the main query, so highlight_query changes only which words are marked, not which documents are found.
The query likely did not match the highlighted field, or the field name is wrong. A field with no matching terms returns no highlight unless you set no_match_size.
Yes. List them all under highlight.fields, each with its own options. To match fields by pattern use a wildcard such as "*", which is slower, so list the fields you need.
No. Highlights are generated at search time and returned beside _source.
Start with unified, the default. Use fvh only for very large fields where you have enabled term vectors.
Add a highlight object with the fields you want to see marked, and each hit gets fragments with the matching words wrapped in tags. Customise tags with pre_tags and post_tags, shape fragments with fragment_size and number_of_fragments, and set encoder to html when stored text may contain markup. The default unified highlighter suits most cases, and highlight_query lets you mark terms that are not in the main query.