babenkoivan / elastic-scout-driver-plus

Extension for Elastic Scout Driver
MIT License
267 stars 52 forks source link

Make ElasticScoutDriverPlus\Engine non final, so that can be extended in order to prioritize models' indexing based on custom rules #131

Closed sk33wiff closed 2 years ago

sk33wiff commented 2 years ago

We are migrating from the old driver, where we managed to prioritize the indexing of models' data according to their size by creating a custom Indexer and use it instead of the Simple or Bulk indexers that comes out of the box with that driver.

We need to refactor our solution into something that can be used with the new ecosystem, which this driver is part of.

Reasons why we want to be able to overwrite ElasticScoutDriverPlus\Engine::update()

    public function update($models)
    {
        if ($models->isEmpty()) {
            return;
        }

        $indexName = $models->first()->searchableAs();
        $routing = $this->routingFactory->makeFromModels($models);
        $documents = $this->documentFactory->makeFromModels($models); //<-- toSearchableArray is called in here, so the decision of which of the models should be indexed now and which should be deferred needs to happen before 

        $this->documentManager->index($indexName, $documents, $this->refreshDocuments, $routing);
    }

UPDATE: we know we can work around this by using a Decorator, but we'd like to avoid it if possible.

babenkoivan commented 2 years ago

Hey @marcelopm, what ES version do you use? I can surely do it, but only in the new version 4.0, which only supports ES8. I'm not planning to support the previous version due to a lack of time.

Side note: have you considered creating a custom MakeSearchable job and then replacing the default one in the service provider?

function boot() 
{
     Scout::$makeSearchableJob = MyMakeSearchable::class;
}
sk33wiff commented 2 years ago

That's great, thanks for considering it @babenkoivan

We do that already. As part of the solution we came up with, we created a custom MakeSearchable job and overridden the handle() method so that we can pass the current job instance to the update method, as below:

        $this
            ->models
            ->first()
            ->searchableUsing()
            ->update($this->models, $this);

We created also other job classes that extends it.

This allow us to dispatch specific MakeSearchable alike jobs and, more importantly, decide how we want to handle them during (re)indexing of models' data, as mentioned initially.

babenkoivan commented 2 years ago

Hi @marcelopm, just FYI: Engine is now non-final since v4.0.2.