meilisearch: The attribute does not work attributesToHighlight

Hello everyone! The word highlighting function in meilisearch does not work. Please help me solve this issue.

 public function __invoke (Request $request){
        
        $rubric  = Category::select('id', 'title')->where('type', 0)->get()->sortBy('title');
        $query = $request['query'];
        $post = Post::search($query,  function($meilisearch, $query, $options) use ($request){
            $date = strtotime(now());
            
            if(empty($request->rubric))
            {
                $options['filters'] = "status = 1";
            }
            else
            {
                $options['filters'] = "status = 1 AND (category_id = $request->rubric)";
                $options['attributesToHighlight'] = ['title', 'excerpt'];
            }
           
            return $meilisearch->search($query,$options );
        } )
        ->paginate(10);

        $modelCollection =  $post->load('category');
      
        return view('search.index', compact('post', 'request', 'rubric'));
    }

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 42 (3 by maintainers)

Most upvoted comments

Hello @Sliper959595 đź‘‹

Custom ranking rules like desc(created_at) only work on numeric fields. See https://docs.meilisearch.com/reference/features/settings.html#custom-ranking-rule

You can store an extra field so you don’t have to replace all the logic that is already on created_at or change the format of created_at and the logic that goes with it.

Ok, so you are not retrieving the document directly from MeiliSearch, but from your database through eloquent, that’s why you don’t get the _formatted object…

Ok, if the mini-dahsboard highlights it, that means there is no problem on MeiliSearch’s side. There must be a problem with your code, I suspect your query doesn’t pass in the right part of the condition.