From ff09af20cf773939a5e1102489bdb7b1e5a06dd4 Mon Sep 17 00:00:00 2001 From: wissem Date: Mon, 28 Jul 2014 16:33:59 +0200 Subject: [PATCH 1/4] We want the http timeout to be longer and configurable --- src/ElasticSearch/Transport/HTTP.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ElasticSearch/Transport/HTTP.php b/src/ElasticSearch/Transport/HTTP.php index 548c00a..f198625 100755 --- a/src/ElasticSearch/Transport/HTTP.php +++ b/src/ElasticSearch/Transport/HTTP.php @@ -20,7 +20,7 @@ class HTTP extends Base { /** * How long before timing out CURL call */ - const TIMEOUT = 5; + private $timeout = 60; /** * curl handler which is needed for reusing existing http connection to the server @@ -29,8 +29,9 @@ class HTTP extends Base { protected $ch; - public function __construct($host='localhost', $port=9200) { + public function __construct($host='localhost', $port=9200, $httpTimeout=60) { parent::__construct($host, $port); + $this->setTimeout($httpTimeout); $this->ch = curl_init(); } @@ -157,7 +158,7 @@ protected function call($url, $method="GET", $payload=null) { $protocol = "http"; $requestURL = $protocol . "://" . $this->host . $url; curl_setopt($conn, CURLOPT_URL, $requestURL); - curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT); + curl_setopt($conn, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($conn, CURLOPT_PORT, $this->port); curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1) ; curl_setopt($conn, CURLOPT_CUSTOMREQUEST, strtoupper($method)); @@ -221,4 +222,14 @@ protected function call($url, $method="GET", $payload=null) { return $data; } + + public function setTimeout($timeout) + { + $this->timeout = $timeout; + } + + public function getTimeout() + { + return $this->timeout; + } } From 67bb0adb7e90bcab7db2bbeecccaa1de1953491e Mon Sep 17 00:00:00 2001 From: wissem Date: Mon, 28 Jul 2014 16:56:11 +0200 Subject: [PATCH 2/4] don't ask people to change their code --- src/ElasticSearch/Transport/HTTP.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ElasticSearch/Transport/HTTP.php b/src/ElasticSearch/Transport/HTTP.php index f198625..e88c202 100755 --- a/src/ElasticSearch/Transport/HTTP.php +++ b/src/ElasticSearch/Transport/HTTP.php @@ -20,7 +20,7 @@ class HTTP extends Base { /** * How long before timing out CURL call */ - private $timeout = 60; + private $timeout = 5; /** * curl handler which is needed for reusing existing http connection to the server @@ -29,7 +29,7 @@ class HTTP extends Base { protected $ch; - public function __construct($host='localhost', $port=9200, $httpTimeout=60) { + public function __construct($host='localhost', $port=9200, $httpTimeout=5) { parent::__construct($host, $port); $this->setTimeout($httpTimeout); $this->ch = curl_init(); From 6478c4998c9bc36434ab9f26a8322f2f00ce6461 Mon Sep 17 00:00:00 2001 From: Wissem Garouachi Date: Thu, 31 Jul 2014 17:14:38 +0200 Subject: [PATCH 3/4] Timeout should be configurable in the Client class --- src/ElasticSearch/Client.php | 12 ++++++++++-- src/ElasticSearch/Transport/HTTP.php | 6 ++++-- src/ElasticSearch/Transport/Memcached.php | 4 ++-- tests/units/Client.php | 3 ++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ElasticSearch/Client.php b/src/ElasticSearch/Client.php index 259c968..0d1a054 100755 --- a/src/ElasticSearch/Client.php +++ b/src/ElasticSearch/Client.php @@ -23,7 +23,8 @@ class Client { 'protocol' => Client::DEFAULT_PROTOCOL, 'servers' => Client::DEFAULT_SERVER, 'index' => Client::DEFAULT_INDEX, - 'type' => Client::DEFAULT_TYPE + 'type' => Client::DEFAULT_TYPE, + 'timeout' => null, ); protected static $_protocols = array( @@ -66,6 +67,7 @@ public static function connection($config = array()) { if (is_string($config)) { $config = self::parseDsn($config); } + $config += self::$_defaults; $protocol = $config['protocol']; @@ -74,9 +76,15 @@ public static function connection($config = array()) { } $class = self::$_protocols[$protocol]; + if (null !== $config['timeout'] && !is_numeric($config['timeout'])) { + throw new \Exception("HTTP timeout should have a numeric value when specified."); + } + $server = is_array($config['servers']) ? $config['servers'][0] : $config['servers']; list($host, $port) = explode(':', $server); - $transport = new $class($host, $port); + + $transport = new $class($host, $port, $config['timeout']); + $client = new self($transport, $config['index'], $config['type']); $client->config($config); return $client; diff --git a/src/ElasticSearch/Transport/HTTP.php b/src/ElasticSearch/Transport/HTTP.php index e88c202..9808d0e 100755 --- a/src/ElasticSearch/Transport/HTTP.php +++ b/src/ElasticSearch/Transport/HTTP.php @@ -29,9 +29,11 @@ class HTTP extends Base { protected $ch; - public function __construct($host='localhost', $port=9200, $httpTimeout=5) { + public function __construct($host='localhost', $port=9200, $timeout=null) { parent::__construct($host, $port); - $this->setTimeout($httpTimeout); + if(null !== $timeout) { + $this->setTimeout($timeout); + } $this->ch = curl_init(); } diff --git a/src/ElasticSearch/Transport/Memcached.php b/src/ElasticSearch/Transport/Memcached.php index 433c7be..700e5bb 100755 --- a/src/ElasticSearch/Transport/Memcached.php +++ b/src/ElasticSearch/Transport/Memcached.php @@ -15,10 +15,10 @@ */ class Memcached extends Base { - public function __construct($host="127.0.0.1", $port=11311) { + public function __construct($host="127.0.0.1", $port=11311, $timeout=null) { parent::__construct($host, $port); $this->conn = new Memcache; - $this->conn->connect($host, $port); + $this->conn->connect($host, $port, $timeout); } /** diff --git a/tests/units/Client.php b/tests/units/Client.php index be4bfaa..dace237 100644 --- a/tests/units/Client.php +++ b/tests/units/Client.php @@ -17,7 +17,8 @@ public function testDsnIsCorrectlyParsed() { 'protocol' => 'http', 'servers' => 'test.com:9100', 'index' => 'index', - 'type' => 'type' + 'type' => 'type', + 'timeout' => null, ); $this->assert->array($search->config()) ->isEqualTo($config); From 0b535aa3f5ac24a2d3a53702c5e6d75521f333a0 Mon Sep 17 00:00:00 2001 From: gwissem-mention Date: Thu, 31 Jul 2014 17:56:03 +0200 Subject: [PATCH 4/4] A Symfony2 example --- README.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/README.markdown b/README.markdown index 262514d..5c5664a 100644 --- a/README.markdown +++ b/README.markdown @@ -121,3 +121,51 @@ $es->createBulk() ``` +### Usage as a service in Symfony2 + +In order to use the Dependency Injection to inject the client as a service, you'll have to define it before. +So in your bundle's services.yml file you can put something like this : +```yml + your_bundle.elastic_transport: + class: ElasticSearch\Transport\HTTP + arguments: + - localhost + - 9200 + - 60 + + your_bundle.elastic_client: + class: ElasticSearch\Client + arguments: + - @your_bundle.elastic_transport +``` +To make Symfony2 recognize the `ElasticSearch` namespace, you'll have to register it. So in your `app/autoload.php` make sure your have : +```php +registerNamespaces(array( + // ... + 'ElasticSearch' => __DIR__.'/path/to/your/vendor/nervetattoo/elasticsearch/src', +)); +``` +Then, you can get your client via the service container and use it like usual. For example, in your controller you can do this : +```php +get('your_bundle.elastic_client'); + $results = $es + ->setIndex(array("one", "two")) + ->setType(array("mytype", "other-type")) + ->search('title:cool'); + } +} +``` + + +