1 | <?php |
||
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 | * Symfony's debugging Stopwatch. |
||
36 | * |
||
37 | * @var Stopwatch|null |
||
38 | */ |
||
39 | private $stopwatch; |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 10 | public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE) |
|
45 | { |
||
46 | 10 | if ($this->stopwatch) { |
|
47 | 9 | $this->stopwatch->start('es_request', 'fos_elastica'); |
|
48 | } |
||
49 | |||
50 | 10 | $response = parent::request($path, $method, $data, $query, $contentType); |
|
51 | 10 | $responseData = $response->getData(); |
|
52 | |||
53 | 10 | if (isset($responseData['took']) && isset($responseData['hits'])) { |
|
54 | 2 | $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), $response->getEngineTime(), $responseData['hits']['total']); |
|
55 | } else { |
||
56 | 9 | $this->logQuery($path, $method, $data, $query, $response->getQueryTime(), 0, 0); |
|
57 | } |
||
58 | |||
59 | 10 | if ($this->stopwatch) { |
|
60 | 9 | $this->stopwatch->stop('es_request'); |
|
61 | } |
||
62 | |||
63 | 10 | return $response; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $name |
||
68 | * |
||
69 | * @return Index|mixed |
||
70 | */ |
||
71 | 12 | public function getIndex($name) |
|
72 | { |
||
73 | 12 | if (isset($this->indexCache[$name])) { |
|
74 | return $this->indexCache[$name]; |
||
75 | } |
||
76 | |||
77 | 12 | return $this->indexCache[$name] = new Index($this, $name); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Sets a stopwatch instance for debugging purposes. |
||
82 | * |
||
83 | * @param Stopwatch $stopwatch |
||
84 | */ |
||
85 | 13 | public function setStopwatch(Stopwatch $stopwatch = null) |
|
86 | { |
||
87 | 13 | $this->stopwatch = $stopwatch; |
|
88 | 13 | } |
|
89 | |||
90 | /** |
||
91 | * Log the query if we have an instance of ElasticaLogger. |
||
92 | * |
||
93 | * @param string $path |
||
94 | * @param string $method |
||
95 | * @param array $data |
||
96 | * @param array $query |
||
97 | * @param int $queryTime |
||
98 | * @param int $engineMS |
||
99 | * @param int $itemCount |
||
100 | */ |
||
101 | 10 | private function logQuery($path, $method, $data, array $query, $queryTime, $engineMS = 0, $itemCount = 0) |
|
120 | } |
||
121 |