Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed AggregatorObserver delete & forceDelete Methods to respect Aggregator syncing status #301

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

goldmerc
Copy link

@goldmerc goldmerc commented Feb 7, 2022

Q A
Bug fix? yes
New feature? no
BC breaks? no, I don't think so
Related Issue Fix #285
Need Doc update no

Describe your change

This PR changes the AggregatorObserver delete and forceDelete methods to check syncingDisabledFor using the Aggregator class rather than the Model class. This brings these methods inline with the behaviour of the saved method.

It also updates the delete method to reflect changes made in the parent ModelObserver class.

What problem is this fixing?

If you want to use an aggregator but don't want to index individually the models being aggregated, the algolia/scout-extended docs recommend to simply disable syncing for those models, eg...

Laravel\Scout\ModelObserver::disableSyncingFor(Article::class);
Laravel\Scout\ModelObserver::disableSyncingFor(Event::class);

However, the AggregatorObserver delete and forceDelete methods were both checking syncingDisabledFor using the Model class rather than the Aggregator class. This meant that if you deleted a model which had syncing disabled (because you only wanted an Aggregated Index and not individual indexes), it was not deleted from the aggregated index.

Conversely, the saved method does check syncingDisabledFor with the Aggregator class, which I believe should be the correct behaviour.

The Delete and ForceDelete methods were checking whether syncing was disabled for the model rather than the searchable. This fixes that and updates the Delete method to reflect changes in the parent ModelObserver class.
Copy link
Contributor

@DevinCodes DevinCodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @goldmerc ,

Thank you for this PR! I left some comments, please let me know what you think and if you have any questions.

@@ -85,12 +85,14 @@ public function saved($model): void
*/
public function deleted($model): void
{
if (static::syncingDisabledFor($model)) {
if (!$model->wasSearchableBeforeDelete()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check here won't always work. Aggregators can contain classes that don't have the Searchable trait on them. Without this trait, this check will fail because the method is not defined. We should probably add a check on whether or not the model includes the Searchable trait (you can see how here).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that. Are you sure aggregators work if the underlying models don't have the Searchable trait? The docs for scout extended recommend disabling syncing for models if you don't want them to create their own individual model indexes. If what you say is correct, that wouldn't be needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is something that's possible to do. This is not well documented, but the tests ensure this works as well (in AggregatorTest.php, we use the News aggregator that includes the Post model, which doesn't include the Searchable trait).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. In that case, maybe best to check if the method exists rather than whether the Searchable trait is being used. That way an aggregated class without the Searchable trait can still implement a wasSearchableBeforeDelete method if wanted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that works for me too! 🙂

src/Searchable/AggregatorObserver.php Outdated Show resolved Hide resolved
src/Searchable/AggregatorObserver.php Outdated Show resolved Hide resolved
@kellerjmrtn
Copy link

Having the same issue as #285, I'd like to bump this PR. We've patched it in our application with a very similar fix (simply removing the static::syncingDisabledFor($model) check and adding a static::syncingDisabledFor($aggregator) check in the loop), but an official fix would be helpful

Do you need someone to take it over? I could create my own PR if desired

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AggregatorObserver delete method shouldn't check individual model syncing status
3 participants