1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Leopard; |
3
|
|
|
|
4
|
|
|
use Elasticsearch\Client; |
5
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
6
|
|
|
use Triadev\Es\Contract\ElasticsearchContract; |
7
|
|
|
use Triadev\Leopard\Business\Dsl\Search; |
8
|
|
|
use Triadev\Leopard\Business\Dsl\Suggestion; |
9
|
|
|
use Triadev\Leopard\Business\Mapping\Builder; |
10
|
|
|
use Triadev\Leopard\Contract\ElasticsearchManagerContract; |
11
|
|
|
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract; |
12
|
|
|
|
13
|
|
|
class ElasticsearchManager implements ElasticsearchManagerContract |
14
|
|
|
{ |
15
|
|
|
/** @var Client */ |
16
|
|
|
private $esClient; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ElasticsearchManager constructor. |
20
|
|
|
* @param ElasticsearchContract $elasticsearch |
21
|
|
|
*/ |
22
|
46 |
|
public function __construct(ElasticsearchContract $elasticsearch) |
23
|
|
|
{ |
24
|
46 |
|
$this->esClient = $elasticsearch->getClient(); |
25
|
46 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get elasticsearch client |
29
|
|
|
* |
30
|
|
|
* @return Client |
31
|
|
|
*/ |
32
|
14 |
|
public function getEsClient(): Client |
33
|
|
|
{ |
34
|
14 |
|
return $this->esClient; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get es default index |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
4 |
|
public function getEsDefaultIndex() : string |
43
|
|
|
{ |
44
|
4 |
|
return config('leopard.index'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Search |
49
|
|
|
* |
50
|
|
|
* @return Search |
51
|
|
|
*/ |
52
|
9 |
|
public function search(): Search |
53
|
|
|
{ |
54
|
9 |
|
return app()->make(Search::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Suggestion |
59
|
|
|
* |
60
|
|
|
* @return Suggestion |
61
|
|
|
*/ |
62
|
3 |
|
public function suggest() : Suggestion |
63
|
|
|
{ |
64
|
3 |
|
return app()->make(Suggestion::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Repository |
69
|
|
|
* |
70
|
|
|
* @return ElasticsearchRepositoryContract |
71
|
|
|
*/ |
72
|
1 |
|
public function repository() : ElasticsearchRepositoryContract |
73
|
|
|
{ |
74
|
1 |
|
return app(ElasticsearchRepositoryContract::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Map |
79
|
|
|
* |
80
|
|
|
* @param \Closure $blueprint |
81
|
|
|
* @param string $index |
82
|
|
|
* @param string $type |
83
|
|
|
* @param bool $createIndex |
84
|
|
|
*/ |
85
|
10 |
|
public function map(\Closure $blueprint, string $index, string $type, bool $createIndex = false) |
86
|
|
|
{ |
87
|
10 |
|
(new Builder())->create($blueprint, $index, $type, $createIndex); |
88
|
10 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Search statement |
92
|
|
|
* |
93
|
|
|
* @param array $params |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
2 |
|
public function searchStatement(array $params) : array |
97
|
|
|
{ |
98
|
2 |
|
return $this->esClient->search($params); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Put mapping statement |
103
|
|
|
* |
104
|
|
|
* @param array $params |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
2 |
|
public function putMappingStatement(array $params): array |
108
|
|
|
{ |
109
|
2 |
|
return $this->esClient->indices()->putMapping($params); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Index statement |
114
|
|
|
* |
115
|
|
|
* @param array $params |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
7 |
|
public function indexStatement(array $params) : array |
119
|
|
|
{ |
120
|
7 |
|
return $this->esClient->index($params); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Update statement |
125
|
|
|
* |
126
|
|
|
* @param array $params |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
1 |
|
public function updateStatement(array $params) : array |
130
|
|
|
{ |
131
|
1 |
|
return $this->esClient->update($params); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Exist statement |
136
|
|
|
* |
137
|
|
|
* @param array $params |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
1 |
|
public function existStatement(array $params) : bool |
141
|
|
|
{ |
142
|
1 |
|
return $this->esClient->exists($params); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Exist index statement |
147
|
|
|
* |
148
|
|
|
* @param array $params |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
10 |
|
public function existIndexStatement(array $params) : bool |
152
|
|
|
{ |
153
|
10 |
|
return $this->esClient->indices()->exists($params); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Delete statement |
158
|
|
|
* |
159
|
|
|
* @param array $params |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
1 |
|
public function deleteStatement(array $params) : array |
163
|
|
|
{ |
164
|
1 |
|
return $this->esClient->delete($params); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get statement |
169
|
|
|
* |
170
|
|
|
* @param array $params |
171
|
|
|
* @return array|null |
172
|
|
|
*/ |
173
|
6 |
|
public function getStatement(array $params) : ?array |
174
|
|
|
{ |
175
|
|
|
try { |
176
|
6 |
|
return $this->esClient->get($params); |
177
|
6 |
|
} catch (Missing404Exception $e) { |
178
|
6 |
|
return null; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Suggest statement |
184
|
|
|
* |
185
|
|
|
* @param array $params |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function suggestStatement(array $params): array |
189
|
|
|
{ |
190
|
|
|
return $this->esClient->suggest($params); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Bulk statement |
195
|
|
|
* |
196
|
|
|
* @param array $params |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
3 |
|
public function bulkStatement(array $params): array |
200
|
|
|
{ |
201
|
3 |
|
return $this->esClient->bulk($params); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|