pdphilip / elasticsearch
An Elasticsearch implementation of Laravel's Eloquent ORM
Installs: 23 100
Dependents: 2
Suggesters: 0
Security: 0
Stars: 94
Watchers: 5
Forks: 17
Open Issues: 1
Requires
- php: ^8.2
- elasticsearch/elasticsearch: ^8.15
- illuminate/container: ^10.0|^11.0
- illuminate/database: ^10.0|^11.0
- illuminate/events: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- doctrine/coding-standard: 12.0.x-dev
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- mockery/mockery: ^1.4.4
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^3
- pestphp/pest-plugin-arch: ^3
- pestphp/pest-plugin-laravel: ^3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- dev-main
- v4.5.0
- v4.4.1
- v4.4.0
- v4.3.0
- v4.2.0
- v4.1.1
- v4.1.0
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.11.0
- 3.10.x-dev
- v3.10.0
- 3.9.x-dev
- v3.9.4
- v3.9.3
- v3.9.2
- v3.9.1
- v3.9.0
- 3.8.x-dev
- v3.8.4
- v3.8.3
- v3.8.2
- v3.8.1
- v3.8.0
- v2.10.7
- v2.10.6
- v2.10.5
- v2.10.4
- v2.10.3
- v2.10.2
- v2.10.1
- v2.10.0
- 2.9.x-dev
- v2.9.9
- v2.9.8
- v2.9.7
- v2.9.6
- v2.9.5
- v2.9.4
- v2.9.3
- v2.9.2
- v2.9.1
- v2.9.0
- 2.8.x-dev
- v2.8.7
- v2.8.6
- v2.8.5
- v2.8.4
- v2.8.3
- v2.8.2
- v2.8.1
- v2.8.0
- 2.7.x-dev
- v2.7.7
- v2.7.6
- v2.7.5
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- 2.6.x-dev
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- 1.9.x-dev
- v1.9.3
- v1.9.2
- v1.9.1
- v1.9.0
- 1.8.x-dev
- v1.8.2
- v1.8.1
- v1.8.0
- 1.7.x-dev
- v1.7.1
- v1.7.0
- 1.6.x-dev
- v1.6.2
- v1.6.1
- v1.6.0
- v0.9.2
- v0.9.1
- 0.8.x-dev
- v0.8.2
- v0.8.1
- v0.8-alpha
- 0.7.x-dev
- v0.7.2
- v0.7.1
- v0.7-alpha
- 0.6.x-dev
- v0.6.2
- v0.6.1
- v0.6-alpha
- dev-dev-main
- dev-github-actions
- dev-collection-exp
- dev-dev-l9
- dev-dev-l8
This package is auto-updated.
Last update: 2024-11-17 21:29:58 UTC
README
Laravel-Elasticsearch
Laravel-Elasticsearch: An Elasticsearch implementation of Laravel's Eloquent ORM
This package extends Laravel's Eloquent model and query builder with seamless integration of Elasticsearch functionalities. Designed to feel native to Laravel, this package enables you to work with Eloquent models while leveraging the powerful search and analytics capabilities of Elasticsearch.
Examples:
$logs = UserLog::where('created_at','>=',Carbon::now()->subDays(30))->get();
$updates = UserLog::where('status', 1)->update(['status' => 4]);
$updates = UserLog::where('status', 1)->paginate(50);
$profiles = UserProfile::whereIn('country_code',['US','CA'])->orderByDesc('last_login')->take(10)->get();
$deleted = UserProfile::where('state','unsubscribed')->where('updated_at','<=',Carbon::now()->subDays(90))->delete();
$search = UserProfile::phrase('loves espressos')->highlight()->search();
Read the Documentation
Using OpenSearch? Github
Package Tests
Installation
Maintained versions (Elasticsearch 8.x):
Laravel 10.x & 11.x (main):
composer require pdphilip/elasticsearch
Unmaintained versions (Elasticsearch 8.x):
Unmaintained versions (Elasticsearch 7.x):
Configuration
- Set up your
.env
with the following Elasticsearch settings:
ES_AUTH_TYPE=http ES_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 model ES_SSL_CERT= ES_SSL_CERT_PASSWORD= ES_SSL_KEY= ES_SSL_KEY_PASSWORD= # Options ES_OPT_ID_SORTABLE=false ES_OPT_VERIFY_SSL=true ES_OPT_RETRIES= ES_OPT_META_HEADERS=true ES_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=cloud ES_HOSTS="https://xxxxx-xxxxxx.es.europe-west1.gcp.cloud.es.io:9243" ES_USERNAME=elastic ES_PASSWORD=XXXXXXXXXXXXXXXXXXXX ES_CLOUD_ID=XXXXX:ZXVyb3BlLXdl.........SQwYzM1YzU5ODI5MTE0NjQ3YmEyNDZlYWUzOGNkN2Q1Yg== ES_API_ID= ES_API_KEY= ES_SSL_CA= ES_INDEX_PREFIX=my_app ES_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' => [ 'allow_id_sort' => env('ES_OPT_ID_SORTABLE', false), '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 set will log ES errors to this index, ex: 'laravel_es_errors' ],
3. If packages are not autoloaded, add the service provider:
For Laravel 11:
//bootstrap/providers.php <?php return [ App\Providers\AppServiceProvider::class, PDPhilip\Elasticsearch\ElasticServiceProvider::class, ];
For Laravel 10 and below:
//config/app.php 'providers' => [ ... ... PDPhilip\Elasticsearch\ElasticServiceProvider::class, ...
Now, you're all set to use Elasticsearch with Laravel as if it were native to the framework.
Documentation Links
Getting Started
Eloquent
- The Base Model
- Querying Models
- Saving Models
- Deleting Models
- Ordering and Pagination
- Distinct and GroupBy
- Aggregations
- Chunking
- Nested Queries
- Elasticsearch Specific Queries
- Full-Text Search
- Dynamic Indices
Relationships
Schema/Index
Misc
New in Version 4
(and 3.9.1/3.8.1)
- Search Highlighting
- whereTimestamp()
- Raw Aggregation
- Updated Error Handling
- Chunk Upgrade: Point In Time (PIT)
New in Version 3
Nested Queries (see)
- Nested Object Queries
- Order By Nested
- Filter Nested Values: Filters nested values of the parent collection
New Where
clauses
- Phrase Matching: The enhancement in phrase matching capabilities allows for refined search precision, facilitating the targeting of exact word sequences within textual fields, thus improving search specificity and relevance.
- Exact Matching: Strengthening exact match queries enables more stringent search criteria, ensuring the retrieval of documents that precisely align with specified parameters.
Sorting Enhancements
- Ordering with ES features: Includes modes and missing values for sorting fields.
- Order by Geo Distance
Saving Updates
Grouped Queries
- Grouped Queries: Queries can be grouped allowing multiple conditions to be nested within a single query block.