set connect_timeout of elasticsearch php client
Asked Answered
O

6

5

i want to configure a a small timeout between my elasticsearch php client to the my elasticsearch server.

i tried to pass some parameters to the guzzle client but it seems this doesn't work. here is the code:

$params = array();
$params['hosts'] = $hosts;
$params['guzzleOptions']['connect_timeout'] = 2.0;
$params['guzzleOptions']['timeout'] = 2.0;
$this->elastica_obj = new Elasticsearch\Client($params);

i searched and found that the problem might occured because the timeout is set in the cURL layer (that is lower than the guzzle) (Limit connecting time with Guzzle HTTP PHP client)

i guess i need somehow to set CURLOPT_CONNECTTIMEOUT_MS parameter to the value i want (2000ms) but i don't see any good way to pass it through the elasticsearch php client.

Does someone knows how to do it?

Olli answered 25/8, 2014 at 17:21 Comment(0)
O
5

Thanks Zack, I tried it but it doesn't work.

I debugged the client and the way the parameters are being pass from through the guzzle to the curl handle.

the way i find to accomplish it is to pass this parameter to the Elasticsearch client

$params['guzzleOptions']['curl.options'][CURLOPT_CONNECTTIMEOUT] = 2.0;  // this applies 2 seconds connection_timeout

hope it is helping :)

Niv

Olli answered 27/8, 2014 at 8:50 Comment(0)
G
9

Since elasticsearch/elasticsearch v5.1.2, you also can use setConnectionParams() method which applies to all requests

use GuzzleHttp\RequestOptions;
use Elasticsearch\ClientBuilder;

ClientBuilder::create()
    ->setConnectionParams([
        'client' => [
            RequestOptions::TIMEOUT => 10,
            RequestOptions::CONNECT_TIMEOUT => 10,
        ],
    ])
    ->build();
Galipot answered 13/10, 2021 at 9:29 Comment(0)
O
5

Thanks Zack, I tried it but it doesn't work.

I debugged the client and the way the parameters are being pass from through the guzzle to the curl handle.

the way i find to accomplish it is to pass this parameter to the Elasticsearch client

$params['guzzleOptions']['curl.options'][CURLOPT_CONNECTTIMEOUT] = 2.0;  // this applies 2 seconds connection_timeout

hope it is helping :)

Niv

Olli answered 27/8, 2014 at 8:50 Comment(0)
S
3

For the latest version 2.x it's done in a different way. Citing Zach:

In ES-PHP 2.x the timeout is now specified per-request. See the documentation here: https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_per_request_configuration.html#_curl_timeouts

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

$params = [
    'index' => 'test',
    'type' => 'test',
    'id' => 1,
    'client' => [
        'timeout' => 10,        // ten second timeout
        'connect_timeout' => 10
    ]
];
$response = $client->get($params);

Though, it is not possible to do it on global level. Follow this issue for updates.

Saccharo answered 7/2, 2016 at 21:48 Comment(1)
Updated link to the documentaiton: elastic.co/guide/en/elasticsearch/client/php-api/7.x/…Scheelite
T
1

I'm using the 8.x Elasticsearch client, and I had to manually set the HTTP client to work around https://mcmap.net/q/1921424/-client-is-not-supported-for-custom-options.

For me, setting the timeout values in the Guzzle HTTP client was the answer.

$client = ClientBuilder::create()
    ->setHttpClient(new \GuzzleHttp\Client([
        'connect_timeout' => 10, // seconds
        'timeout' => 10 // https://docs.guzzlephp.org/en/latest/request-options.html
    ])) 
    ->setHosts(config('elasticsearch.hosts'))
    ->setApiKey(config('elasticsearch.api_key'))
    ->build();
Tormoria answered 12/7, 2023 at 8:14 Comment(0)
M
0

Assuming you mean Elasticsearch-PHP client (and not Elastica):

The guzzleOptions parameter accepts any Guzzle parameter, and follows the same array syntax that Guzzle uses. So you need to do:

$params = array();
$params['hosts'] = $hosts;
$params['guzzleOptions']['command.request_options']['connect_timeout'] = 2.0;
$params['guzzleOptions']['command.request_options']['timeout'] = 2.0;
$this->elastica_obj = new Elasticsearch\Client($params);

This will apply a 2s timeout to all requests sent through the client

There is a "shortcut" timeout parameter that is supposed to apply to all connection types (Guzzle, CurlMultiConnection, etc)...but I'm looking through the code now and I dont think it actually works for Guzzle. I'll open a ticket.

Mccormack answered 25/8, 2014 at 21:35 Comment(0)
H
0

In my php elastic version solution is

retun ClientBuilder::create()
      ->setHttpClientOptions([
            'connect_timeout' => 10,//seconds
        ])
       ->build();

Maybe someone will be useful =)

Hercules answered 5/2, 2023 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.