Search code examples
laravellaravel-5laravel-scout

using insert() and doing a bulk insert doesnt seem to trigger a insert with scout into Algolia


I am doing a bulk insert in Laravel like

\App\Example::insert([
    [
        'name' => 'abc',
        'value' => '123',
    ],
    [
        'name' => 'def',
        'value' => '456',
    ],
    // etc...
]);

This will do a bulk insert, one query but still inserting many rows at once.

The problem is that when I use insert() the rows doesnt get inserted to Algolia. So how can I do a bulk insert to Algolia as well?

I dont want to loop through my rows and do a insert one by once since this will cost extra requests


Solution

  • The problem is that Laravel Scout creates the Algolio Searchable on the model::created method.

    You can view that here in the source code.

    As Laravel does not spawn the created/updated/deleted events on Mass Events this means that Algolio will never receive these as well.

    One option to work around this would be to take the most recent auto-incrementing ID, then after the insert take that ID again, then call ->searchable() on a filtered where clause on the relationship, like this:

     DB::transaction(function() {
        $currentId = \App\Example::max('id');
        // do the insert
        $lastId = \App\Example::max('id');
    
        \App\Example::where('id', '>', $currentId)->where('id', '<=', $lastId)->searchable();
     });
    

    By using a transaction we ensure that we the $lastId wouldn't be corrupted by parallel queries. In this way, we have to perform 4 queries, which is still nominal compared to possibly dozens or more.

    Truthfully you could probably simplify that code more.