scout-extended icon indicating copy to clipboard operation
scout-extended copied to clipboard

MakeSearchable runs twice when updating aggregated model

Open aniplaylist opened this issue 5 years ago • 7 comments

Hello,

I have just set up an Aggregator on my project with two Models in it.

Whenever I'm updating one of these two models the MakeSearchable Job is run twice :

  • Once for the Model
  • Once for the Aggregator

Is this the expected behaviour? Isn't this consuming two indexing operations on Algolia instead of one?

Two jobs seen on Horizon :

horizon-jobs

Model :

jobs-103-model

Aggregator :

jobs-104-aggregator

aniplaylist avatar May 26 '20 18:05 aniplaylist

Correct. If both the model, and the aggregator are searchable that is the normal behaviour.

You really need to make the models searchable? If not, you can remove the Searchable trait from those models, and the aggregator will still work consuming just one indexing operation per update.

nunomaduro avatar May 26 '20 18:05 nunomaduro

Just tried removing the Searchable trait from my model but MakeSearchable job is not triggered anymore (not even once) and the entry I updated is removed from my index

aniplaylist avatar May 26 '20 18:05 aniplaylist

That is because you probably forgot to call the bootSearchable in your service provider as specified here: algolia.com/doc/framework-integration/laravel/advanced-use-cases/multiple-models-in-one-index/?language=php#aggregators---multiple-models-in-one-index.

nunomaduro avatar May 26 '20 19:05 nunomaduro

I didn't forget 🤔

<?php

namespace App\Providers;

use App\Search\Results;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Results::bootSearchable();
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    }
}

aniplaylist avatar May 26 '20 19:05 aniplaylist

Hi @aniplaylist , just ran into the same issue.

Simply change the shouldBeSearchable method on your Aggregator to make it look like this :

    public function shouldBeSearchable()
    {
        return $this->model->shouldBeSearchable();
    }

tech-commoprices avatar Jun 03 '20 16:06 tech-commoprices

It could be useful to put this in the docs btw :-)

tech-commoprices avatar Jun 03 '20 16:06 tech-commoprices

Thanks! We are aware of this issue, no ETA for having this on the package at the moment.

nunomaduro avatar Jun 03 '20 16:06 nunomaduro