1 | <?php |
||
16 | class Client extends BaseClient |
||
17 | { |
||
18 | /** |
||
19 | * Stores created indexes to avoid recreation. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $indexCache = array(); |
||
24 | |||
25 | /** |
||
26 | * Symfony's debugging Stopwatch. |
||
27 | * |
||
28 | * @var Stopwatch|null |
||
29 | */ |
||
30 | private $stopwatch; |
||
31 | |||
32 | /** |
||
33 | * @param string $path |
||
34 | * @param string $method |
||
35 | * @param array $data |
||
36 | * @param array $query |
||
37 | * |
||
38 | * @return \Elastica\Response |
||
39 | */ |
||
40 | 10 | public function request($path, $method = Request::GET, $data = array(), array $query = array()) |
|
41 | { |
||
42 | 10 | if ($this->stopwatch) { |
|
43 | 9 | $this->stopwatch->start('es_request', 'fos_elastica'); |
|
44 | 9 | } |
|
45 | |||
46 | 10 | $start = microtime(true); |
|
47 | 10 | $response = parent::request($path, $method, $data, $query); |
|
48 | 10 | $responseData = $response->getData(); |
|
49 | |||
50 | 10 | if (isset($responseData['took']) && isset($responseData['hits'])) { |
|
51 | 2 | $this->logQuery($path, $method, $data, $query, $start, $response->getEngineTime(), $responseData['hits']['total']); |
|
52 | 2 | } else { |
|
53 | 9 | $this->logQuery($path, $method, $data, $query, $start, 0, 0); |
|
54 | } |
||
55 | |||
56 | 10 | if ($this->stopwatch) { |
|
57 | 9 | $this->stopwatch->stop('es_request'); |
|
58 | 9 | } |
|
59 | |||
60 | 10 | return $response; |
|
61 | } |
||
62 | |||
63 | 11 | public function getIndex($name) |
|
64 | { |
||
65 | 11 | if (isset($this->indexCache[$name])) { |
|
66 | return $this->indexCache[$name]; |
||
67 | } |
||
68 | |||
69 | 11 | return $this->indexCache[$name] = new Index($this, $name); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Sets a stopwatch instance for debugging purposes. |
||
74 | * |
||
75 | * @param Stopwatch $stopwatch |
||
76 | */ |
||
77 | 12 | public function setStopwatch(Stopwatch $stopwatch = null) |
|
81 | |||
82 | /** |
||
83 | * Log the query if we have an instance of ElasticaLogger. |
||
84 | * |
||
85 | * @param string $path |
||
86 | * @param string $method |
||
87 | * @param array $data |
||
88 | * @param array $query |
||
89 | * @param int $start |
||
90 | */ |
||
91 | 10 | private function logQuery($path, $method, $data, array $query, $start, $engineMS = 0, $itemCount = 0) |
|
109 | } |
||
110 |