algolia / scout-extended

Scout Extended: The Full Power of Algolia in Laravel
https://www.algolia.com/doc/framework-integration/laravel/getting-started/introduction-to-scout-extended/
MIT License
400 stars 87 forks source link

MakeSearchable runs twice when updating aggregated model #237

Open aniplaylist opened 4 years ago

aniplaylist commented 4 years ago

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 :

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
nunomaduro commented 4 years ago

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.

aniplaylist commented 4 years ago

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

nunomaduro commented 4 years ago

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.

aniplaylist commented 4 years ago

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()
    {
    }
}
tech-commoprices commented 4 years ago

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 commented 4 years ago

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

nunomaduro commented 4 years ago

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