Instructor: Here, when we type this query, it's not instantly obvious why those results are there. By looking long enough, we can finally spot that the match occurred in the description. This is not optimal.
To fight that issue, a simple way to make results relevance obvious to your user is to implement highlighting. The goal is simple. Style differently parts of the hits that are matching the query so that they stand out from the rest of the hits information.
Thankfully, Algolia does a big part of the job for us. Let's take a look at the JSON response here, typing A. For each query, we get, in addition to hits and facets, the highlight result object. Highlighting can be enabled in the specific subset of searchable attributes. By default, it's activated on all of them.
Here, we see all of our searchable attributes -- brand, categories, descriptions, name, and type. Let's look at how a single attribute is composed.
Opening the name attribute, here we get four elements. The one that really matters to us for the moment is the value. This is where the searchable attribute initial value will be returned, modified with the ais-highlight markers enclosing the matching parts. Those markers are then replaced by the library with a tag of your choosing, by default, the mark tag. Here, we'll keep the default.
To display highlighted results, there's a couple of things we need to do. First, we need to create a CSS rule for our mark tag. This has already been done in our style.css file. Let's first highlight the name.
Instead of accessing them directly through their attributes, we will leverage the Instant.Search highlight method, passing it the attribute and the hit data. Saving and reloading the page, now typing a few queries, we have highlighting visible on the name and we can now tell immediately what results are matching and where.
Let's repeat the same process for the description. Now we have a search page with fully functional highlighting, simply by tweaking a little our existing template.