|
| 1 | +# Laravel-OpenSearch |
| 2 | + |
| 3 | +**This package has been built off the back of the original [Elasticsearch version](https://github.com/pdphilip/laravel-elasticsearch) of this package since v3.11.0** |
| 4 | + |
| 5 | +### An OpenSearch implementation of Laravel's Eloquent ORM |
| 6 | + |
| 7 | +This package extends Laravel's Eloquent model and query builder with seamless integration of OpenSearch functionalities. Designed to feel native to Laravel, this package enables you to work with Eloquent models while leveraging the |
| 8 | +powerful search and analytics capabilities of OpenSearch. |
| 9 | + |
| 10 | +```php |
| 11 | +$logs = UserLog::where('type', UserLogType::LOGIN)->where('created_at','>=',Carbon::now()->subDays(30))->get(); |
| 12 | +``` |
| 13 | + |
| 14 | +### Read the [Documentation](https://elasticsearch.pdphilip.com/) |
| 15 | + |
| 16 | +Note: This package will receive its own version of this documentation. In the meantime, you can reference the Elasticsearch documentation and replace all namespaces `PDPhilip\Elasticsearch\...` with `PDPhilip\OpenSearch\...` |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Installation |
| 21 | + |
| 22 | +**Laravel 11.x (main):** |
| 23 | + |
| 24 | +```bash |
| 25 | +composer require pdphilip/opensearch |
| 26 | +``` |
| 27 | + |
| 28 | +## Configuration |
| 29 | + |
| 30 | +1. Set up your `.env` with the following OpenSearch settings: |
| 31 | + |
| 32 | +```ini |
| 33 | +OS_HOSTS="http://opensearch:9200" |
| 34 | +OS_USERNAME= |
| 35 | +OS_PASSWORD= |
| 36 | +OS_INDEX_PREFIX=my_app |
| 37 | + |
| 38 | +OS_SIG_V4_PROVIDER= |
| 39 | +OS_SIG_V4_REGION= |
| 40 | +OS_SIG_V4_SERVICE= |
| 41 | + |
| 42 | +OS_SSL_CERT= |
| 43 | +OS_SSL_CERT_PASSWORD= |
| 44 | +OS_SSL_KEY= |
| 45 | +OS_SSL_KEY_PASSWORD= |
| 46 | + |
| 47 | +OS_OPT_VERIFY_SSL=true |
| 48 | +OS_OPT_RETRIES= |
| 49 | +OS_OPT_SNIFF_ON_START= |
| 50 | +OS_OPT_PORT_HOST_HEADERS= |
| 51 | +``` |
| 52 | + |
| 53 | +For multiple nodes, pass in as comma-separated: |
| 54 | + |
| 55 | +```ini |
| 56 | +ES_HOSTS="http://opensearch-node1:9200,http://opensearch-node2:9200,http://opensearch-node3:9200" |
| 57 | +``` |
| 58 | + |
| 59 | +2. In `config/database.php`, add the opensearch connection: |
| 60 | + |
| 61 | +```php |
| 62 | +'opensearch' => [ |
| 63 | + 'driver' => 'opensearch', |
| 64 | + 'hosts' => explode(',', env('OS_HOSTS', 'http://localhost:9200')), |
| 65 | + 'basic_auth' => [ |
| 66 | + 'username' => env('OS_USERNAME', ''), |
| 67 | + 'password' => env('OS_PASSWORD', ''), |
| 68 | + ], |
| 69 | + 'sig_v4' => [ |
| 70 | + 'provider' => env('OS_SIG_V4_PROVIDER'), |
| 71 | + 'region' => env('OS_SIG_V4_REGION'), |
| 72 | + 'service' => env('OS_SIG_V4_SERVICE'), |
| 73 | + ], |
| 74 | + 'ssl' => [ |
| 75 | + 'cert' => env('OS_SSL_CERT', ''), |
| 76 | + 'cert_password' => env('OS_SSL_CERT_PASSWORD', ''), |
| 77 | + 'key' => env('OS_SSL_KEY', ''), |
| 78 | + 'key_password' => env('OS_SSL_KEY_PASSWORD', ''), |
| 79 | + ], |
| 80 | + 'index_prefix' => env('OS_INDEX_PREFIX', false), |
| 81 | + 'options' => [ |
| 82 | + 'ssl_verification' => env('OS_OPT_VERIFY_SSL', true), |
| 83 | + 'retires' => env('OS_OPT_RETRIES'), |
| 84 | + 'sniff_on_start' => env('OS_OPT_SNIFF_ON_START'), |
| 85 | + 'port_in_host_header' => env('OS_OPT_PORT_HOST_HEADERS'), |
| 86 | + ], |
| 87 | + 'query_log' => [ |
| 88 | + 'index' => false, //Or provide a name for the logging index ex: 'laravel_query_logs' |
| 89 | + 'error_only' => true, //If false, then all queries are logged if the query_log index is set |
| 90 | + ], |
| 91 | +], |
| 92 | +``` |
| 93 | + |
| 94 | +### 3. If packages are not autoloaded, add the service provider: |
| 95 | + |
| 96 | +For **Laravel 10 and below**: |
| 97 | + |
| 98 | +```php |
| 99 | +//config/app.php |
| 100 | +'providers' => [ |
| 101 | + ... |
| 102 | + ... |
| 103 | + PDPhilip\OpenSearch\OpenSearchServiceProvider::class, |
| 104 | + ... |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +For **Laravel 11**: |
| 109 | + |
| 110 | +```php |
| 111 | +//bootstrap/providers.php |
| 112 | +<?php |
| 113 | +return [ |
| 114 | + App\Providers\AppServiceProvider::class, |
| 115 | + PDPhilip\OpenSearch\OpenSearchServiceProvider::class, |
| 116 | +]; |
| 117 | +``` |
| 118 | + |
| 119 | +Now, you're all set to use OpenSearch with Laravel as if it were native to the framework. |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +# Documentation Links |
| 124 | + |
| 125 | +## Getting Started |
| 126 | + |
| 127 | +- [Installation](https://elasticsearch.pdphilip.com/#installation) |
| 128 | +- [Configuration](https://elasticsearch.pdphilip.com/#configuration) |
| 129 | + |
| 130 | +## Eloquent |
| 131 | + |
| 132 | +- [The Base Model](https://elasticsearch.pdphilip.com/the-base-model) |
| 133 | +- [Querying Models](https://elasticsearch.pdphilip.com/querying-models) |
| 134 | +- [Saving Models](https://elasticsearch.pdphilip.com/saving-models) |
| 135 | +- [Deleting Models](https://elasticsearch.pdphilip.com/deleting-models) |
| 136 | +- [Ordering and Pagination](https://elasticsearch.pdphilip.com/ordering-and-pagination) |
| 137 | +- [Distinct and GroupBy](https://elasticsearch.pdphilip.com/distinct) |
| 138 | +- [Aggregations](https://elasticsearch.pdphilip.com/aggregation) |
| 139 | +- [Chunking](https://elasticsearch.pdphilip.com/chunking) |
| 140 | +- [Nested Queries](https://elasticsearch.pdphilip.com/nested-queries) **New in Version 3** |
| 141 | +- [Elasticsearch Specific Queries](https://elasticsearch.pdphilip.com/es-specific) |
| 142 | +- [Full-Text Search](https://elasticsearch.pdphilip.com/full-text-search) |
| 143 | +- [Dynamic Indices](https://elasticsearch.pdphilip.com/dynamic-indices) |
| 144 | + |
| 145 | +## Relationships |
| 146 | + |
| 147 | +- [Elasticsearch to Elasticsearch](https://elasticsearch.pdphilip.com/es-es) |
| 148 | +- [Elasticsearch to MySQL](https://elasticsearch.pdphilip.com/es-mysql) |
| 149 | + |
| 150 | +## Schema/Index |
| 151 | + |
| 152 | +- [Migrations](https://elasticsearch.pdphilip.com/migrations) |
| 153 | +- [Re-indexing Process](https://elasticsearch.pdphilip.com/re-indexing) |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | + |
| 158 | +--- |
0 commit comments