skip to Main Content

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);
  })
}

[...]

2

Answers


  1. Chosen as BEST ANSWER

    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,
            ];
        }
    

  2. I want the search results to include extra data like link url or thumb url.

    You can make use of Eloquent: API Resources

    php artisan make:resource PostResource
    

    app/Http/Resources/Post.php

    <?php
    
    namespace AppHttpResources;
    
    use IlluminateHttpRequest;
    use IlluminateHttpResourcesJsonJsonResource;
    
    class Post extends JsonResource
    {
        /**
         * Transform the resource into an array.
         *
         * @return array<string, mixed>
         */
        public function toArray(Request $request): array
        {
            return [
                'title' => $this->title,
                'description' => $this->description,
                'url' => $this->url, // URL of the post
                'thumb_url' => $this->thumb_url, // Thumb url of the post
            ];
        }
    }
    

    and use the API Resource as a transformation layer between your search results and JSON responses.

    routes/web.php

    <?php
    
    use AppHttpResourcesPost as PostResource;
    use IlluminateSupportFacadesRoute;
    use IlluminateHttpRequest;
    use AppModelsPost;
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider and all of them will
    | be assigned to the "web" middleware group. Make something great!
    |
    */
    
    Route::get('/', function (Request $request) {
        return PostResource::collection(
            Post::search($request->search)
                ->paginate(2)
        );
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search