1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Service; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchBundle\Result\ArrayIterator; |
15
|
|
|
use ONGR\ElasticsearchBundle\Result\RawIterator; |
16
|
|
|
use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery; |
17
|
|
|
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery; |
18
|
|
|
use ONGR\ElasticsearchDSL\Search; |
19
|
|
|
use ONGR\ElasticsearchDSL\Sort\FieldSort; |
20
|
|
|
use ONGR\ElasticsearchBundle\Result\DocumentIterator; |
21
|
|
|
use Elasticsearch\Client; |
22
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
23
|
|
|
use ONGR\ElasticsearchBundle\Event\Events; |
24
|
|
|
use ONGR\ElasticsearchBundle\Event\BulkEvent; |
25
|
|
|
use ONGR\ElasticsearchBundle\Event\CommitEvent; |
26
|
|
|
use ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException; |
27
|
|
|
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector; |
28
|
|
|
use ONGR\ElasticsearchBundle\Result\Converter; |
29
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
30
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Document repository class. |
34
|
|
|
*/ |
35
|
|
|
class Repository |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var string Fully qualified class namespace |
39
|
|
|
*/ |
40
|
|
|
private $classNamespace; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string Elasticsearch type name |
44
|
|
|
*/ |
45
|
|
|
private $type; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array Manager configuration |
49
|
|
|
*/ |
50
|
|
|
private $config = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Client |
54
|
|
|
*/ |
55
|
|
|
private $client; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Converter |
59
|
|
|
*/ |
60
|
|
|
private $converter; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array Container for bulk queries |
64
|
|
|
*/ |
65
|
|
|
private $bulkQueries = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array Holder for consistency, refresh and replication parameters |
69
|
|
|
*/ |
70
|
|
|
private $bulkParams = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
private $indexSettings; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var MetadataCollector |
79
|
|
|
*/ |
80
|
|
|
private $metadataCollector; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* After commit to make data available the refresh or flush operation is needed |
84
|
|
|
* so one of those methods has to be defined, the default is refresh. |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
private $commitMode = 'refresh'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The size that defines after how much document inserts call commit function. |
92
|
|
|
* |
93
|
|
|
* @var int |
94
|
|
|
*/ |
95
|
|
|
private $bulkCommitSize = 100; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Container to count how many documents was passed to the bulk query. |
99
|
|
|
* |
100
|
|
|
* @var int |
101
|
|
|
*/ |
102
|
|
|
private $bulkCount = 0; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var Repository[] Repository local cache |
106
|
|
|
*/ |
107
|
|
|
private $repositories; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var EventDispatcherInterface |
111
|
|
|
*/ |
112
|
|
|
private $eventDispatcher; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var Stopwatch |
116
|
|
|
*/ |
117
|
|
|
private $stopwatch; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $classNamespace |
121
|
|
|
* @param array $config |
122
|
|
|
* @param Client $client |
123
|
|
|
* @param array $indexSettings |
124
|
|
|
* @param MetadataCollector $metadataCollector |
125
|
|
|
* @param Converter $converter |
126
|
|
|
*/ |
127
|
|
|
public function __construct( |
128
|
|
|
$classNamespace, |
129
|
|
|
array $config, |
130
|
|
|
$client, |
131
|
|
|
array $indexSettings, |
132
|
|
|
$metadataCollector, |
133
|
|
|
$converter |
134
|
|
|
) { |
135
|
|
|
if (!class_exists($classNamespace)) { |
136
|
|
|
throw new \InvalidArgumentException( |
137
|
|
|
sprintf('Cannot create repository for non-existing class "%s".', $classNamespace) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$this->classNamespace = $classNamespace; |
142
|
|
|
$this->config = $config; |
143
|
|
|
$this->client = $client; |
144
|
|
|
$this->indexSettings = $indexSettings; |
145
|
|
|
$this->metadataCollector = $metadataCollector; |
146
|
|
|
$this->converter = $converter; |
147
|
|
|
$this->type = $this->resolveType($classNamespace); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function getType() |
154
|
|
|
{ |
155
|
|
|
return $this->type; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function createSearch() :Search |
159
|
|
|
{ |
160
|
|
|
return new Search(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns a single document by ID. Returns NULL if document was not found. |
165
|
|
|
* |
166
|
|
|
* @param string $id Document ID to find |
167
|
|
|
* @param string $routing Custom routing for the document |
168
|
|
|
* |
169
|
|
|
* @return object |
170
|
|
|
*/ |
171
|
|
|
public function find($id, $routing = null) |
172
|
|
|
{ |
173
|
|
|
$params = [ |
174
|
|
|
'index' => $this->getIndexName(), |
175
|
|
|
'type' => $this->getType(), |
176
|
|
|
'id' => $id, |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
if ($routing) { |
|
|
|
|
180
|
|
|
$params['routing'] = $routing; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$result = $this->getClient()->get($params); |
184
|
|
|
|
185
|
|
|
return $this->getConverter()->convertToDocument($result, $this); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Finds documents by a set of criteria. |
190
|
|
|
* |
191
|
|
|
* @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
192
|
|
|
* @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
193
|
|
|
* @param int|null $limit Example: 5. |
194
|
|
|
* @param int|null $offset Example: 30. |
195
|
|
|
*/ |
196
|
|
|
public function findBy( |
197
|
|
|
array $criteria, |
198
|
|
|
array $orderBy = [], |
199
|
|
|
$limit = null, |
200
|
|
|
$offset = null |
201
|
|
|
):DocumentIterator { |
202
|
|
|
$search = $this->createSearch(); |
203
|
|
|
|
204
|
|
|
if ($limit !== null) { |
205
|
|
|
$search->setSize($limit); |
206
|
|
|
} |
207
|
|
|
if ($offset !== null) { |
208
|
|
|
$search->setFrom($offset); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
foreach ($criteria as $field => $value) { |
212
|
|
|
if (preg_match('/^!(.+)$/', $field)) { |
213
|
|
|
$boolType = BoolQuery::MUST_NOT; |
214
|
|
|
$field = preg_replace('/^!/', '', $field); |
215
|
|
|
} else { |
216
|
|
|
$boolType = BoolQuery::MUST; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$search->addQuery( |
220
|
|
|
new QueryStringQuery(is_array($value) ? implode(' OR ', $value) : $value, ['default_field' => $field]), |
221
|
|
|
$boolType |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
foreach ($orderBy as $field => $direction) { |
226
|
|
|
$search->addSort(new FieldSort($field, $direction)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->findDocuments($search); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Finds a single document by a set of criteria. |
234
|
|
|
* |
235
|
|
|
* @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
236
|
|
|
* @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
237
|
|
|
*/ |
238
|
|
|
public function findOneBy(array $criteria, array $orderBy = []) |
239
|
|
|
{ |
240
|
|
|
return $this->findBy($criteria, $orderBy, 1, null)->current(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
View Code Duplication |
public function findDocuments(Search $search) :DocumentIterator |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
$results = $this->executeSearch($search); |
246
|
|
|
|
247
|
|
|
return new DocumentIterator( |
248
|
|
|
$results, |
249
|
|
|
$this->getManager(), |
|
|
|
|
250
|
|
|
$this->getScrollConfiguration($results, $search->getScroll()) |
251
|
|
|
); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
View Code Duplication |
public function findArray(Search $search):ArrayIterator |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
$results = $this->executeSearch($search); |
257
|
|
|
|
258
|
|
|
return new ArrayIterator( |
259
|
|
|
$results, |
260
|
|
|
$this->getManager(), |
|
|
|
|
261
|
|
|
$this->getScrollConfiguration($results, $search->getScroll()) |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
View Code Duplication |
public function findRaw(Search $search):RawIterator |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$results = $this->executeSearch($search); |
268
|
|
|
|
269
|
|
|
return new RawIterator( |
270
|
|
|
$results, |
271
|
|
|
$this->getManager(), |
|
|
|
|
272
|
|
|
$this->getScrollConfiguration($results, $search->getScroll()) |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
private function executeSearch(Search $search):array |
277
|
|
|
{ |
278
|
|
|
return $this->getManager()->search([$this->getType()], $search->toArray(), $search->getUriParams()); |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function getScrollConfiguration(array $raw, string $scrollDuration):array |
282
|
|
|
{ |
283
|
|
|
$scrollConfig = []; |
284
|
|
|
if (isset($raw['_scroll_id'])) { |
285
|
|
|
$scrollConfig['_scroll_id'] = $raw['_scroll_id']; |
286
|
|
|
$scrollConfig['duration'] = $scrollDuration; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $scrollConfig; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function count(Search $search, array $params = []):int |
293
|
|
|
{ |
294
|
|
|
$body = array_merge( |
295
|
|
|
[ |
296
|
|
|
'index' => $this->getIndexName(), |
297
|
|
|
'type' => $this->type, |
298
|
|
|
'body' => $search->toArray(), |
299
|
|
|
], |
300
|
|
|
$params |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
$results = $this |
304
|
|
|
->getClient()->count($body); |
305
|
|
|
|
306
|
|
|
return $results['count']; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function remove($id, $routing = null):array |
310
|
|
|
{ |
311
|
|
|
$params = [ |
312
|
|
|
'index' => $this->getManager()->getIndexName(), |
|
|
|
|
313
|
|
|
'type' => $this->type, |
314
|
|
|
'id' => $id, |
315
|
|
|
]; |
316
|
|
|
|
317
|
|
|
if ($routing) { |
318
|
|
|
$params['routing'] = $routing; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$response = $this->getManager()->getClient()->delete($params); |
|
|
|
|
322
|
|
|
|
323
|
|
|
return $response; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function update(string $id, array $fields = [], string $script = null, array $params = []):array |
327
|
|
|
{ |
328
|
|
|
$body = array_filter( |
329
|
|
|
[ |
330
|
|
|
'doc' => $fields, |
331
|
|
|
'script' => $script, |
332
|
|
|
] |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
$params = array_merge( |
336
|
|
|
[ |
337
|
|
|
'id' => $id, |
338
|
|
|
'index' => $this->getManager()->getIndexName(), |
|
|
|
|
339
|
|
|
'type' => $this->type, |
340
|
|
|
'body' => $body, |
341
|
|
|
], |
342
|
|
|
$params |
343
|
|
|
); |
344
|
|
|
|
345
|
|
|
return $this->getManager()->getClient()->update($params); |
|
|
|
|
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
private function resolveType($className):string |
349
|
|
|
{ |
350
|
|
|
return $this->getMetadataCollector()->getDocumentType($className); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function getClassNamespace():string |
354
|
|
|
{ |
355
|
|
|
return $this->classNamespace; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
public function getClient():Client |
359
|
|
|
{ |
360
|
|
|
return $this->client; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
public function getConfig():array |
364
|
|
|
{ |
365
|
|
|
return $this->config; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
public function getMetadataCollector():MetadataCollector |
369
|
|
|
{ |
370
|
|
|
return $this->metadataCollector; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function getConverter():Converter |
374
|
|
|
{ |
375
|
|
|
return $this->converter; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
public function getCommitMode():string |
379
|
|
|
{ |
380
|
|
|
return $this->commitMode; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function setCommitMode(string $commitMode) |
384
|
|
|
{ |
385
|
|
|
if ($commitMode === 'refresh' || $commitMode === 'flush' || $commitMode === 'none') { |
386
|
|
|
$this->commitMode = $commitMode; |
387
|
|
|
} else { |
388
|
|
|
throw new \LogicException('The commit method must be either refresh, flush or none.'); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function getBulkCommitSize():int |
393
|
|
|
{ |
394
|
|
|
return $this->bulkCommitSize; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
public function setBulkCommitSize(int $bulkCommitSize) |
398
|
|
|
{ |
399
|
|
|
$this->bulkCommitSize = $bulkCommitSize; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
public function search(array $types, array $query, array $queryStringParams = []):array |
403
|
|
|
{ |
404
|
|
|
$params = []; |
405
|
|
|
$params['index'] = $this->getIndexName(); |
406
|
|
|
|
407
|
|
|
$resolvedTypes = []; |
408
|
|
|
foreach ($types as $type) { |
409
|
|
|
$resolvedTypes[] = $this->resolveTypeName($type); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
if (!empty($resolvedTypes)) { |
413
|
|
|
$params['type'] = implode(',', $resolvedTypes); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
$params['body'] = $query; |
417
|
|
|
|
418
|
|
|
if (!empty($queryStringParams)) { |
419
|
|
|
$params = array_merge($queryStringParams, $params); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$this->stopwatch('start', 'search'); |
423
|
|
|
$result = $this->client->search($params); |
424
|
|
|
$this->stopwatch('stop', 'search'); |
425
|
|
|
|
426
|
|
|
return $result; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
public function msearch(array $body):array |
430
|
|
|
{ |
431
|
|
|
$result = $this->client->msearch( |
432
|
|
|
[ |
433
|
|
|
'index' => $this->getIndexName(), // set default index |
434
|
|
|
'body' => $body |
435
|
|
|
] |
436
|
|
|
); |
437
|
|
|
return $result; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
public function persist($document):array |
441
|
|
|
{ |
442
|
|
|
$documentArray = $this->converter->convertToArray($document); |
443
|
|
|
$type = $this->getMetadataCollector()->getDocumentType(get_class($document)); |
444
|
|
|
|
445
|
|
|
$this->bulk('index', $type, $documentArray); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function flush(array $params = []):array |
449
|
|
|
{ |
450
|
|
|
return $this->client->indices()->flush(array_merge(['index' => $this->getIndexName()], $params)); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
public function refresh(array $params = []):array |
454
|
|
|
{ |
455
|
|
|
return $this->client->indices()->refresh(array_merge(['index' => $this->getIndexName()], $params)); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
public function commit(array $params = []):array |
459
|
|
|
{ |
460
|
|
|
if (!empty($this->bulkQueries)) { |
461
|
|
|
$bulkQueries = array_merge($this->bulkQueries, $this->bulkParams); |
462
|
|
|
$bulkQueries['index']['_index'] = $this->getIndexName(); |
463
|
|
|
$this->eventDispatcher->dispatch( |
464
|
|
|
Events::PRE_COMMIT, |
465
|
|
|
new CommitEvent($this->getCommitMode(), $bulkQueries) |
466
|
|
|
); |
467
|
|
|
|
468
|
|
|
$this->stopwatch('start', 'bulk'); |
469
|
|
|
$bulkResponse = $this->client->bulk($bulkQueries); |
470
|
|
|
$this->stopwatch('stop', 'bulk'); |
471
|
|
|
|
472
|
|
|
if ($bulkResponse['errors']) { |
473
|
|
|
throw new BulkWithErrorsException( |
474
|
|
|
json_encode($bulkResponse), |
475
|
|
|
0, |
476
|
|
|
null, |
477
|
|
|
$bulkResponse |
|
|
|
|
478
|
|
|
); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$this->bulkQueries = []; |
482
|
|
|
$this->bulkCount = 0; |
483
|
|
|
|
484
|
|
|
$this->stopwatch('start', 'refresh'); |
485
|
|
|
|
486
|
|
|
switch ($this->getCommitMode()) { |
487
|
|
|
case 'flush': |
488
|
|
|
$this->flush($params); |
489
|
|
|
break; |
490
|
|
|
case 'refresh': |
491
|
|
|
$this->refresh($params); |
492
|
|
|
break; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
$this->eventDispatcher->dispatch( |
496
|
|
|
Events::POST_COMMIT, |
497
|
|
|
new CommitEvent($this->getCommitMode(), $bulkResponse) |
|
|
|
|
498
|
|
|
); |
499
|
|
|
|
500
|
|
|
$this->stopwatch('stop', 'refresh'); |
501
|
|
|
|
502
|
|
|
return $bulkResponse; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
return []; |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
public function bulk(string $operation, string $type, array $query):array |
509
|
|
|
{ |
510
|
|
|
if (!in_array($operation, ['index', 'create', 'update', 'delete'])) { |
511
|
|
|
throw new \InvalidArgumentException('Wrong bulk operation selected'); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
$this->eventDispatcher->dispatch( |
515
|
|
|
Events::BULK, |
516
|
|
|
new BulkEvent($operation, $type, $query) |
517
|
|
|
); |
518
|
|
|
|
519
|
|
|
$this->bulkQueries['body'][] = [ |
520
|
|
|
$operation => array_filter( |
521
|
|
|
[ |
522
|
|
|
'_type' => $type, |
523
|
|
|
'_id' => isset($query['_id']) ? $query['_id'] : null, |
524
|
|
|
'_routing' => isset($query['_routing']) ? $query['_routing'] : null, |
525
|
|
|
'_parent' => isset($query['_parent']) ? $query['_parent'] : null, |
526
|
|
|
] |
527
|
|
|
), |
528
|
|
|
]; |
529
|
|
|
unset($query['_id'], $query['_ttl'], $query['_parent'], $query['_routing']); |
530
|
|
|
|
531
|
|
|
switch ($operation) { |
532
|
|
|
case 'index': |
533
|
|
|
case 'create': |
534
|
|
|
case 'update': |
535
|
|
|
$this->bulkQueries['body'][] = $query; |
536
|
|
|
break; |
537
|
|
|
case 'delete': |
538
|
|
|
// Body for delete operation is not needed to apply. |
539
|
|
|
default: |
540
|
|
|
// Do nothing. |
541
|
|
|
break; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
// We are using counter because there is to difficult to resolve this from bulkQueries array. |
545
|
|
|
$this->bulkCount++; |
546
|
|
|
|
547
|
|
|
$response = []; |
548
|
|
|
|
549
|
|
|
if ($this->bulkCommitSize === $this->bulkCount) { |
550
|
|
|
$response = $this->commit(); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
return $response; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
public function setBulkParams(array $params) |
557
|
|
|
{ |
558
|
|
|
$this->bulkParams = $params; |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
public function createIndex(bool $noMapping = false):array |
562
|
|
|
{ |
563
|
|
|
if ($noMapping) { |
564
|
|
|
unset($this->indexSettings['body']['mappings']); |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
return $this->getClient()->indices()->create($this->indexSettings); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Drops elasticsearch index. |
572
|
|
|
*/ |
573
|
|
|
public function dropIndex():array |
574
|
|
|
{ |
575
|
|
|
return $this->getClient()->indices()->delete(['index' => $this->getIndexName()]); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* Tries to drop and create fresh elasticsearch index. |
580
|
|
|
*/ |
581
|
|
|
public function dropAndCreateIndex(bool $noMapping = false):array |
582
|
|
|
{ |
583
|
|
|
try { |
584
|
|
|
if ($this->indexExists()) { |
585
|
|
|
$this->dropIndex(); |
586
|
|
|
} |
587
|
|
|
} catch (\Exception $e) { |
588
|
|
|
// Do nothing, our target is to create new index. |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
return $this->createIndex($noMapping); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
public function indexExists() |
595
|
|
|
{ |
596
|
|
|
return $this->getClient()->indices()->exists(['index' => $this->getIndexName()]); |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
public function getIndexName():string |
600
|
|
|
{ |
601
|
|
|
return $this->indexSettings['index']; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
public function setIndexName(string $name) |
605
|
|
|
{ |
606
|
|
|
$this->indexSettings['index'] = $name; |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
public function getIndexMappings():array |
610
|
|
|
{ |
611
|
|
|
return $this->indexSettings['body']['mappings']; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Returns Elasticsearch version number. |
616
|
|
|
*/ |
617
|
|
|
public function getVersionNumber():string |
618
|
|
|
{ |
619
|
|
|
return $this->client->info()['version']['number']; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
/** |
623
|
|
|
* Clears elasticsearch index cache. |
624
|
|
|
*/ |
625
|
|
|
public function clearIndexCache():array |
626
|
|
|
{ |
627
|
|
|
return $this->getClient()->indices()->clearCache(['index' => $this->getIndexName()]); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
/** |
631
|
|
|
* Fetches next scroll batch of results. |
632
|
|
|
*/ |
633
|
|
|
public function scroll(string $scrollId, string $scrollDuration = '5m'):array |
634
|
|
|
{ |
635
|
|
|
$results = $this->getClient()->scroll(['scroll_id' => $scrollId, 'scroll' => $scrollDuration]); |
636
|
|
|
|
637
|
|
|
return $results; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Clears scroll. |
642
|
|
|
*/ |
643
|
|
|
public function clearScroll(string $scrollId):array |
644
|
|
|
{ |
645
|
|
|
return $this->getClient()->clearScroll(['scroll_id' => $scrollId]); |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Calls "Get Settings API" in Elasticsearch and will return you the currently configured settings. |
650
|
|
|
* |
651
|
|
|
* return array |
652
|
|
|
*/ |
653
|
|
|
public function getSettings():array |
654
|
|
|
{ |
655
|
|
|
return $this->getClient()->indices()->getSettings(['index' => $this->getIndexName()]); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Gets Elasticsearch aliases information. |
660
|
|
|
* @param $params |
661
|
|
|
* |
662
|
|
|
* @return array |
663
|
|
|
*/ |
664
|
|
|
public function getAliases($params = []):array |
665
|
|
|
{ |
666
|
|
|
return $this->getClient()->indices()->getAliases(array_merge(['index' => $this->getIndexName()], $params)); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Resolves type name by class name. |
671
|
|
|
*/ |
672
|
|
|
private function resolveTypeName($classNamespace):string |
673
|
|
|
{ |
674
|
|
|
return $this->getMetadataCollector()->getDocumentType($classNamespace); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Starts and stops an event in the stopwatch |
679
|
|
|
* |
680
|
|
|
* @param string $action only 'start' and 'stop' |
681
|
|
|
* @param string $name name of the event |
682
|
|
|
*/ |
683
|
|
|
private function stopwatch($action, $name) |
684
|
|
|
{ |
685
|
|
|
if (isset($this->stopwatch)) { |
686
|
|
|
$this->stopwatch->$action('ongr_es: '.$name, 'ongr_es'); |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: