Skip to content
SScout Redis Search
ESC

    Guides

    Indexing

    Make models searchable, import existing records, and keep Redis documents in sync.


    A searchable model

    use Illuminate\Database\Eloquent\Model;
    use Laravel\Scout\Searchable;
    
    final class Article extends Model
    {
        use Searchable;
    
        public function searchableAs(): string
        {
            return 'articles';
        }
    
        public function toSearchableArray(): array
        {
            return [
                'id' => (string) $this->getKey(),
                'title' => $this->title,
                'body' => $this->body,
                'category' => $this->category,
                'published_at' => $this->published_at?->timestamp,
            ];
        }
    }

    The array keys must match the fields in the articles schema. Scalar values are stored in a Redis hash; arrays and objects are JSON encoded.

    The model’s Scout key is always stored in __scout_key, even if it is not in the model’s toSearchableArray() result.

    Create the index

    Create an index from the configured schema, then import records that already exist in the database:

    php artisan scout:index articles
    php artisan scout:import "App\Models\Article"

    Use Scout’s normal searchable() and unsearchable() methods for individual records:

    $article->searchable();
    $article->unsearchable();

    Updates overwrite the existing Redis hash, so re-indexing a changed model is safe and idempotent.

    Queueing

    In write-heavy applications, push indexing onto Scout’s queue:

    SCOUT_QUEUE=true

    Run a worker and remember that a queued document may not be searchable until the job has completed:

    php artisan queue:work

    Flush and rebuild

    Flush a model’s indexed records with Scout’s command:

    php artisan scout:flush "App\Models\Article"

    This driver drops and recreates the configured index, leaving it ready for a new import. After a schema change, use scout:delete-index, scout:index, and scout:import explicitly so the operation is easy to observe in deployment logs.