Search and Elasticsearch learning path Level 2 This level · 6 of 18
Medium search elasticsearch

Why can highlighting make search slower, and how do you keep it cheap?

The 30 second answer

To build a snippet, the highlighter needs the character offsets of matched words. Without stored offsets, the unified highlighter re-analyzes the text of every returned document. Storing offsets in the postings with index_options offsets lets it skip that, and term vectors serve the fast vector highlighter at the cost of a bigger index. I highlight few fields, only for the returned page, and keep the default max_analyzed_offset cap.

Could you say this in an interview?

What highlighting does. It shows a snippet with the matched words marked, like "comfy summer dress in linen". To do that, the highlighter must know where each word starts and ends in the original text.

Where offsets come from. The docs list three sources:

  • The postings list. "If index_options is set to offsets in the mapping, the unified highlighter uses this information to highlight documents without re-analyzing the text."
  • Term vectors. The fast vector highlighter (fvh) needs term_vector: with_positions_offsets, which "increases the size of the index".
  • Re-analysis. Without either, the text is analyzed again at query time for every field and document highlighted. The docs say this "is repeated for every field and every document that needs to be highlighted".

A safety cap. Re-analysis of very large texts is limited: the docs say "the maximum number of text characters that will be analyzed has been limited to 1000000", configurable with index.highlight.max_analyzed_offset.

Badly written. Highlight every field with a wildcard, many fragments, on large pages:

{ "size": 200, "query": { "match": { "description": "linen" } },
  "highlight": { "fields": { "*": {} }, "number_of_fragments": 10 } }

Better. One or two fields, offsets indexed, few fragments, only for the page shown:

PUT products
{ "mappings": { "properties": {
    "description": { "type": "text", "index_options": "offsets" } } } }

GET products/_search
{ "size": 48, "query": { "match": { "description": "linen" } },
  "highlight": { "fields": { "description": { "fragment_size": 120, "number_of_fragments": 1 } } } }

Trade-offs.

  • Offsets in postings or term vectors make the index bigger and indexing a bit slower, in exchange for cheaper highlighting.
  • Product tiles in fashion often show only name, brand and image, so the cheapest highlight is none. Highlighting matters more for long text: help articles, reviews, internal tools.

Alternate interview scenarios

"Search is fast without highlighting and slow with it."

Check which fields are highlighted and whether they're large, profile with and without highlighting, then index offsets for those fields or stop highlighting them.

"A support tool highlights matches in 1 MB descriptions and errors out."

That's the max_analyzed_offset cap. Index offsets for that field, or set max_analyzed_offset in the highlight request so it highlights up to the limit instead of failing.

Sources (3)
  1. Elasticsearch docs: Highlighting (highlighters, offsets strategy, max_analyzed_offset)elastic.co
  2. Elasticsearch docs: index_optionselastic.co
  3. Elasticsearch docs: Highlighting settings (max_analyzed_offset per request)elastic.co
Previous
Next