- PHP Client: other versions:
- Overview
- Quickstart
- Installation
- Configuration
- Inline Host Configuration
- Extended Host Configuration
- Authorization and Encryption
- Set retries
- Enabling the Logger
- Configure the HTTP Handler
- Setting the Connection Pool
- Setting the Connection Selector
- Setting the Serializer
- Setting a custom ConnectionFactory
- Set the Endpoint closure
- Building the client from a configuration hash
- Per-request configuration
- Future Mode
- Dealing with JSON Arrays and Objects in PHP
- Index Management Operations
- Indexing Documents
- Getting Documents
- Updating Documents
- Deleting documents
- Search Operations
- Namespaces
- Security
- Connection Pool
- Selectors
- Serializers
- PHP Version Requirement
- Breaking changes from 5.x
- Community DSLs
- Community Integrations
- Reference - Endpoints
- Elasticsearch\Client
- Elasticsearch\ClientBuilder
- Elasticsearch\Namespaces\CatNamespace
- Elasticsearch\Namespaces\ClusterNamespace
- Elasticsearch\Namespaces\IndicesNamespace
- Elasticsearch\Namespaces\IngestNamespace
- Elasticsearch\Namespaces\NodesNamespace
- Elasticsearch\Namespaces\RemoteNamespace
- Elasticsearch\Namespaces\SnapshotNamespace
- Elasticsearch\Namespaces\TasksNamespace
Set retries
editSet retries
editBy default, the client will retry n
times, where n = number of nodes
in your cluster. A retry is only performed
if the operation results in a "hard" exception: connection refusal, connection timeout, DNS lookup timeout, etc. 4xx and
5xx errors are not considered retry’able events, since the node returns an operational response.
If you would like to disable retries, or change the number, you can do so with the setRetries()
method:
$client = ClientBuilder::create() ->setRetries(2) ->build();
When the client runs out of retries, it will throw the last exception that it received. For example, if you have ten
alive nodes, and setRetries(5)
, the client will attempt to execute the command up to five times. If all five nodes
result in a connection timeout (for example), the client will throw an OperationTimeoutException
. Depending on the
Connection Pool being used, these nodes may also be marked dead.
To help in identification, exceptions that are thrown due to max retries will wrap a MaxRetriesException
. For example,
you can catch a specific curl exception then check if it wraps a MaxRetriesException using getPrevious()
:
$client = Elasticsearch\ClientBuilder::create() ->setHosts(["localhost:1"]) ->setRetries(0) ->build(); try { $client->search($searchParams); } catch (Elasticsearch\Common\Exceptions\Curl\CouldNotConnectToHost $e) { $previous = $e->getPrevious(); if ($previous instanceof 'Elasticsearch\Common\Exceptions\MaxRetriesException') { echo "Max retries!"; } }
Alternatively, all "hard" curl exceptions (CouldNotConnectToHost
, CouldNotResolveHostException
, OperationTimeoutException
)
extend the more general TransportException
. So you could instead catch the general TransportException
and then
check it’s previous value:
$client = Elasticsearch\ClientBuilder::create() ->setHosts(["localhost:1"]) ->setRetries(0) ->build(); try { $client->search($searchParams); } catch (Elasticsearch\Common\Exceptions\TransportException $e) { $previous = $e->getPrevious(); if ($previous instanceof 'Elasticsearch\Common\Exceptions\MaxRetriesException') { echo "Max retries!"; } }