Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

algolia search - attributes to highlight vs attributes to snippet

I couldn't find a difference between these two settings in algoliasearch-client-js. Could you simply explain how, and more importantly why to use these settings?

Example would be great!

like image 530
vergilius Avatar asked Sep 01 '25 20:09

vergilius


1 Answers

attributesToHighlight allows to retrieve the full content of attributes with matched words highlighted using <em> html tag.

attributesToSnippet extracts the part of the attribute that contains the most matched words and highlight them.

For example if you indexed object is:

{ "question": "algolia search - attributes to highlight vs attributes to snippet"}

If you use attributesToHighlight, your search will be similar to:

search("algolia se", {"attributesToHighlight": "question"})

You will receive an answer of this form:

{
  "question": "algolia search - attributes to high:light vs attributes to snippet",
  "_highlightResult": {
    "question": {
      "value": "<em>algolia</em> </em>se</em>arch - attributes to highlight vs attributes to snippet",
      "matchLevel": "full",
      "matchedWords": [
        "algolia",
        "se"
      ]
}

If you use attributesToSnippet, your search will be similar to:

search("algolia se", {"attributesToSnippet": "question:2"})

You will receive an answer of this form:

{
  "question": "algolia search - attributes to high:light vs attributes to snippet",
  "_snippetResult": {
    "question": {
      "value": "<em>algolia</em> </em>se</em>arch",
      "matchLevel": "full",
      "matchedWords": [
        "algolia",
        "se"
      ]
}
like image 142
speedblue Avatar answered Sep 05 '25 02:09

speedblue