iwai / elasticsearch-guzzle5connection

关于此软件包的最新版本(1.0.1)没有可用的许可证信息。

1.0.1 2015-05-26 11:35 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:48:08 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Status

对 elasticsearch-php 的异步支持

注意!!!

  • 不支持的 ping 方法
  • 不支持的 exists 方法
  • 不支持的日志记录

安装

{
    "require": {
        "elasticsearch/elasticsearch": "~1.0",
        "iwai/elasticsearch-guzzle5connection": "~1.0"
    }
}

示例

透明的异步请求

use Elasticsearch\Client as ESClient;

$client = new ESClient([
    'hosts' => [ '127.0.0.1:9200' ],
    'connectionClass' => '\Iwai\Elasticsearch\Guzzle5Connection',
    'serializerClass' => '\Iwai\Elasticsearch\FutureSerializer'
]);

$response = $client->get([
    'index' => 'index_name',
    'type'  => 'type',
    'id'    => '1',
]);

echo $response['hits']['total'];

显式等待请求

use Elasticsearch\Client as ESClient;

$client = new ESClient([
    'hosts' => [ '127.0.0.1:9200' ],
    'connectionClass' => '\Iwai\Elasticsearch\Guzzle5Connection',
    'serializerClass' => '\Iwai\Elasticsearch\FutureSerializer'
]);

$future = $client->get([
    'index' => 'index_name',
    'type'  => 'type',
    'id'    => '1',
]);

$response = $future->wait();

echo $response['hits']['total'];

Promise 风格

use Elasticsearch\Client as ESClient;

$client = new ESClient([
    'hosts' => [ '127.0.0.1:9200' ],
    'connectionClass' => '\Iwai\Elasticsearch\Guzzle5Connection',
    'serializerClass' => '\Iwai\Elasticsearch\FutureSerializer'
]);

$future = $client->get([
    'index' => 'index_name',
    'type'  => 'type',
    'id'    => '1',
]);

$futureData->then(function ($response) {
    echo $response['hits']['total'];
});

与 RingPHP 一起

use React\EventLoop;
use WyriHaximus\React\RingPHP\HttpClientAdapter;
use Elasticsearch\Client as ESClient;

$loop  = EventLoop\Factory::create();

$client = new ESClient([
    'hosts' => [ '127.0.0.1:9200' ],
    'connectionClass' => '\Iwai\Elasticsearch\Guzzle5Connection', // required
    'serializerClass' => '\Iwai\Elasticsearch\FutureSerializer',  // required
    'connectionParams' => [ 'ringphp_handler' => new HttpClientAdapter($loop) ] // optional
]);

$futureData = $client->get([
    'index' => 'index_name',
    'type'  => 'type',
    'id'    => '1',
]);

$futureData->then(function ($response) {
    echo $response['hits']['total'];
});

$loop->run();