Highlighting is a vital tool for showing searchers why a result matched their query by providing different styling to all matched query words.By default, Highlighting is enabled on all searchableAttributes
unless specified otherwise in attributesToHighlight.
Use one of the API clients or the Algolia dashboard, not InstantSearch, to configureattributesToHighlight.
Below is an example of how to configure which attributes to highlight:
var response = await client.SetSettingsAsync( "INDEX_NAME", new IndexSettings { AttributesToHighlight = new List<string> { "author", "title", "content" }, });
Algolia highlights the first 50,000 characters of the whole result set (5,000 logograms for CJK languages). This limit prevents impacting the user experience by ensuring a fast response time even with large results.
Since Algolia returns highlighted results as they’re stored in the engine, make sure you sanitize your HTML.
Unsanitized or invalid HTML content, particularly with user-provided content, can be a security risk and opens up the possibility of cross-site-scripting attacks on your site.
The primary configurations that you can set for highlighting are pre-tags and post-tags.
This configuration lets you use any tag (HTML or otherwise) to highlight results in the UI.You can set the pre-tags and post-tags (that is, the strings before and after the matched query words) to any string value. They’re set to <em> and </em> by default.You can configure this setting using the highlightPreTag and highlightPostTag parameters at either query or indexing time.
var response = await client.SetSettingsAsync( "INDEX_NAME", new IndexSettings { AttributesToHighlight = new List<string> { "author", "title", "content" }, HighlightPreTag = "<em class=\"search-highlight\">", HighlightPostTag = "</em>", });
Algolia provides a Highlight widget in the InstantSearch libraries to highlight matches on the frontend.
Refer to the widget docs for usage notes and code examples.
Snippeting returns parts of the matched attribute, namely, the matched words and some words around them. Unlike highlighting, you must enable snippeting for each attribute you wish to snippet, although you can set the value * to snippet all attributes.The snippeted result wraps the matched words in the pre-highlighting and post-highlighting tags.
Use one of the API clients or the Algolia dashboard, not InstantSearch, to configureattributesToSnippet.
You can set the number of words to return when defining your attributesToSnippet with the syntax attribute:nbWords. For example, body:20 returns twenty-word snippets for the attribute body. When undefined, the value defaults to 10.
var response = await client.SetSettingsAsync( "INDEX_NAME", new IndexSettings { AttributesToSnippet = new List<string> { "body:20", "title" }, });
The engine inserts text to show where words have been removed from the snippeted text.The default replacement text is … (Unicode U+20216): an ellipsis character. However, you can change this with the snippetEllipsisText setting:
var response = await client.SetSettingsAsync( "INDEX_NAME", new IndexSettings { SnippetEllipsisText = "[…]" });
If you want an attribute to keep its HTML formatting,
instead of asking Algolia to shorten (snippet) it for you,
create short-form in your data based on a subset of the original,
longer-form records.
Ensure that these short-form records are restricted to an appropriate length and contain valid HTML (all opening tags have matching closing tags).
You can do this programmatically,
for example, with:
RegExp. If you have a limited, known set of HTML, you can use regular expressions to split the content. Be aware of the limitations of the RegExp approach.
There’s a Snippet widget in the InstantSearch libraries to snippet text attributes on the frontend.
Refer to the widget docs for usage notes and code examples.