Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sunrise's avatar

Algolia highlighting in Laravel 5.3

I am using Laravel 5.3 and Algolia,

I want to highlight the search result,I read the documentation but I still don't know how to do it.

https://www.algolia.com/doc/api-client/php/parameters#attributestohighlight

Any ideas?

0 likes
5 replies
Braunson's avatar

@sunrise

Hmm it seems that settings in Algolia are usually set at the same time as the data indexing. After looking into the Scout repo, there was a pending pull request from Algolia to expose the setSettings method exactly to achieve what you're looking for in order to be able to set your setting to highlight. It was closed 18 days ago but Taylor is on it!

Alternatively for now you can just extend the AlgoliaEngine trait to add this method on your own.

tntstudio's avatar

If you try with another driver like tntsearch you'll have access to a highlighter class and would use it like:

use TeamTNT\TNTSearch\TNTSearch;

$articles = Article::search($searchString)->get();
$tnt = new TNTSearch;

$articles = $articles->map(function($article) use ($searchString, $tnt) {
    $article->title = $tnt->highlight($title, $searchString, 'em'),
});

The third argument of the highlight method is the html element you want to use

eugenefvdm's avatar

How on do you do a partial highlight using Algolia / Laravel Scout? I have a very long paragraph and I just need to highlight some of the words found, e.g. "testing" and a couple of words round it.

Please or to participate in this conversation.