API Reference / API Parameters / attributesToHighlight
Type: list of strings
Engine default: all searchable attributes
Parameter syntax
'attributesToHighlight' => [
  'attribute',
  '*', // returns all attributes in the index not just searchable attributes
  ...
]

Can be used in these methods:

About this parameter

List of attributes to highlight.

Usage notes

  • If not set, all searchable attributes are highlighted (see searchableAttributes).
  • The special value * may be used to highlight all attributes.
  • Pass an empty array ([]) to disable highlighting and remove the _highlightResult object in API responses.

Impact on the response:

  • When highlighting is enabled, each hit in the response will contain an additional _highlightResult object (provided that at least one of its attributes is highlighted) with the following fields:

    • value (string): Markup text with occurrences highlighted. The tags used for highlighting are specified via highlightPreTag and highlightPostTag.

    • matchLevel (string, enum) = {none | partial | full}: Indicates how well the attribute matched the search query.

    • matchedWords (array): List of words from the query that matched the object.

    • fullyHighlighted (boolean): Whether the entire attribute value is highlighted.

Examples

Set default list of attributes to highlight

1
2
3
4
5
6
7
$index->setSettings([
  'attributesToHighlight' => [
    "author",
    "title",
    "content"
  ]
]);

Make all attributes highlighted by default

1
2
3
4
5
$index->setSettings([
  'attributesToHighlight' => [
    "*"
  ]
]);
1
2
3
4
5
6
$results = $index->search('query', [
  'attributesToHighlight' => [
    "title",
    "content"
  ]
]);

The Response Object

1
2
3
4
5
6
7
8
9
"_highlightResult":{
  "attribute1":{
    "value":"<b>q<\/b>uery",
    "matchLevel":"full",
    "fullyHighlighted":false,
    "matchedWords": [ "q" ]
  },
  ...
}
Did you find this page helpful?