ElasticSearch 协程客户端组件

协程版 ElasticSearch Client,对官方客户端的协程化移植

组件要求

  • easyswoole/spl: ^1.3
  • easyswoole/http-client: ^1.3
  • easyswoole/swoole-ide-helper: ^1.3

安装方法

composer require easyswoole/elasticsearch

仓库地址

easyswoole/elasticsearch

Client 用法

  1. <?php
  2. $config = new \EasySwoole\ElasticSearch\Config([
  3. 'host' => '127.0.0.1',
  4. 'port' => 9200
  5. ]);
  6. $elasticsearch = new \EasySwoole\ElasticSearch\ElasticSearch($config);
  7. go(function () use ($elasticsearch) {
  8. $bean = new \EasySwoole\ElasticSearch\RequestBean\Search();
  9. $bean->setIndex('my_index');
  10. $bean->setType('my_type');
  11. $bean->setBody(['query' => ['matchAll' => []]]);
  12. $response = $elasticsearch->client()->search($bean)->getBody();
  13. var_dump(json_decode($response, true));
  14. });

x-pack 验证

elasticsearch 开启 x-pack 登录验证时,只需在 config 中再传入用户名密码即可

  1. <?php
  2. $config = new \EasySwoole\ElasticSearch\Config([
  3. 'host' => '127.0.0.1',
  4. 'port' => 9200,
  5. 'username' => 'elastic',
  6. 'password' => '123456'
  7. ]);
  8. $elasticsearch = new \EasySwoole\ElasticSearch\ElasticSearch($config);

修改 http 为 https

  1. <?php
  2. $config = new \EasySwoole\ElasticSearch\Config([
  3. 'host' => '127.0.0.1',
  4. 'port' => 9200,
  5. 'username' => 'elastic',
  6. 'password' => '123456',
  7. 'scheme' => 'https'
  8. ]);
  9. $elasticsearch = new \EasySwoole\ElasticSearch\ElasticSearch($config);