Conditional Indexing
Not everything deserves an index entry. Tell ElasticLens what to skip.
The excludeIndex() Method
Section titled “The excludeIndex() Method”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.
How It Works
Section titled “How It Works”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
Cleaning Up Stale Records
Section titled “Cleaning Up Stale Records”When you add or change excludeIndex() conditions, previously indexed records that now qualify for exclusion still have entries. Run a full rebuild:
php artisan lens:build UserThe build skips excluded records and removes stale entries for records that no longer qualify.
Health Check Accounting
Section titled “Health Check Accounting”lens:health accounts for exclusions. Skipped records are tracked separately from errors, so your health report stays accurate.
php artisan lens:health User