Highlighting Search Results
Each search hit returns a highlight for every matched field: the field text with matched query words wrapped in tags. Storefronts use it to show shoppers why a product matched.
Highlighting is configured with highlight_settings on a search profile. Changes apply immediately. A reindex is not required.
Response
Highlights are returned in each hit's highlight object, keyed by field:
snippet: the field text with matches marked. Fields longer thansnippet_thresholdare trimmed tocontext_num_tokenswords either side of each match.value: the whole marked-up field. Returned only for fields infull_fields.
{
"highlight": {
"description": {
"snippet": "existing locks. 9 V <mark>alkaline</mark> battery included. Fits most"
}
}
}
Settings
| Setting | Default | Description |
|---|---|---|
fields | All query fields | Fields to highlight |
full_fields | None | Highlighted fields that also return value |
start_tag | <mark> | Tag before each match |
end_tag | </mark> | Tag after each match |
snippet_threshold | 30 | Field length above which snippet is trimmed. 0 trims every field |
context_num_tokens | 4 | Words kept either side of each match. 0 returns the matched word only |
Lengths count words, not characters.
{
"highlight_settings": {
"fields": ["name", "description"],
"full_fields": ["name"],
"start_tag": "<em>",
"end_tag": "</em>",
"snippet_threshold": 30,
"context_num_tokens": 6
}
}
On update, the object is replaced as a whole. Send "highlight_settings": null to restore the defaults.
Behavior Notes
fieldscan name fields the profile does not query, for example to searchnamebut highlightdescriptiontoo. Highlighting has no effect on matching or ranking. An empty array means all query fields. Highlighting cannot be turned off.full_fieldsentries must be highlighted fields: infieldswhen set, otherwise query fields. The profile is rejected otherwise.highlight_full_fieldsrequest parameter replaces the profile'sfull_fieldsfor that request.- Tags fall back to their defaults independently. Set both, or you get markup like
<em>toaster</mark>. The same tags apply to facet value search labels. - Autocomplete always highlights the suggestion text.
fieldsandfull_fieldsare ignored. Tags, snippet settings, andhighlight_full_fieldsapply.
warning
Highlights are not escaped. Sanitize them before rendering as HTML.