API Reference / API Parameters / attributesToSnippet
Type: list of strings
Engine default: [] (no attribute is snippeted)
Parameter syntax
'attributesToSnippet' => [
  'attribute',
  'attribute2:number_of_words;', // limits the size of the snippet
  ...
]

Can be used in these methods:

About this parameter

List of attributes to snippet, with an optional maximum number of words to snippet.

Usage notes:

  • The number of words can be omitted, and defaults to 10.
  • If not set, no attributes are snippeted.
  • The special value * may be used to snippet all attributes.

Impact on the response:

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

    • value (string): Markup text with occurrences highlighted and optional ellipsis indicators. The tags used for highlighting are specified via highlightPreTag and highlightPostTag. The text used to indicate ellipsis is specified via snippetEllipsisText.

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

Examples

Set default list of attributes to snippet

1
2
3
4
5
6
$index->setSettings([
  'attributesToSnippet' => [
    'content:80',
    'description'
  ]
]);

Make all attributes highlighted by default

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

The Response Object

1
2
3
4
5
6
7
"_snippetResult":{
  "attribute":{
    "value":"to be or not to be that is ...",
    "matchLevel":"full"
  },
  ...
}

Did you find this page helpful?