1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Elastica; |
13
|
|
|
|
14
|
|
|
use Elastica\Client as BaseClient; |
15
|
|
|
use Elastica\Request; |
16
|
|
|
use FOS\ElasticaBundle\Logger\ElasticaLogger; |
17
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Extends the default Elastica client to provide logging for errors that occur |
21
|
|
|
* during communication with ElasticSearch. |
22
|
|
|
* |
23
|
|
|
* @author Gordon Franke <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Client extends BaseClient |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Stores created indexes to avoid recreation. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $indexCache = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Stores created index template to avoid recreation. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $indexTemplateCache = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Symfony's debugging Stopwatch. |
43
|
|
|
* |
44
|
|
|
* @var Stopwatch|null |
45
|
|
|
*/ |
46
|
|
|
private $stopwatch; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
14 |
|
public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE) |
52
|
|
|
{ |
53
|
14 |
|
if ($this->stopwatch) { |
54
|
13 |
|
$this->stopwatch->start('es_request', 'fos_elastica'); |
55
|
|
|
} |
56
|
|
|
|
57
|
14 |
|
$response = parent::request($path, $method, $data, $query, $contentType); |
58
|
14 |
|
$responseData = $response->getData(); |
59
|
|
|
|
60
|
14 |
|
if (isset($responseData['took']) && isset($responseData['hits'])) { |
61
|
2 |
|
$this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']); |
62
|
|
|
} else { |
63
|
13 |
|
$this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0); |
64
|
|
|
} |
65
|
|
|
|
66
|
14 |
|
if ($this->stopwatch) { |
67
|
13 |
|
$this->stopwatch->stop('es_request'); |
68
|
|
|
} |
69
|
|
|
|
70
|
14 |
|
return $response; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $name |
75
|
|
|
* |
76
|
|
|
* @return Index|mixed |
77
|
|
|
*/ |
78
|
15 |
|
public function getIndex($name) |
79
|
|
|
{ |
80
|
15 |
|
if (isset($this->indexCache[$name])) { |
81
|
|
|
return $this->indexCache[$name]; |
82
|
|
|
} |
83
|
|
|
|
84
|
15 |
|
return $this->indexCache[$name] = new Index($this, $name); |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
public function getIndexTemplate($name) |
88
|
|
|
{ |
89
|
6 |
|
if (isset($this->indexTemplateCache[$name])) { |
90
|
1 |
|
return $this->indexTemplateCache[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
return $this->indexTemplateCache[$name] = new IndexTemplate($this, $name); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets a stopwatch instance for debugging purposes. |
98
|
|
|
* |
99
|
|
|
* @param Stopwatch $stopwatch |
100
|
|
|
*/ |
101
|
17 |
|
public function setStopwatch(Stopwatch $stopwatch = null) |
102
|
|
|
{ |
103
|
17 |
|
$this->stopwatch = $stopwatch; |
104
|
17 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Log the query if we have an instance of ElasticaLogger. |
108
|
|
|
* |
109
|
|
|
* @param string $path |
110
|
|
|
* @param string $method |
111
|
|
|
* @param array|string $data |
112
|
|
|
* @param array $query |
113
|
|
|
* @param int $queryTime |
114
|
|
|
* @param int $engineMS |
115
|
|
|
* @param int $itemCount |
116
|
|
|
*/ |
117
|
14 |
|
private function logQuery($path, $method, $data, array $query, $queryTime, $engineMS = 0, $itemCount = 0) |
118
|
|
|
{ |
119
|
14 |
|
if (!$this->_logger or !$this->_logger instanceof ElasticaLogger) { |
120
|
|
|
return; |
121
|
|
|
} |
122
|
|
|
|
123
|
14 |
|
$connection = $this->getLastRequest()->getConnection(); |
124
|
|
|
|
125
|
|
|
$connectionArray = [ |
126
|
14 |
|
'host' => $connection->getHost(), |
127
|
14 |
|
'port' => $connection->getPort(), |
128
|
14 |
|
'transport' => $connection->getTransport(), |
129
|
14 |
|
'headers' => $connection->hasConfig('headers') ? $connection->getConfig('headers') : [], |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
/** @var ElasticaLogger $logger */ |
133
|
14 |
|
$logger = $this->_logger; |
134
|
14 |
|
$logger->logQuery($path, $method, $data, $queryTime, $connectionArray, $query, $engineMS, $itemCount); |
135
|
14 |
|
} |
136
|
|
|
} |
137
|
|
|
|