Application · lesson 11 of 20 · Search, Browse, And Discovery

Search result highlighting

Underlying principle: Discovery supports several starting points: a known term, a broad category, a partial memory, or a set of constraints. The interface helps people refine their understanding while they search.

Show query-linked highlighted terms inside relevant result fields and snippets, provide enough context and accessible emphasis, and keep the highlights synchronized with the submitted query and backend match data.

Start with the decision

Does this fit your task?

Results contain enough text that users need to scan for where the query matched.

Review the boundary: Compare the task with the alternatives in the decision section.

Review the decision →
Study the interaction

Follow the task before choosing the component

Discovery supports several starting points: a known term, a broad category, a partial memory, or a set of constraints. The interface helps people refine their understanding while they search.

01 / Recognize the need

What the person is doing

Search results include long titles, summaries, transcripts, records, documents, tickets, messages, products, logs, or knowledge articles.

02 / Design the response

What the interface must communicate

Show query-linked highlighted terms inside relevant result fields and snippets, provide enough context and accessible emphasis, and keep the highlights synchronized with the submitted query and backend match data.

Submitted query with one or more highlighted result fields.

03 / Judge the outcome

What to look for

Can users tell exactly which words, phrases, or fragments caused each result to match?

Changing the query updates highlighted words, snippets, match counts, and result summaries together.

Decision first

Choose this pattern when the problem matches

Use when

  • Results contain enough text that users need to scan for where the query matched.
  • Matches can occur in multiple fields and users need to understand title, body, metadata, or attachment relevance.
  • Users compare many similar documents, tickets, messages, logs, products, or records.

Avoid when

  • The result title is the entire match and extra emphasis would add noise.
  • The query is empty, redacted, hidden for privacy, or too broad for meaningful emphasis.
  • The task is query formulation before submission, zero-result recovery, or selecting a known value.
  • Highlighting would expose sensitive hidden fields, private notes, or permission-denied content.

Problem it prevents

Search results include long titles, summaries, transcripts, records, documents, tickets, messages, products, logs, or knowledge articles.

Test the behavior

Try strong and weak versions of the interaction

Open the working example, try its controls, and compare the behavior against the decision guidance.

In the lab you can
  • switch between strong UI, weak UI, strong UX, and weak UX;
  • trigger required, empty, loading, error, and recovery states;
  • inspect keyboard and accessibility expectations beside the demo.
Launch Search result highlighting in the lab
Pattern anatomy

What a strong implementation has to make clear

User need

Search results include long titles, summaries, transcripts, records, documents, tickets, messages, products, logs, or knowledge articles.

Pattern promise

Show query-linked highlighted terms inside relevant result fields and snippets, provide enough context and accessible emphasis, and keep the highlights synchronized with the submitted query and backend match data.

Required state

Submitted query with one or more highlighted result fields.

Failure to inspect

The visual highlight is disconnected from the query that produced the result list.

Access contract

Use semantic text markup such as mark for highlighted passages when the highlight indicates relevance in the current search context.

Quality bar

The difference between expert and weak execution

Strong implementation

Specific, visible, recoverable

  • A result card for Benefits appeal policy highlights Benefits in the title and appeal in the snippet, with a small label saying 2 highlighted fields.
  • A document search result shows a compact fragment with ellipses before and after the matching sentence so the highlighted term has readable context.
  • A user searches benefits appeal and can immediately see which result matched benefits in the title and appeal in the excerpt.
  • A user disables body highlighting to scan title matches only, while the result count and order stay unchanged.
Weak implementation

Vague, hidden, hard to recover from

  • Old highlighted words remain visible after the user clears the query or switches to a zero-result state.
  • Result highlighting is implemented by injecting raw backend HTML into a card, causing unsafe or broken markup.
  • The visual highlight is disconnected from the query that produced the result list.
  • A user trusts a result because the card is highlighted even though the query term appears only in hidden or permission-denied content.
UI guidance
  • Render highlighted query matches inside result titles, snippets, or field previews with semantic text markup such as mark and a visible style that does not rely on color alone.
  • Pair highlighted fragments with field labels, match counts, result titles, and enough surrounding snippet text that users can understand why the result is present.
UX guidance
  • Use search result highlighting to explain relevance after a query has returned results, especially when users scan dense documents, records, tickets, messages, or logs.
  • Keep the highlight tied to the submitted query and clear or update it whenever the query, scope, filters, or backend highlight response changes.
Implementation contract

What the implementation must handle

States

  • Submitted query with one or more highlighted result fields.
  • Multiple matching fields state with title, snippet, and metadata labels.
  • Snippet truncation state with ellipsis or fragment boundaries around removed text.
  • Updated query state where old highlights are removed before new ones appear.

Interaction

  • Changing the query updates highlighted words, snippets, match counts, and result summaries together.
  • Opening a result preserves the query context or provides a path back to the highlighted result list.
  • Highlight styling remains readable in selected, focused, hover, dark, high-contrast, and printed states.
  • Screen-reader and keyboard users can understand match location from result text, field labels, counts, and context rather than color alone.

Accessibility

  • Use semantic text markup such as mark for highlighted passages when the highlight indicates relevance in the current search context.
  • Keep highlight contrast readable against both result-card background and selected or focused states.
  • Avoid announcing every single highlight boundary unless missing that boundary would materially harm understanding.
  • Ensure highlighted snippets remain selectable, zoomable, and readable when text spacing or high-contrast modes are active.

Review

  • Can users tell exactly which words, phrases, or fragments caused each result to match?
  • Does the highlight map to the submitted query and field labels rather than unrelated metadata or ranking rules?
  • Can users distinguish exact, fuzzy, synonym, and semantic highlights when that difference affects trust?
  • Does the implementation sanitize backend markup and avoid raw HTML injection?
Evidence trail

Source-backed claims behind this guidance

Algolia Highlighting and snippeting

Algolia - checked

Algolia documents highlighting and snippeting for explaining why a result matched, including matched words, snippets, pre/post tags, performance limits, and sanitization.

Elasticsearch Search API highlighting

Elastic - checked

Elastic documents highlighted fragments per search hit and cautions that complex query logic may produce highlights that do not map exactly to all query conditions.

MDN HTML mark text element

MDN Web Docs - checked

MDN documents mark as suitable for context-relevant passages such as words that matched a search operation.

Full agent/debug reference

Problem Context

  • Search results include long titles, summaries, transcripts, records, documents, tickets, messages, products, logs, or knowledge articles.
  • Users need to scan many results quickly and decide which result is worth opening.
  • Search logic may match across several fields, synonyms, snippets, or semantic fragments that are not obvious from the result title alone.

Selection Rules

  • Choose search result highlighting when returned results need visible evidence of where the submitted query matched.
  • Highlight specific terms, phrases, or backend-provided fragments rather than entire result cards.
  • Show highlighted snippets only for fields that participated in matching or that the system can explain honestly.
  • Use field labels when matches can appear in title, body, metadata, comments, attachments, or hidden indexed fields.
  • Show surrounding text and fragment boundaries so a highlighted term has readable context.
  • Use mark semantics or equivalent text-level markup for highlighted passages, then style the match with contrast, border, weight, or underline in addition to color.
  • Sanitize backend-provided highlight markup or convert it into safe text nodes before rendering result cards.
  • Clear stale highlights when users clear the query, change scope, alter filters, or move to a no-results state.
  • Explain fuzzy, synonym, semantic, or analyzer-driven highlights when the displayed match is not an exact query-word match.
  • Avoid highlighting if the query is empty, sensitive, hidden for privacy, or too broad to produce meaningful match emphasis.

Required States

  • No query state with no highlighted terms.
  • Submitted query with one or more highlighted result fields.
  • Multiple matching fields state with title, snippet, and metadata labels.
  • Snippet truncation state with ellipsis or fragment boundaries around removed text.
  • No highlighted fragment available state that still explains why the result appears when possible.
  • Fuzzy, synonym, or semantic match state with a cue that the highlight may not be an exact word match.
  • Updated query state where old highlights are removed before new ones appear.
  • No-results state with all previous highlights cleared.
  • Unsafe or invalid highlight markup fallback state that renders safe plain text.

Interaction Contract

  • A highlight represents the submitted query or configured highlight query, not a decorative emphasis chosen independently from search matching.
  • Changing the query updates highlighted words, snippets, match counts, and result summaries together.
  • Toggling which fields are highlighted changes only the emphasis layer, not the underlying result set or ranking.
  • Opening a result preserves the query context or provides a path back to the highlighted result list.
  • Highlight styling remains readable in selected, focused, hover, dark, high-contrast, and printed states.
  • Screen-reader and keyboard users can understand match location from result text, field labels, counts, and context rather than color alone.
  • Backend highlight markup is treated as untrusted content unless it has already been sanitized by the rendering layer.
  • If the highlighter is approximate for complex or semantic queries, the UI does not overclaim exact boolean satisfaction.

Implementation Checklist

  • Keep a canonical submitted query separate from draft input so highlights match the result set currently shown.
  • Tokenize or request highlight fragments from the search service using the same analyzer or highlight configuration as the search query.
  • Render matches as safe text nodes and mark elements, or use a trusted highlight component that escapes raw content.
  • Include field labels and match counts when more than one result field can contain highlighted fragments.
  • Limit snippet length, show ellipses or boundaries, and keep enough surrounding words for comprehension.
  • Style mark elements with sufficient contrast and at least one non-color cue such as weight, border, underline, or background shape.
  • Test exact, phrase, prefix, fuzzy, synonym, CJK, diacritic, punctuation, and case-insensitive queries against the displayed highlight behavior.
  • Remove stale highlight state before showing no-results, error, loading, or empty-query states.

Common Generated-UI Mistakes

  • Highlighting whole cards, rows, or table columns instead of the matching terms.
  • Using color-only yellow text as the sole signal that a word matched.
  • Showing highlights that come from popularity, ads, categories, or business rules instead of the submitted query.
  • Injecting backend highlight HTML directly into result cards without sanitization.
  • Leaving old highlighted terms visible after the query changed.
  • Treating approximate semantic snippets as exact proof that every query condition matched.

Critique Questions

  • Can users tell exactly which words, phrases, or fragments caused each result to match?
  • Does the highlight map to the submitted query and field labels rather than unrelated metadata or ranking rules?
  • Is there enough surrounding snippet text to judge relevance without opening every result?
  • Can users distinguish exact, fuzzy, synonym, and semantic highlights when that difference affects trust?
  • Does the implementation sanitize backend markup and avoid raw HTML injection?
  • Are highlights still understandable without color perception or with assistive technology verbosity disabled?
Accessibility
  • Use semantic text markup such as mark for highlighted passages when the highlight indicates relevance in the current search context.
  • Do not rely on color alone; pair background color with text weight, underline, border, count, or field label cues.
  • Keep highlight contrast readable against both result-card background and selected or focused states.
  • Avoid announcing every single highlight boundary unless missing that boundary would materially harm understanding.
  • Expose match counts and field names in text for users who do not perceive visual highlight styling.
  • Ensure highlighted snippets remain selectable, zoomable, and readable when text spacing or high-contrast modes are active.
Keyboard Behavior
  • Tab reaches search input, result cards, and any highlight-field controls in a predictable order.
  • Changing highlight-field toggles does not unexpectedly move focus or reorder results.
  • If next-match navigation exists, Enter or Space moves to the next highlighted fragment and announces its field and result title.
  • Opening a result and returning to the list restores the submitted query and visible highlighted result when possible.
  • Keyboard focus indicators remain visible when they overlap highlighted text.
Variants
  • Exact term highlighting
  • Phrase highlighting
  • Field-specific highlighting
  • Snippet highlighting
  • Semantic fragment highlighting
  • Synonym highlight disclosure
  • Attachment text highlighting
  • Log line highlighting
  • In-document hit navigation

Verification

Source trail checked:

Saved items