laravellaravel-scoutmeilisearch

How to customize scout/meilisearch search results?


I'm using Scout with meilisearch and I want the search results to include extra data like link url or thumb url. Till now in docs I've found (and used) how to define the indexed fields, the filterable fields and the sortable ones. But didn't find how to add arbitrary data, except for using the toSearchableArray function, that indeed will add data if added, but it also will index it, which is not what I want. One thing is adding fields to index, another is adding them only in retrieved data.

Also, please note that I would like to pack all the data on the server side, not on the client layer.

How to? Mmm, I also must say that I'm currently doing the frontend only using meilisearch-js, something like (very trimmed down):

import { MeiliSearch } from 'meilisearch'

const client = new MeiliSearch({
  host: 'http://localhost:7700/', 
  apiKey: 'mykey',
});

function search_seeds(value, options){
  seeds.search(value, options).then(response => {
    build_search_results_menu(response.hits);
  })
}

[...]

Solution

  • I'll answer by myself: first, I want to say, Laravel Scout's docs are a shame, really really poor. I also did a PR adding the following info but they currently didn't even accept it.

    That said, I found out that, if you want to exploit Scout to add custom props to the search results, you can simply use scoutMetadata() in your model (just an example adding a couple of props):

        public function scoutMetadata(){
            $image = $this->images->first();
            $thumbnail = null;
    
            if($image){
                $thumbnail = $image->get_image_url('micro');
            }
            
            return [
                'url' => url("/semi/{$this->id}"),
                'thumbnail' => $thumbnail,
            ];
        }