Skip to content

Add opt-in unique indexing jobs to prevent Scout reindexing already queued models#996

Merged
taylorotwell merged 3 commits into
laravel:11.xfrom
stevebauman:unique-searchable
Jun 8, 2026
Merged

Add opt-in unique indexing jobs to prevent Scout reindexing already queued models#996
taylorotwell merged 3 commits into
laravel:11.xfrom
stevebauman:unique-searchable

Conversation

@stevebauman

Copy link
Copy Markdown
Contributor

Description:

I recently discovered that every time $model->save() is called on a searchable model (regardless of whether the model actually changed), Scout dispatches a MakeSearchable job. In write-heavy applications this means the queue fills with tons of MakeSearchable jobs for the same records, causing the same documents to be re-indexed more often than necessary. The same applies to RemoveFromSearch.

Because these jobs use SerializesModels, a queued job always re-fetches the latest model state when it runs -- so collapsing redundant jobs for the same records has no effect on what ultimately gets indexed. It just removes wasted work.

This PR adds opt-in unique variants of both indexing jobs:

  • Laravel\Scout\Jobs\MakeSearchableUnique
  • Laravel\Scout\Jobs\RemoveFromSearchUnique

Both extend their base job and implement ShouldBeUniqueUntilProcessing, sharing a UniqueByScoutKeys trait that derives the job's unique identity from the queueable model class plus its (sorted) Scout keys.

Usage:

Developers can opt-in by pointing Scout at the unique jobs (ex. in a service provider):

use Laravel\Scout\Scout;
use Laravel\Scout\Jobs\MakeSearchableUnique;
use Laravel\Scout\Jobs\RemoveFromSearchUnique;

Scout::makeSearchableUsing(MakeSearchableUnique::class);
Scout::removeFromSearchUsing(RemoveFromSearchUnique::class);

I've made this opt-in since this ShouldBeUnique assumes that the developer has a default cache store set up.

Let me know your thoughts! ❤️

@taylorotwell taylorotwell merged commit 763b25b into laravel:11.x Jun 8, 2026
28 checks passed
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.

2 participants