Set the Endpoint closure
editSet the Endpoint closure
editThe client uses an Endpoint closure to dispatch API requests to the correct Endpoint object. A namespace object will construct a new Endpoint via this closure, which means this is a handy location if you wish to extend the available set of API endpoints available.
For example, we could add a new endpoint like so:
$transport = $this->transport; $serializer = $this->serializer; $newEndpoint = function ($class) use ($transport, $serializer) { if ($class == 'SuperSearch') { return new MyProject\SuperSearch($transport); } else { // Default handler $fullPath = '\\Elasticsearch\\Endpoints\\' . $class; if ($class === 'Bulk' || $class === 'Msearch' || $class === 'MPercolate') { return new $fullPath($transport, $serializer); } else { return new $fullPath($transport); } } }; $client = ClientBuilder::create() ->setEndpoint($newEndpoint) ->build();
Obviously, by doing this you take responsibility that all existing endpoints still function correctly. And you also assume the responsibility of correctly wiring the Transport and Serializer into each endpoint.
Building the client from a configuration hash
editTo help ease automated building of the client, all configurations can be
provided in a setting hash instead of calling the individual methods directly.
This functionality is exposed through the ClientBuilder::FromConfig()
static
method, which accepts an array of configurations and returns a fully built
client.
Array keys correspond to the method name, for example retries
key corresponds
to setRetries()
method.
$params = [ 'hosts' => [ 'localhost:9200' ], 'retries' => 2, 'handler' => ClientBuilder::singleHandler() ]; $client = ClientBuilder::fromConfig($params);
Unknown parameters throw an exception, to help the user find potential problems. If this behavior is not desired (for example, you are using the hash for other purposes, and may have keys unrelated to the Elasticsearch client), you can set $quiet = true in fromConfig() to silence the exceptions.
$params = [ 'hosts' => [ 'localhost:9200' ], 'retries' => 2, 'imNotReal' => 5 ]; // Set $quiet to true to ignore the unknown `imNotReal` key $client = ClientBuilder::fromConfig($params, true);