|
1 | | -<img align="left" width="70" height="70" src="https://cdn.snipform.io/pdphilip/elasticsearch/laravel-x-es.png"> |
| 1 | +<img align="left" width="50" height="50" src="https://cdn.snipform.io/pdphilip/elasticsearch/laravel-x-es.png"> |
2 | 2 |
|
3 | | -# Laravel x Elasticsearch |
| 3 | +## Elasticsearch implementation of Laravel's Eloquent ORM |
4 | 4 |
|
5 | | -This package extends Laravel's Eloquent model and query builder with seamless integration of Elasticsearch functionalities. Designed to feel native to Laravel, this plugin enables you to work with Eloquent models while leveraging the |
| 5 | +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 |
6 | 6 | powerful search and analytics capabilities of Elasticsearch. |
7 | 7 |
|
8 | 8 | ### Read the [Documentation](https://elasticsearch.pdphilip.com/) |
@@ -44,7 +44,96 @@ composer require pdphilip/elasticsearch |
44 | 44 | | Laravel 7.x | `composer require pdphilip/elasticsearch:~1.7` | ❌ | |
45 | 45 | | Laravel 6.x (5.8) | `composer require pdphilip/elasticsearch:~1.6` | ❌ | |
46 | 46 |
|
47 | | -Next, [Configuration](https://elasticsearch.pdphilip.com/#configuration) |
| 47 | +## Configuration |
| 48 | + |
| 49 | +1. Set up your `.env` with the following Elasticsearch settings: |
| 50 | + |
| 51 | +```ini |
| 52 | +ES_AUTH_TYPE=http |
| 53 | +ES_HOSTS="http://localhost:9200" |
| 54 | +ES_USERNAME= |
| 55 | +ES_PASSWORD= |
| 56 | +ES_CLOUD_ID= |
| 57 | +ES_API_ID= |
| 58 | +ES_API_KEY= |
| 59 | +ES_SSL_CERT= |
| 60 | +ES_INDEX_PREFIX=my_app |
| 61 | +# prefix will be added to all indexes created by the package with an underscore |
| 62 | +# ex: my_app_user_logs for UserLog.php model |
| 63 | +``` |
| 64 | + |
| 65 | +For multiple nodes, pass in as comma-separated: |
| 66 | + |
| 67 | +```ini |
| 68 | +ES_HOSTS="http://es01:9200,http://es02:9200,http://es03:9200" |
| 69 | +``` |
| 70 | + |
| 71 | +<details> |
| 72 | +<summary>Example cloud config .env: (Click to expand)</summary> |
| 73 | + |
| 74 | +```ini |
| 75 | +ES_AUTH_TYPE=cloud |
| 76 | +ES_HOSTS="https://xxxxx-xxxxxx.es.europe-west1.gcp.cloud.es.io:9243" |
| 77 | +ES_USERNAME=elastic |
| 78 | +ES_PASSWORD=XXXXXXXXXXXXXXXXXXXX |
| 79 | +ES_CLOUD_ID=XXXXX:ZXVyb3BlLXdl.........SQwYzM1YzU5ODI5MTE0NjQ3YmEyNDZlYWUzOGNkN2Q1Yg== |
| 80 | +ES_API_ID= |
| 81 | +ES_API_KEY= |
| 82 | +ES_SSL_CERT= |
| 83 | +``` |
| 84 | + |
| 85 | +</details> |
| 86 | + |
| 87 | + |
| 88 | +2. In `config/database.php`, add the elasticsearch connection: |
| 89 | + |
| 90 | +```php |
| 91 | +'elasticsearch' => [ |
| 92 | + 'driver' => 'elasticsearch', |
| 93 | + 'auth_type' => env('ES_AUTH_TYPE', 'http'), //http, cloud or api |
| 94 | + 'hosts' => explode(',', env('ES_HOSTS', 'http://localhost:9200')), |
| 95 | + 'username' => env('ES_USERNAME', ''), |
| 96 | + 'password' => env('ES_PASSWORD', ''), |
| 97 | + 'cloud_id' => env('ES_CLOUD_ID', ''), |
| 98 | + 'api_id' => env('ES_API_ID', ''), |
| 99 | + 'api_key' => env('ES_API_KEY', ''), |
| 100 | + 'ssl_cert' => env('ES_SSL_CERT', ''), |
| 101 | + 'index_prefix' => env('ES_INDEX_PREFIX', false), |
| 102 | + 'query_log' => [ |
| 103 | + 'index' => false, //Or provide a name for the logging index ex: 'laravel_query_logs' |
| 104 | + 'error_only' => true, //If false, then all queries are logged if the query_log index is set |
| 105 | + ], |
| 106 | +], |
| 107 | +``` |
| 108 | + |
| 109 | +### 3. If packages are not autoloaded, add the service provider: |
| 110 | + |
| 111 | +For **Laravel 10 and below**: |
| 112 | +```php |
| 113 | +//config/app.php |
| 114 | +'providers' => [ |
| 115 | + ... |
| 116 | + ... |
| 117 | + PDPhilip\Elasticsearch\ElasticServiceProvider::class, |
| 118 | + ... |
| 119 | + |
| 120 | +``` |
| 121 | + |
| 122 | +For **Laravel 11**: |
| 123 | +```php |
| 124 | +//bootstrap/providers.php |
| 125 | +<?php |
| 126 | +return [ |
| 127 | + App\Providers\AppServiceProvider::class, |
| 128 | + PDPhilip\Elasticsearch\ElasticServiceProvider::class, |
| 129 | +]; |
| 130 | +``` |
| 131 | + |
| 132 | +Now, you're all set to use Elasticsearch with Laravel as if it were native to the framework, ex: |
| 133 | + |
| 134 | +```php |
| 135 | +$products = Product::where('manufacturer.country', 'England')->take(10)->get(); |
| 136 | +``` |
48 | 137 |
|
49 | 138 | --- |
50 | 139 |
|
|
0 commit comments