https://github.com/pdphilip/laravel-opensearch
An OpenSearch implementation of Laravel's Eloquent ORM
https://github.com/pdphilip/laravel-opensearch
eloquent-orm laravel opensearch
Last synced: 3 months ago
JSON representation
An OpenSearch implementation of Laravel's Eloquent ORM
- Host: GitHub
- URL: https://github.com/pdphilip/laravel-opensearch
- Owner: pdphilip
- License: mit
- Created: 2024-04-28T21:39:53.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-05T09:37:32.000Z (over 1 year ago)
- Last Synced: 2025-03-21T09:11:17.331Z (over 1 year ago)
- Topics: eloquent-orm, laravel, opensearch
- Language: PHP
- Homepage: https://opensearch.pdphilip.com/
- Size: 110 KB
- Stars: 29
- Watchers: 1
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README

# Laravel-OpenSearch
[](https://packagist.org/packages/pdphilip/opensearch)
[](https://github.com/pdphilip/laravel-opensearch/actions/workflows/run-tests.yml?query=branch%3Amain)
[](https://github.com/pdphilip/laravel-opensearch/actions/workflows/phpstan.yml?query=branch%3Amain++)
[](https://packagist.org/packages/pdphilip/opensearch)
> [OpenSearch](https://opensearch.net/) is a distributed, community-driven, Apache 2.0-licensed, 100% open-source search and analytics suite used for a broad set of use cases like real-time application monitoring, log analytics, and website
> search.
## An OpenSearch implementation of Laravel's Eloquent ORM
### The Power of OpenSearch with Laravel's Eloquent
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
powerful search and analytics capabilities of OpenSearch.
The Eloquent you already know:
```php
UserLog::where('created_at','>=',Carbon::now()->subDays(30))->get();
```
```php
UserLog::create([
'user_id' => '2936adb0-b10d-11ed-8e03-0b234bda3e12',
'ip' => '62.182.98.146',
'location' => [40.7185,-74.0025],
'country_code' => 'US',
'status' => 1,
]);
```
```php
UserLog::where('status', 1)->update(['status' => 4]);
```
```php
UserLog::where('status', 4)->orderByDesc('created_at')->paginate(50);
```
```php
UserProfile::whereIn('country_code',['US','CA'])
->orderByDesc('last_login')->take(10)->get();
```
```php
UserProfile::where('state','unsubscribed')
->where('updated_at','<=',Carbon::now()->subDays(90))->delete();
```
opensearch with Eloquent:
```php
UserProfile::searchTerm('Laravel')->orSearchTerm('opensearch')->get();
```
```php
UserProfile::searchPhrasePrefix('loves espressos and t')->highlight()->get();
```
```php
UserProfile::whereMatch('bio', 'PHP')->get();
```
```php
UserLog::whereGeoDistance('location', '10km', [40.7185,-74.0025])->get();
```
```php
UserProfile::whereFuzzy('description', 'qick brwn fx')->get();
```
Built in Relationships (even to SQL models):
```php
UserLog::where('status', 1)->orderByDesc('created_at')->with('user')->get();
```
---
# Read the [Documentation](https://opensearch.pdphilip.com/)
## Installation
**Laravel 10.x, 11.x & 12.x (main):**
```bash
composer require pdphilip/opensearch
```
| Laravel Version | Command | Maintained |
|--------------------|--------------------------------------------|------------|
| Laravel 10/11/12 | `composer require pdphilip/opensearch:~3 ` | ✅ |
| Laravel 10/11 (v2) | `composer require pdphilip/opensearch:~2 ` | ❌ EOL |
| Laravel 8 & 9 | `composer require pdphilip/opensearch:~1` | ❌ EOL |
## Configuration
1. Set up your `.env` with the following OpenSearch settings:
```ini
OS_HOSTS="http://opensearch:9200"
OS_USERNAME=
OS_PASSWORD=
OS_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
# AWS SigV4 Config:
OS_SIG_V4_PROVIDER=
OS_SIG_V4_REGION=
OS_SIG_V4_SERVICE=
# Cert Config:
OS_SSL_CERT=
OS_SSL_CERT_PASSWORD=
OS_SSL_KEY=
OS_SSL_KEY_PASSWORD=
# Optional Settings:
OS_OPT_VERIFY_SSL=true
OS_OPT_RETRIES=
OS_OPT_SNIFF_ON_START=
OS_OPT_PORT_HOST_HEADERS=
OS_OPT_ID_SORTABLE=false
OS_OPT_META_HEADERS=true
OS_OPT_BYPASS_MAP_VALIDATION=false
OS_OPT_DEFAULT_LIMIT=1000
```
For multiple nodes, pass in as comma-separated:
```ini
OS_HOSTS="http://opensearch-node1:9200,http://opensearch-node2:9200,http://opensearch-node3:9200"
```
2. In `config/database.php`, add the OpensSearch connection:
```php
'opensearch' => [
'driver' => 'opensearch',
'hosts' => explode(',', env('OS_HOSTS', 'http://localhost:9200')),
'basic_auth' => [
'username' => env('OS_USERNAME', ''),
'password' => env('OS_PASSWORD', ''),
],
'sig_v4' => [
'provider' => env('OS_SIG_V4_PROVIDER'),
'region' => env('OS_SIG_V4_REGION'),
'service' => env('OS_SIG_V4_SERVICE'),
],
'ssl' => [
'cert' => env('OS_SSL_CERT', ''),
'cert_password' => env('OS_SSL_CERT_PASSWORD', ''),
'key' => env('OS_SSL_KEY', ''),
'key_password' => env('OS_SSL_KEY_PASSWORD', ''),
],
'index_prefix' => env('OS_INDEX_PREFIX', false),
'options' => [
'bypass_map_validation' => env('OS_OPT_BYPASS_MAP_VALIDATION', false),
'ssl_verification' => env('OS_OPT_VERIFY_SSL', true),
'retires' => env('OS_OPT_RETRIES',null),
'sniff_on_start' => env('OS_OPT_SNIFF_ON_START',false),
'logging' => env('OS_OPT_LOGGING', false),
'port_in_host_header' => env('OS_OPT_PORT_HOST_HEADERS',false),
'default_limit' => env('OS_OPT_DEFAULT_LIMIT', 1000),
'allow_id_sort' => env('OS_OPT_ID_SORTABLE', false),
],
],
```
### 3. If packages are not autoloaded, add the service provider:
For **Laravel 10 and below**:
```php
//config/app.php
'providers' => [
...
...
PDPhilip\OpenSearch\OpenSearchServiceProvider::class,
...
```
For **Laravel 11**:
```php
//bootstrap/providers.php