|
1
|
|
|
<?php namespace Bardex\Elastic; |
|
2
|
|
|
|
|
3
|
|
|
use Elasticsearch\Client as ElasticClient; |
|
4
|
|
|
use Elasticsearch\ClientBuilder as ElasticClientBuilder; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @package Bardex\Elastic |
|
8
|
|
|
* @author Alexey Sumin <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class Client |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var ElasticClient $client */ |
|
13
|
|
|
protected $elastic; |
|
14
|
|
|
|
|
15
|
|
|
/** @var IListener[] */ |
|
16
|
|
|
protected $listeners = []; |
|
17
|
|
|
|
|
18
|
|
|
/** @var IHydrator */ |
|
19
|
|
|
protected $hydrator; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param ElasticClient $elastic |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(ElasticClient $elastic) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->elastic = $elastic; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string|array $host |
|
32
|
|
|
* @return Client |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function create($host) |
|
35
|
|
|
{ |
|
36
|
|
|
$es = ElasticClientBuilder::create() |
|
37
|
|
|
->setHosts((array)$host) |
|
38
|
|
|
->build(); |
|
39
|
|
|
|
|
40
|
|
|
return new static($es); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param IHydrator $hydrator |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setHydrator($hydrator) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->hydrator = $hydrator; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return IHydrator |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getHydrator() |
|
56
|
|
|
{ |
|
57
|
|
|
if (null === $this->hydrator) { |
|
58
|
|
|
$this->hydrator = new Hydrator; |
|
59
|
|
|
} |
|
60
|
|
|
return $this->hydrator; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create new instance of SearchQuery |
|
65
|
|
|
* @return SearchQuery |
|
66
|
|
|
*/ |
|
67
|
|
|
public function createSearchQuery() |
|
68
|
|
|
{ |
|
69
|
|
|
$query = new SearchQuery($this); |
|
70
|
|
|
return $query; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Create new instance of MultiQuery |
|
75
|
|
|
* @return MultiQuery |
|
76
|
|
|
*/ |
|
77
|
|
|
public function createMultiQuery() |
|
78
|
|
|
{ |
|
79
|
|
|
$query = new MultiQuery($this); |
|
80
|
|
|
return $query; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param IListener $listener |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
|
|
public function addListener(IListener $listener) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->listeners[] = $listener; |
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param IListener $listener |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
public function removeListener(IListener $listener) |
|
98
|
|
|
{ |
|
99
|
|
|
foreach ($this->listeners as $i => $listItem) { |
|
100
|
|
|
if ($listener === $listItem) { |
|
101
|
|
|
unset($this->listeners[$i]); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function triggerSuccess(array $query, array $response, $time) |
|
108
|
|
|
{ |
|
109
|
|
|
foreach ($this->listeners as $listener) { |
|
110
|
|
|
$listener->onSuccess($query, $response, $time); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function triggerError(array $query, \Exception $e) |
|
115
|
|
|
{ |
|
116
|
|
|
foreach ($this->listeners as $listener) { |
|
117
|
|
|
$listener->onError($query, $e); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function query($endpoint, array $query) |
|
122
|
|
|
{ |
|
123
|
|
|
// send query to elastic |
|
124
|
|
|
$start = microtime(1); |
|
125
|
|
|
|
|
126
|
|
|
try { |
|
127
|
|
|
$result = call_user_func([$this->elastic, $endpoint], $query); |
|
128
|
|
|
$time = round((microtime(1) - $start) * 1000); |
|
129
|
|
|
$this->triggerSuccess($query, $result, $time); |
|
130
|
|
|
return $result; |
|
131
|
|
|
} catch (\Exception $e) { |
|
132
|
|
|
$this->triggerError($query, $e); |
|
133
|
|
|
throw $e; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function search(array $query, $hydration = true) |
|
138
|
|
|
{ |
|
139
|
|
|
$response = $this->query('search', $query); |
|
140
|
|
|
if ($hydration) { |
|
141
|
|
|
$result = $this->getHydrator()->hydrateResponse($response); |
|
142
|
|
|
return $result; |
|
143
|
|
|
} else { |
|
144
|
|
|
return $response; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function msearch(array $query, $hydration = true) |
|
149
|
|
|
{ |
|
150
|
|
|
$responses = $this->query('msearch', $query); |
|
151
|
|
|
if ($hydration) { |
|
152
|
|
|
$results = []; |
|
153
|
|
|
foreach ($responses['responses'] as $response) { |
|
154
|
|
|
$results[] = $this->getHydrator()->hydrateResponse($response); |
|
155
|
|
|
} |
|
156
|
|
|
return new SearchResult($results, 0); |
|
157
|
|
|
} else { |
|
158
|
|
|
return $responses; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|