friendsofcat / opensearch-client
Laravel 集成的官方 PHP OpenSearch 客户端
2.0.0
2023-04-25 14:39 UTC
Requires
- php: ^7.4 || ^8.0
- opensearch-project/opensearch-php: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- orchestra/testbench: ^7.5
- phpstan/phpstan: ^1.6
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-08-25 17:34:30 UTC
README
Laravel 集成的官方 PHP OpenSearch 客户端。
内容
兼容性
当前版本的 OpenSearch 客户端已与以下配置进行测试
- PHP 7.4-8.x
- Opensearch 2.x
安装
可以通过 Composer 安装库
composer require friendsofcat/opensearch-client
配置
要更改客户端设置,您需要首先发布配置文件
php artisan vendor:publish --provider="OpenSearch\Laravel\Client\ServiceProvider"
在新建的 config/opensearch.client.php
文件中,您可以定义默认连接名称。
return [ 'default' => env('OPENSEARCH_CONNECTION', 'default'), 'connections' => [ 'default' => [ 'hosts' => [ env('OPENSEARCH_HOST', 'localhost:9200'), ], // you can also set HTTP client options (which is Guzzle by default) as follows 'httpClientOptions' => [ 'timeout' => 2, ], ], ], ];
如果您需要更多控制客户端创建,您可以创建自己的客户端构建器
// see OpenSearch\Laravel\Client\ClientBuilder for the reference class MyClientBuilder implements OpenSearch\Laravel\Client\ClientBuilderInterface { public function default(): Client { // should return a client instance for the default connection } public function connection(string $name): Client { // should return a client instance for the connection with the given name } }
不要忘记在您的应用程序服务提供者中注册构建器
class MyAppServiceProvider extends Illuminate\Support\ServiceProvider { public function register() { $this->app->singleton(ClientBuilderInterface::class, MyClientBuilder::class); } }
使用
使用 OpenSearch\Laravel\Client\ClientBuilderInterface
获取客户端实例访问权限
namespace App\Console\Commands; use OpenSearch\Client; use OpenSearch\Laravel\Client\ClientBuilderInterface; use Illuminate\Console\Command; class CreateIndex extends Command { protected $signature = 'create:index {name}'; protected $description = 'Creates an index'; public function handle(ClientBuilderInterface $clientBuilder) { // get a client for the default connection $client = $clientBuilder->default(); // get a client for the connection with name "write" $client = $clientBuilder->connection('write'); $client->indices()->create([ 'index' => $this->argument('name') ]); } }