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

Attributes to snippet, with an optional maximum word limit.

Usage notes

This parameter lets you only show part of an attribute in search results: a snippet

  • The snippet length is 10 words .
  • To apply snippets to all attributes, use *.

Impact on the response:

When snippets are enabled, each search result contains a _snippetResult object, which includes:

  • value (string): the snippet text, highlighted according to the highlightPreTag and highlightPostTag settings. The default indicator for a snippet is an ellipsis character: customize it with snippetEllipsisText.
  • matchLevel (string, can be none, partial, or full): shows how closely the attribute matched the search query.

Examples

Set default attributes to snippet

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

Snippet all attributes 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?