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