A Laravel integration for the official OpenSearch PHP client.
Install the package with Composer:
composer require directorytree/opensearch-clientPublish the configuration file:
php artisan vendor:publish --provider="DirectoryTree\OpenSearchClient\OpenSearchClientServiceProvider"The published config/opensearch.client.php file defines the default connection and any named OpenSearch connections:
return [
'default' => env('OPENSEARCH_CONNECTION', 'default'),
'connections' => [
'default' => [
'base_uri' => env('OPENSEARCH_HOST', 'http://localhost:9200'),
],
],
];Each connection is passed directly to OpenSearch\GuzzleClientFactory.
Resolve DirectoryTree\OpenSearchClient\ClientBuilderInterface from the container to build OpenSearch clients:
namespace App\Console\Commands;
use DirectoryTree\OpenSearchClient\ClientBuilderInterface;
use Illuminate\Console\Command;
class CreateIndex extends Command
{
protected $signature = 'create:index {name}';
protected $description = 'Creates an index';
public function handle(ClientBuilderInterface $clientBuilder): void
{
$client = $clientBuilder->default();
$client->indices()->create([
'index' => $this->argument('name'),
]);
}
}You can also resolve named connections:
$client = $clientBuilder->connection('write');