1 | <?php |
||
2 | namespace Triadev\Es; |
||
3 | |||
4 | use Triadev\Es\Business\AbstractElasticsearch; |
||
5 | use Triadev\Es\Contract\ElasticsearchDocumentContract; |
||
6 | |||
7 | class ElasticsearchDocument extends AbstractElasticsearch implements ElasticsearchDocumentContract |
||
8 | { |
||
9 | /** |
||
10 | * Create document |
||
11 | * |
||
12 | * @param string $index |
||
13 | * @param string $type |
||
14 | * @param string|null $version |
||
15 | * @param array $params |
||
16 | * @param null|string $id |
||
17 | * @return array |
||
18 | */ |
||
19 | 9 | public function createDocument( |
|
20 | string $index, |
||
21 | string $type, |
||
22 | string $version = null, |
||
23 | array $params = [], |
||
24 | ?string $id = null |
||
25 | ) : array { |
||
26 | 9 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
27 | 9 | $params['type'] = $type; |
|
28 | |||
29 | 9 | if ($id) { |
|
30 | 9 | $params['id'] = $id; |
|
31 | } |
||
32 | |||
33 | 9 | $result = $this->getElasticsearchClient()->index($params); |
|
34 | |||
35 | 9 | return $result; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Update document |
||
40 | * |
||
41 | * @param string $index |
||
42 | * @param string $type |
||
43 | * @param string|null $version |
||
44 | * @param array $params |
||
45 | * @param null|string $id |
||
46 | * @return array |
||
47 | */ |
||
48 | 1 | public function updateDocument( |
|
49 | string $index, |
||
50 | string $type, |
||
51 | string $version = null, |
||
52 | array $params = [], |
||
53 | ?string $id = null |
||
54 | ) : array { |
||
55 | 1 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
56 | 1 | $params['type'] = $type; |
|
57 | |||
58 | 1 | if ($id) { |
|
59 | 1 | $params['id'] = $id; |
|
60 | } |
||
61 | |||
62 | 1 | $result = $this->getElasticsearchClient()->update($params); |
|
63 | |||
64 | 1 | return $result; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Create documents with bulk |
||
69 | * |
||
70 | * @param string $index |
||
71 | * @param string $type |
||
72 | * @param string|null $version |
||
73 | * @param array $body |
||
74 | * @param array|null $ids |
||
75 | * @param array|null $parents |
||
76 | * @return array |
||
77 | */ |
||
78 | public function createDocumentsWithBulk( |
||
79 | string $index, |
||
80 | string $type, |
||
81 | string $version = null, |
||
82 | array $body = [], |
||
83 | ?array $ids = null, |
||
84 | ?array $parents = null |
||
85 | ) : array { |
||
86 | $params = []; |
||
87 | |||
88 | foreach ($body as $key => $b) { |
||
89 | $esIndex = [ |
||
90 | '_index' => $this->createIndexWithVersion($index, $version), |
||
91 | '_type' => $type, |
||
92 | ]; |
||
93 | |||
94 | if (is_array($ids) && count($body) == count($ids)) { |
||
95 | $esIndex['_id'] = $ids[$key]; |
||
96 | } |
||
97 | |||
98 | if (is_array($parents) && count($body) == count($parents)) { |
||
99 | $esIndex['parent'] = $parents[$key]; |
||
100 | } |
||
101 | |||
102 | $params['body'][] = [ |
||
103 | 'index' => $esIndex |
||
104 | ]; |
||
105 | |||
106 | $params['body'][] = $b; |
||
107 | } |
||
108 | |||
109 | $result = $this->getElasticsearchClient()->bulk($params); |
||
110 | |||
111 | return $result; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Delete document |
||
116 | * |
||
117 | * @param string $index |
||
118 | * @param string $type |
||
119 | * @param string $id |
||
120 | * @param string|null $version |
||
121 | * @param array $params |
||
122 | * @return array |
||
123 | */ |
||
124 | 1 | public function deleteDocument( |
|
125 | string $index, |
||
126 | string $type, |
||
127 | string $id, |
||
128 | string $version = null, |
||
129 | array $params = [] |
||
130 | ) : array { |
||
131 | 1 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
132 | 1 | $params['type'] = $type; |
|
133 | 1 | $params['id'] = $id; |
|
134 | |||
135 | 1 | $result = $this->getElasticsearchClient()->delete($params); |
|
136 | |||
137 | 1 | return $result; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Delete documents with bulk |
||
142 | * |
||
143 | * @param string $index |
||
144 | * @param string $type |
||
145 | * @param array $ids |
||
146 | * @param string|null $version |
||
147 | * @param array|null $parents |
||
148 | * @return array |
||
149 | */ |
||
150 | 1 | public function deleteDocumentsWithBulk( |
|
151 | string $index, |
||
152 | string $type, |
||
153 | array $ids, |
||
154 | string $version = null, |
||
155 | ?array $parents = null |
||
156 | ) : array { |
||
157 | 1 | $params = []; |
|
158 | |||
159 | 1 | foreach ($ids as $key => $id) { |
|
160 | $esIndex = [ |
||
161 | 1 | '_index' => $this->createIndexWithVersion($index, $version), |
|
162 | 1 | '_type' => $type, |
|
163 | 1 | '_id' => $id |
|
164 | ]; |
||
165 | |||
166 | 1 | if (is_array($parents) && count($ids) == count($parents)) { |
|
167 | $esIndex['parent'] = $parents[$key]; |
||
168 | } |
||
169 | |||
170 | 1 | $params['body'][] = [ |
|
171 | 1 | 'delete' => $esIndex |
|
172 | ]; |
||
173 | } |
||
174 | |||
175 | 1 | $result = $this->getElasticsearchClient()->bulk($params); |
|
176 | |||
177 | 1 | return $result; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Delete documents by query |
||
182 | * |
||
183 | * @param string $index |
||
184 | * @param string $type |
||
185 | * @param array $body |
||
186 | * @param string|null $version |
||
187 | * @param array $params |
||
188 | * @return array |
||
189 | */ |
||
190 | 1 | public function deleteDocumentsByQuery( |
|
191 | string $index, |
||
192 | string $type, |
||
193 | array $body = [], |
||
194 | string $version = null, |
||
195 | array $params = [] |
||
196 | ): array { |
||
197 | 1 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
198 | 1 | $params['type'] = $type; |
|
199 | 1 | $params['body'] = $body; |
|
200 | |||
201 | 1 | $result = $this->getElasticsearchClient()->deleteByQuery($params); |
|
202 | |||
203 | 1 | return $result; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get document |
||
208 | * |
||
209 | * @param string $index |
||
210 | * @param string $type |
||
211 | * @param string $id |
||
212 | * @param string|null $version |
||
213 | * @return array |
||
214 | */ |
||
215 | 3 | public function getDocument( |
|
216 | string $index, |
||
217 | string $type, |
||
218 | string $id, |
||
219 | string $version = null |
||
220 | ) : array { |
||
221 | 3 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
222 | 3 | $params['type'] = $type; |
|
223 | 3 | $params['id'] = $id; |
|
224 | |||
225 | 3 | $result = $this->getElasticsearchClient()->get($params); |
|
226 | |||
227 | 3 | return $result; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Mget documents |
||
232 | * |
||
233 | * @param string $index |
||
234 | * @param string $type |
||
235 | * @param array $params |
||
236 | * @param string|null $version |
||
237 | * @return array |
||
238 | */ |
||
239 | 1 | public function mgetDocuments( |
|
240 | string $index, |
||
241 | string $type, |
||
242 | array $params = [], |
||
243 | string $version = null |
||
244 | ) : array { |
||
245 | 1 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
246 | 1 | $params['type'] = $type; |
|
247 | |||
248 | 1 | $result = $this->getElasticsearchClient()->mget($params); |
|
249 | |||
250 | 1 | return $result; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * Exist document |
||
255 | * |
||
256 | * @param string $index |
||
257 | * @param string $type |
||
258 | * @param string $id |
||
259 | * @param array $params |
||
260 | * @param string|null $version |
||
261 | * @return bool |
||
262 | */ |
||
263 | 1 | public function existDocument( |
|
264 | string $index, |
||
265 | string $type, |
||
266 | string $id, |
||
267 | array $params = [], |
||
268 | string $version = null |
||
269 | ) : bool { |
||
270 | 1 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
271 | 1 | $params['type'] = $type; |
|
272 | 1 | $params['id'] = $id; |
|
273 | |||
274 | 1 | $result = (bool)$this->getElasticsearchClient()->exists($params); |
|
275 | |||
276 | 1 | return $result; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * Count document |
||
281 | * |
||
282 | * @param string $index |
||
283 | * @param string $type |
||
284 | * @param array $params |
||
285 | * @param string|null $version |
||
286 | * @return int |
||
287 | */ |
||
288 | 6 | public function countDocuments( |
|
289 | string $index, |
||
290 | string $type, |
||
291 | array $params = [], |
||
292 | string $version = null |
||
293 | ): int { |
||
294 | 6 | $params['index'] = $this->createIndexWithVersion($index, $version); |
|
295 | 6 | $params['type'] = $type; |
|
296 | |||
297 | 6 | $result = $this->getElasticsearchClient()->count($params); |
|
298 | |||
299 | 6 | return $result['count']; |
|
300 | } |
||
301 | } |
||
302 |