Skip to content

Conditional Indexing

Not everything deserves an index entry. Tell ElasticLens what to skip.

By default, every record gets indexed. Override excludeIndex() on your Base Model to change that:

class User extends Model
{
use Indexable;
public function excludeIndex(): bool
{
return $this->status === 'banned';
}
}

Return true and the record is skipped. This applies to both bulk builds (lens:build) and automatic syncs via observers.

During builds, ElasticLens calls excludeIndex() on each record. When it returns true:

  • The record is skipped, no index entry created
  • Build state is marked as skipped (not failed)
  • If the record was previously indexed, the stale entry remains until the next full build

When you add or change excludeIndex() conditions, previously indexed records that now qualify for exclusion still have entries. Run a full rebuild:

Terminal window
php artisan lens:build User

The build skips excluded records and removes stale entries for records that no longer qualify.

lens:health accounts for exclusions. Skipped records are tracked separately from errors, so your health report stays accurate.

Terminal window
php artisan lens:health User