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