This content is for v3.9. Switch to the latest version for up-to-date documentation.
Getting Started
An Elasticsearch Implementation of Laravel’s Eloquent ORM
Section titled “An Elasticsearch Implementation of Laravel’s Eloquent ORM”This package seamlessly integrates Elasticsearch functionalities into Laravel’s Eloquent model and query builder, making it feel native to Laravel. This enables you to utilize Eloquent models while leveraging the powerful search and analytics capabilities of Elasticsearch.
Interested in the OpenSearch version of this package? Visit Github.
Section titled “Interested in the OpenSearch version of this package? Visit Github.”Installation - 3.x
Section titled “Installation - 3.x”Laravel 9
Section titled “Laravel 9”- Elasticsearch 8.x
composer require pdphilip/elasticsearch:~3.9
Laravel 8
Section titled “Laravel 8”- Elasticsearch 8.x
composer require pdphilip/elasticsearch:~3.8
Configuration Guide
Section titled “Configuration Guide”-
Set up your
.env
with the following Elasticsearch settings:ES_AUTH_TYPE=httpES_HOSTS="http://localhost:9200"ES_USERNAME=ES_PASSWORD=ES_CLOUD_ID=ES_API_ID=ES_API_KEY=ES_SSL_CA=ES_INDEX_PREFIX=my_app# prefix will be added to all indexes created by the package with an underscore# ex: my_app_user_logs for UserLog.php modelES_SSL_CERT=ES_SSL_CERT_PASSWORD=ES_SSL_KEY=ES_SSL_KEY_PASSWORD=# OptionsES_OPT_ID_SORTABLE=falseES_OPT_VERIFY_SSL=trueES_OPT_RETRIES=ES_OPT_META_HEADERS=trueES_ERROR_INDEX=For multiple nodes, pass in as comma-separated:
ES_HOSTS="http://es01:9200,http://es02:9200,http://es03:9200"Example cloud config .env: (Click to expand)
ES_AUTH_TYPE=cloudES_HOSTS="https://xxxxx-xxxxxx.es.europe-west1.gcp.cloud.es.io:9243"ES_USERNAME=elasticES_PASSWORD=XXXXXXXXXXXXXXXXXXXXES_CLOUD_ID=XXXXX:ZXVyb3BlLXdl.........SQwYzM1YzU5ODI5MTE0NjQ3YmEyNDZlYWUzOGNkN2Q1Yg==ES_API_ID=ES_API_KEY=ES_SSL_CA=ES_INDEX_PREFIX=my_appES_ERROR_INDEX= -
In
config/database.php
, add the elasticsearch connection:'elasticsearch' => ['driver' => 'elasticsearch','auth_type' => env('ES_AUTH_TYPE', 'http'), //http or cloud'hosts' => explode(',', env('ES_HOSTS', 'http://localhost:9200')),'username' => env('ES_USERNAME', ''),'password' => env('ES_PASSWORD', ''),'cloud_id' => env('ES_CLOUD_ID', ''),'api_id' => env('ES_API_ID', ''),'api_key' => env('ES_API_KEY', ''),'ssl_cert' => env('ES_SSL_CA', ''),'ssl' => ['cert' => env('ES_SSL_CERT', ''),'cert_password' => env('ES_SSL_CERT_PASSWORD', ''),'key' => env('ES_SSL_KEY', ''),'key_password' => env('ES_SSL_KEY_PASSWORD', ''),],'index_prefix' => env('ES_INDEX_PREFIX', false),'options' => ['bypass_map_validation' => env('ES_OPT_BYPASS_MAP_VALIDATION', false), // Skips validation when looking for fields that require a none text subfield.'insert_chunk_size' => env('ES_OPT_INSERT_CHUNK_SIZE', 1000), // Represents how large each bulk insert chunk should be'ssl_verification' => env('ES_OPT_VERIFY_SSL', true),'retires' => env('ES_OPT_RETRIES', null),'meta_header' => env('ES_OPT_META_HEADERS', true),],'error_log_index' => env('ES_ERROR_INDEX', false),], -
If packages are not autoloaded, add the service provider: For Laravel 10 and below:
config/app.php 'providers' => [......PDPhilip\Elasticsearch\ElasticServiceProvider::class,...For Laravel 11:
bootstrap/providers.php <?phpreturn [App\Providers\AppServiceProvider::class,PDPhilip\Elasticsearch\ElasticServiceProvider::class,];
This setup prepares you to fully utilize Elasticsearch within Laravel, treating it as an integral part of the framework.
Package versions
Section titled “Package versions”Elasticsearch 8.x
Section titled “Elasticsearch 8.x”Laravel Version | Command | Maintenance |
---|---|---|
Laravel 10/11/12 | composer require pdphilip/elasticsearch | ✅ Active |
Laravel 10 & 11 (v4) | composer require pdphilip/elasticsearch:~4 | 🛠️ LTS |
Laravel 9 | composer require pdphilip/elasticsearch:~3.9 | 🛠️ LTS |
Laravel 8 | composer require pdphilip/elasticsearch:~3.8 | 🛠️ LTS |
Laravel 7.x | composer require pdphilip/elasticsearch:~2.7 | ❌ EOL |
Laravel 6.x (5.8) | composer require pdphilip/elasticsearch:~2.6 | ❌ EOL |
Elasticsearch 7.x
Section titled “Elasticsearch 7.x”Laravel Version | Command | Maintenance |
---|---|---|
Laravel 9.x | composer require pdphilip/elasticsearch:~1.9 | ❌ EOL |
Laravel 8.x | composer require pdphilip/elasticsearch:~1.8 | ❌ EOL |
Laravel 7.x | composer require pdphilip/elasticsearch:~1.7 | ❌ EOL |
Laravel 6.x (5.8) | composer require pdphilip/elasticsearch:~1.6 | ❌ EOL |