Truncate results

The text in the results I am displaying is too long. I want to truncate the displayed text to eg. 140 characters and add ‘…’ to the end. This is the current code:

{{{_highlightResult.description.value}}}

(edited for clarity)

Try snippetResult instead of highlightResult. Snippets are similar to highlights, except that highlights include all text of the attribute and snippets only include the text around the match.

{{{_snippetResult.description.value}}}

Snippet results are not included by default, they must be added to the search query using the searchParameters and attributesToSnippet keys. You can add this to all queries by specifying the settings when you make the call to initialize instantsearch.

var search = instantsearch({
  appId: '$appId',
  apiKey: '$apiKey',
  indexName: '$indexName',
  urlSync: true,
  searchParameters: {
    attributesToSnippet: ["FIELD_NAME"] 
  }
});

Make sure to replace FIELD_NAME with the attribute of the object you will be using to display the results.

See the fields section of the JavaScript guide to learn more about snippet results and how you can customize them, including their size in words.

1 Like

window.ALGOLIA_CONFIG = {
‘applicationId’: ‘{{ site.algolia.application_id }}’,
‘indexName’: ‘{{ site.algolia.index_name }}’,
‘apiKey’: ‘{{ site.algolia.read_only_api_key }}’,
‘searchParameters’: {
attributesToSnippet: [“FIELD_NAME”] // ???
}
}

{{{ _SnippetResult.description.value }}}

Thanks.

1 Like

Try a lowercase “s” for snippet, so:

{{{ _snippetResult.description.value }}}

1 Like