The Elasticsearch PHP client offers a mode called "future" or "async" mode allows batch processing of requests (sent in parallel to the cluster).

$client = ClientBuilder::create()->build();
$futures = [];

for ($i = 0; $i < 1000; $i++) {
    $params = [
        'index'  => 'test',
        'id'     => $i,
        'client' => [
            'future' => 'lazy',
        ],
    ];

    $futures[] = $client->get($params);     //queue up the request
}

foreach ($futures as $future) {
    // access future's values, causing resolution if necessary
    echo $future['_source'];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Batch size defaults to 100 requests/batch. The batch size can be set via the max_handles setting when configuring the handler:

$handlerParams = [
    'max_handles' => 500,
];

$defaultHandler = ClientBuilder::defaultHandler($handlerParams);

$client = ClientBuilder::create()
            ->setHandler($defaultHandler)
            ->build();
1
2
3
4
5
6
7
8
9

Refs:

  • https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/future_mode.html

Powered by VuePress
Copyright 2010-2024 Thai Nguyen Hung all rights reserved.