Changing or replacing Selector Class

edit

Changing or replacing Selector Class

edit

Changing the selector is also very simple: instantiate the client with your chosen implementation:

$params['selectorClass'] = '\Elasticsearch\ConnectionPool\Selectors\RandomSelector';
$client = new Elasticsearch\Client($params);


The client will now query random nodes. It is sometimes useful to build a custom selector which services your particular cluster with custom business logic.

For example, we can build a new selector that only selects the first connection each time. This is obviously not a good selector (!!!), but it demonstrates the concept well:

namespace MyProject\Selectors;

use Elasticsearch\Connections\ConnectionInterface;
use Elasticsearch\ConnectionPool\Selectors\SelectorInterface

class FirstSelector implements SelectorInterface
{

    /**
     * Selects the first connection
     *
     * @param array $connections Array of Connection objects
     *
     * @return ConnectionInterface
     */
    public function select($connections)
    {
        return $connections[0];
    }

}


And now we can specify that when creating the client:

$params['selectorClass'] = '\MyProject\Selectors\FirstSelector';
$client = new Elasticsearch\Client($params);