2014-04-21 01:21:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOS\ElasticaBundle\Elastica;
|
|
|
|
|
|
|
|
use Elastica\Client as BaseClient;
|
|
|
|
use Elastica\Request;
|
|
|
|
use FOS\ElasticaBundle\Logger\ElasticaLogger;
|
2014-07-03 15:20:18 +02:00
|
|
|
use Symfony\Component\Stopwatch\Stopwatch;
|
2014-04-21 01:21:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extends the default Elastica client to provide logging for errors that occur
|
|
|
|
* during communication with ElasticSearch.
|
|
|
|
*
|
|
|
|
* @author Gordon Franke <info@nevalon.de>
|
|
|
|
*/
|
|
|
|
class Client extends BaseClient
|
|
|
|
{
|
2014-06-18 08:48:00 +02:00
|
|
|
/**
|
|
|
|
* Stores created indexes to avoid recreation.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $indexCache = array();
|
|
|
|
|
2014-07-03 15:20:18 +02:00
|
|
|
/**
|
|
|
|
* Symfony's debugging Stopwatch
|
|
|
|
*
|
|
|
|
* @var Stopwatch|null
|
|
|
|
*/
|
|
|
|
private $stopwatch;
|
|
|
|
|
2014-04-21 01:21:50 +02:00
|
|
|
/**
|
2014-08-24 11:53:24 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param string $method
|
|
|
|
* @param array $data
|
|
|
|
* @param array $query
|
|
|
|
* @return \Elastica\Response
|
2014-04-21 01:21:50 +02:00
|
|
|
*/
|
|
|
|
public function request($path, $method = Request::GET, $data = array(), array $query = array())
|
|
|
|
{
|
2014-07-03 15:20:18 +02:00
|
|
|
if ($this->stopwatch) {
|
|
|
|
$this->stopwatch->start('es_request', 'fos_elastica');
|
|
|
|
}
|
|
|
|
|
2014-04-21 01:21:50 +02:00
|
|
|
$start = microtime(true);
|
|
|
|
$response = parent::request($path, $method, $data, $query);
|
|
|
|
|
2014-08-24 11:53:24 +02:00
|
|
|
$this->logQuery($path, $method, $data, $query, $start);
|
2014-04-21 01:21:50 +02:00
|
|
|
|
2014-07-03 15:20:18 +02:00
|
|
|
if ($this->stopwatch) {
|
|
|
|
$this->stopwatch->stop('es_request');
|
|
|
|
}
|
|
|
|
|
2014-04-21 01:21:50 +02:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIndex($name)
|
|
|
|
{
|
2014-06-18 08:48:00 +02:00
|
|
|
if (isset($this->indexCache[$name])) {
|
|
|
|
return $this->indexCache[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->indexCache[$name] = new Index($this, $name);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
2014-07-03 15:20:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a stopwatch instance for debugging purposes.
|
|
|
|
*
|
|
|
|
* @param Stopwatch $stopwatch
|
|
|
|
*/
|
|
|
|
public function setStopwatch(Stopwatch $stopwatch = null)
|
|
|
|
{
|
|
|
|
$this->stopwatch = $stopwatch;
|
|
|
|
}
|
2014-08-24 11:53:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the query if we have an instance of ElasticaLogger.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $method
|
|
|
|
* @param array $data
|
|
|
|
* @param array $query
|
|
|
|
* @param int $start
|
|
|
|
*/
|
|
|
|
private function logQuery($path, $method, $data, array $query, $start)
|
|
|
|
{
|
|
|
|
if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$time = microtime(true) - $start;
|
|
|
|
$connection = $this->getLastRequest()->getConnection();
|
|
|
|
|
|
|
|
$connection_array = array(
|
|
|
|
'host' => $connection->getHost(),
|
|
|
|
'port' => $connection->getPort(),
|
|
|
|
'transport' => $connection->getTransport(),
|
|
|
|
'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->_logger->logQuery($path, $method, $data, $time, $connection_array, $query);
|
|
|
|
}
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|