Skip to content

This content is for v3.1. Switch to the latest version for up-to-date documentation.

Getting Started

ElasticLens for Laravel is a package that uses Laravel-Elasticsearch to create and sync a searchable index of your SQL models.

Latest Stable VersionGitHub Tests Action StatusGitHub Code Style Action StatusMonthly Downloads

ElasticLens, like scout, is a package that allows you to do full text search on your SQL models.

ElasticLens integrates directly with the Laravel-Elasticsearch package, creating a dedicated Index Model that is fully accessible and automatically synced with your SQL Base Model

  • Laravel 10/11/12
  • Elasticsearch 8.x

Before you start, you’ll need to configure your Elasticsearch connection for Laravel. See the Laravel-Elasticsearch Getting Started Guide for full setup instructions.

Terminal window
composer require pdphilip/elasticlens

Publish the config file and run the migrations with:

Terminal window
php artisan lens:install

Run the migrations to create the index build and migration logs indexes:

Terminal window
php artisan migrate

lens:install will publish the config file to config/elasticlens.php and create the migration files build and migration logs.

You can customize the configuration in config/elasticlens.php

config/elasticlens.php
return [
'database' => 'elasticsearch',
'queue' => null, // Set queue to use for dispatching index builds, ex: default, high, low, etc.
// Watchers map changes to a given model to tigger an index build
// By default the base model is observed and will trigger an index build
// In some cases you may want to observe a different model
// For example when a relation is updated
'watchers' => [
// \App\Models\Profile::class => [
// \App\Models\Indexes\IndexedUser::class,
// ],
],
'index_build_state' => [
'enabled' => true, // Recommended to keep this enabled
'log_trim' => 2, // If null, the logs field will be empty
],
'index_migration_logs' => [
'enabled' => true, // Recommended to keep this enabled
],
'namespaces' => [
'App\Models' => 'App\Models\Indexes',
],
'index_paths' => [
'app/Models/Indexes/' => 'App\Models\Indexes',
],
];

The Index Model acts as a separate Elasticsearch model managed by ElasticLens, yet you retain full control over it, just like any other Laravel model. In addition to working directly with the Index Model, ElasticLens offers tools for

  • Full-text searching of your Base Models using the full feature set of the Larevel-Elasticsearch package
  • Mapping fields (with embedded relationships) during the build process
  • Define Index Model migrations.
  • CLI tools to view sync status and manage your Index Models

For Example, a base User Model will sync with an Elasticsearch IndexedUser Model that provides all the features from Laravel-Elasticsearch to search your base User Models