Completed
Pull Request — master (#589)
by
unknown
02:18
created

Manager::addIds()   D

Complexity

Conditions 9
Paths 10

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 30
rs 4.909
cc 9
eloc 19
nc 10
nop 2
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 Elasticsearch\Client;
15
use Elasticsearch\Common\Exceptions\Forbidden403Exception;
16
use Elasticsearch\Common\Exceptions\Missing404Exception;
17
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
18
use ONGR\ElasticsearchBundle\Result\AbstractResultsIterator;
19
use ONGR\ElasticsearchBundle\Result\Converter;
20
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
21
use ONGR\ElasticsearchBundle\Result\RawIterator;
22
use ONGR\ElasticsearchBundle\Result\Result;
23
use ONGR\ElasticsearchDSL\Search;
24
25
/**
26
 * Manager class.
27
 */
28
class Manager
29
{
30
    /**
31
     * @var string Manager name
32
     */
33
    private $name;
34
35
    /**
36
     * @var array Manager configuration
37
     */
38
    private $config = [];
39
40
    /**
41
     * @var Client
42
     */
43
    private $client;
44
45
    /**
46
     * @var Converter
47
     */
48
    private $converter;
49
50
    /**
51
     * @var bool
52
     */
53
    private $readOnly;
54
55
    /**
56
     * @var array Container for bulk queries
57
     */
58
    private $bulkQueries = [];
59
60
    /**
61
     * @var array Holder for consistency, refresh and replication parameters
62
     */
63
    private $bulkParams = [];
64
65
    /**
66
     * @var array
67
     */
68
    private $indexSettings;
69
70
    /**
71
     * @var MetadataCollector
72
     */
73
    private $metadataCollector;
74
75
    /**
76
     * After commit to make data available the refresh or flush operation is needed
77
     * so one of those methods has to be defined, the default is refresh.
78
     *
79
     * @var string
80
     */
81
    private $commitMode = 'refresh';
82
83
    /**
84
     * Reference to the persisted objects that need to be given an ID that returns from
85
     * elasticsearch
86
     *
87
     * @var array
88
     */
89
    private $persistedObjects = [];
90
91
    /**
92
     * The size that defines after how much document inserts call commit function.
93
     *
94
     * @var int
95
     */
96
    private $bulkCommitSize = 100;
97
98
    /**
99
     * Container to count how many documents was passed to the bulk query.
100
     *
101
     * @var int
102
     */
103
    private $bulkCount = 0;
104
105
    /**
106
     * @var Repository[] Repository local cache
107
     */
108
    private $repositories;
109
110
    /**
111
     * @param string            $name              Manager name
112
     * @param array             $config            Manager configuration
113
     * @param Client            $client
114
     * @param array             $indexSettings
115
     * @param MetadataCollector $metadataCollector
116
     * @param Converter         $converter
117
     */
118
    public function __construct(
119
        $name,
120
        array $config,
121
        $client,
122
        array $indexSettings,
123
        $metadataCollector,
124
        $converter
125
    ) {
126
        $this->name = $name;
127
        $this->config = $config;
128
        $this->client = $client;
129
        $this->indexSettings = $indexSettings;
130
        $this->metadataCollector = $metadataCollector;
131
        $this->converter = $converter;
132
133
        $this->setReadOnly($config['readonly']);
134
    }
135
136
    /**
137
     * Returns Elasticsearch connection.
138
     *
139
     * @return Client
140
     */
141
    public function getClient()
142
    {
143
        return $this->client;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getName()
150
    {
151
        return $this->name;
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    public function getConfig()
158
    {
159
        return $this->config;
160
    }
161
162
    /**
163
     * Returns repository by document class.
164
     *
165
     * @param string $className FQCN or string in Bundle:Document format
166
     *
167
     * @return Repository
168
     */
169
    public function getRepository($className)
170
    {
171
        if (!is_string($className)) {
172
            throw new \InvalidArgumentException('Document class must be a string.');
173
        }
174
175
        $namespace = $this->getMetadataCollector()->getClassName($className);
176
177
        if (isset($this->repositories[$namespace])) {
178
            return $this->repositories[$namespace];
179
        }
180
181
        $repository = $this->createRepository($namespace);
182
        $this->repositories[$namespace] = $repository;
183
184
        return $repository;
185
    }
186
187
    /**
188
     * @return MetadataCollector
189
     */
190
    public function getMetadataCollector()
191
    {
192
        return $this->metadataCollector;
193
    }
194
195
    /**
196
     * @return Converter
197
     */
198
    public function getConverter()
199
    {
200
        return $this->converter;
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getCommitMode()
207
    {
208
        return $this->commitMode;
209
    }
210
211
    /**
212
     * @param string $commitMode
213
     */
214
    public function setCommitMode($commitMode)
215
    {
216
        if ($commitMode === 'refresh' || $commitMode === 'flush' || $commitMode === 'none') {
217
            $this->commitMode = $commitMode;
218
        } else {
219
            throw new \LogicException('The commit method must be either refresh, flush or none.');
220
        }
221
    }
222
223
    /**
224
     * @return int
225
     */
226
    public function getBulkCommitSize()
227
    {
228
        return $this->bulkCommitSize;
229
    }
230
231
    /**
232
     * @param int $bulkCommitSize
233
     */
234
    public function setBulkCommitSize($bulkCommitSize)
235
    {
236
        $this->bulkCommitSize = $bulkCommitSize;
237
    }
238
239
    /**
240
     * Creates a repository.
241
     *
242
     * @param string $className
243
     *
244
     * @return Repository
245
     */
246
    private function createRepository($className)
247
    {
248
        return new Repository($this, $className);
249
    }
250
251
    /**
252
     * Executes search query in the index.
253
     *
254
     * @param array $types             List of types to search in.
255
     * @param array $query             Query to execute.
256
     * @param array $queryStringParams Query parameters.
257
     *
258
     * @return array
259
     */
260
    public function search(array $types, array $query, array $queryStringParams = [])
261
    {
262
        $params = [];
263
        $params['index'] = $this->getIndexName();
264
        $params['type'] = implode(',', $types);
265
        $params['body'] = $query;
266
267
        if (!empty($queryStringParams)) {
268
            $params = array_merge($queryStringParams, $params);
269
        }
270
271
        return $this->client->search($params);
272
    }
273
274
    /**
275
     * Adds document to next flush.
276
     *
277
     * @param object $document
278
     */
279
    public function persist($document)
280
    {
281
        $documentArray = $this->converter->convertToArray($document);
282
        $type = $this->getMetadataCollector()->getDocumentType(get_class($document));
283
284
        $this->bulk('index', $type, $documentArray);
285
        if (!isset($document->id)) {
286
            $this->persistedObjects[] = $document;
287
        }
288
    }
289
290
    /**
291
     * Adds document for removal.
292
     *
293
     * @param object $document
294
     */
295
    public function remove($document)
296
    {
297
        $data = $this->converter->convertToArray($document, [], ['_id']);
298
299
        if (!isset($data['_id'])) {
300
            throw new \LogicException(
301
                'In order to use remove() method document class must have property with @Id annotation.'
302
            );
303
        }
304
305
        $type = $this->getMetadataCollector()->getDocumentType(get_class($document));
306
307
        $this->bulk('delete', $type, ['_id' => $data['_id']]);
308
    }
309
310
    /**
311
     * Flushes elasticsearch index.
312
     *
313
     * @param array $params
314
     *
315
     * @return array
316
     */
317
    public function flush(array $params = [])
318
    {
319
        return $this->client->indices()->flush($params);
320
    }
321
322
    /**
323
     * Refreshes elasticsearch index.
324
     *
325
     * @param array $params
326
     *
327
     * @return array
328
     */
329
    public function refresh(array $params = [])
330
    {
331
        return $this->client->indices()->refresh($params);
332
    }
333
334
    /**
335
     * Adds ids to documents
336
     *
337
     * @param array $bulkQueries
338
     *
339
     * @param array $bulkResponse
340
     */
341
    public function addIds(array $bulkQueries, $bulkResponse = [])
342
    {
343
        if (empty($bulkResponse)) {
344
            $this->persistedObjects = [];
345
            return;
346
        }
347
        $indexing = [];
348
        foreach ($bulkQueries['body'] as $number => $query) {
349
            if (isset($query['index']) && !isset($query['index']['_id'])) {
350
                $indexing[] = $number / 2;
351
            }
352
        }
353
        if (isset($bulkQueries['body'][0]['index'])) {
354
            if (isset($this->persistedObjects)) {
355
                $i = 0;
356
                foreach ($this->persistedObjects as $document) {
357
                    $class = get_class($document);
358
                    $mapping = $this->metadataCollector->getMapping($class);
359
360
                    if (isset( $mapping['aliases']['_id'])) {
361
                        $id_property = $mapping['aliases']['_id']['propertyName'];
362
                        $document->$id_property = $bulkResponse['items'][$indexing[$i]]['create']['_id'];
363
                    }
364
365
                    $i++;
366
                }
367
            }
368
            $this->persistedObjects = [];
369
        }
370
    }
371
372
    /**
373
     * Inserts the current query container to the index, used for bulk queries execution.
374
     *
375
     * @param array $params Parameters that will be passed to the flush or refresh queries.
376
     *
377
     * @return null|array
378
     */
379
    public function commit(array $params = [])
380
    {
381
        $this->isReadOnly('Commit');
382
383
        if (!empty($this->bulkQueries)) {
384
            $bulkQueries = array_merge($this->bulkQueries, $this->bulkParams);
385
            $this->bulkQueries = [];
386
387
            $bulkResponse = $this->client->bulk($bulkQueries);
388
389
            switch ($this->getCommitMode()) {
390
                case 'flush':
391
                    $this->flush($params);
392
                    break;
393
                case 'refresh':
394
                    $this->refresh($params);
395
                    break;
396
            }
397
398
            $this->addIds($bulkQueries, $bulkResponse);
0 ignored issues
show
Documentation introduced by
$bulkResponse is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
399
400
            return $bulkResponse;
401
        }
402
        return null;
403
    }
404
405
    /**
406
     * Adds query to bulk queries container.
407
     *
408
     * @param string       $operation One of: index, update, delete, create.
409
     * @param string|array $type      Elasticsearch type name.
410
     * @param array        $query     DSL to execute.
411
     *
412
     * @throws \InvalidArgumentException
413
     */
414
    public function bulk($operation, $type, array $query)
415
    {
416
        $this->isReadOnly('Bulk');
417
418
        if (!in_array($operation, ['index', 'create', 'update', 'delete'])) {
419
            throw new \InvalidArgumentException('Wrong bulk operation selected');
420
        }
421
422
        $this->bulkQueries['body'][] = [
423
            $operation => array_filter(
424
                [
425
                    '_index' => $this->getIndexName(),
426
                    '_type' => $type,
427
                    '_id' => isset($query['_id']) ? $query['_id'] : null,
428
                    '_ttl' => isset($query['_ttl']) ? $query['_ttl'] : null,
429
                    '_parent' => isset($query['_parent']) ? $query['_parent'] : null,
430
                ]
431
            ),
432
        ];
433
        unset($query['_id'], $query['_ttl'], $query['_parent']);
434
435
        switch ($operation) {
436
            case 'index':
437
            case 'create':
438
            case 'update':
439
                $this->bulkQueries['body'][] = $query;
440
                break;
441
            case 'delete':
442
                // Body for delete operation is not needed to apply.
443
            default:
444
                // Do nothing.
445
                break;
446
        }
447
448
        // We are using counter because there is to difficult to resolve this from bulkQueries array.
449
        $this->bulkCount++;
450
451
        if ($this->bulkCommitSize === $this->bulkCount) {
452
            $this->commit();
453
            $this->bulkCount = 0;
454
        }
455
    }
456
457
    /**
458
     * Optional setter to change bulk query params.
459
     *
460
     * @param array $params Possible keys:
461
     *                      ['consistency'] = (enum) Explicit write consistency setting for the operation.
462
     *                      ['refresh']     = (boolean) Refresh the index after performing the operation.
463
     *                      ['replication'] = (enum) Explicitly set the replication type.
464
     */
465
    public function setBulkParams(array $params)
466
    {
467
        $this->bulkParams = $params;
468
    }
469
470
    /**
471
     * Creates fresh elasticsearch index.
472
     *
473
     * @param bool $noMapping Determines if mapping should be included.
474
     *
475
     * @return array
476
     */
477
    public function createIndex($noMapping = false)
478
    {
479
        $this->isReadOnly('Create index');
480
481
        if ($noMapping) {
482
            unset($this->indexSettings['body']['mappings']);
483
        }
484
485
        return $this->getClient()->indices()->create($this->indexSettings);
486
    }
487
488
    /**
489
     * Drops elasticsearch index.
490
     */
491
    public function dropIndex()
492
    {
493
        $this->isReadOnly('Drop index');
494
495
        return $this->getClient()->indices()->delete(['index' => $this->getIndexName()]);
496
    }
497
498
    /**
499
     * Tries to drop and create fresh elasticsearch index.
500
     *
501
     * @param bool $noMapping Determines if mapping should be included.
502
     *
503
     * @return array
504
     */
505
    public function dropAndCreateIndex($noMapping = false)
506
    {
507
        try {
508
            $this->dropIndex();
509
        } catch (\Exception $e) {
510
            // Do nothing, our target is to create new index.
511
        }
512
513
        return $this->createIndex($noMapping);
514
    }
515
516
    /**
517
     * Checks if connection index is already created.
518
     *
519
     * @return bool
520
     */
521
    public function indexExists()
522
    {
523
        return $this->getClient()->indices()->exists(['index' => $this->getIndexName()]);
524
    }
525
526
    /**
527
     * Returns index name this connection is attached to.
528
     *
529
     * @return string
530
     */
531
    public function getIndexName()
532
    {
533
        return $this->indexSettings['index'];
534
    }
535
536
    /**
537
     * Sets index name for this connection.
538
     *
539
     * @param string $name
540
     */
541
    public function setIndexName($name)
542
    {
543
        $this->indexSettings['index'] = $name;
544
    }
545
546
    /**
547
     * Returns Elasticsearch version number.
548
     *
549
     * @return string
550
     */
551
    public function getVersionNumber()
552
    {
553
        return $this->client->info()['version']['number'];
554
    }
555
556
    /**
557
     * Clears elasticsearch client cache.
558
     */
559
    public function clearCache()
560
    {
561
        $this->isReadOnly('Clear cache');
562
563
        $this->getClient()->indices()->clearCache(['index' => $this->getIndexName()]);
564
    }
565
566
    /**
567
     * Set connection to read only state.
568
     *
569
     * @param bool $readOnly
570
     */
571
    public function setReadOnly($readOnly)
572
    {
573
        $this->readOnly = $readOnly;
574
    }
575
576
    /**
577
     * Checks if connection is read only.
578
     *
579
     * @param string $message Error message.
580
     *
581
     * @throws Forbidden403Exception
582
     */
583
    public function isReadOnly($message = '')
584
    {
585
        if ($this->readOnly) {
586
            throw new Forbidden403Exception("Manager is readonly! {$message} operation is not permitted.");
587
        }
588
    }
589
590
    /**
591
     * Returns a single document by ID. Returns NULL if document was not found.
592
     *
593
     * @param string $className Document class name or Elasticsearch type name
594
     * @param string $id        Document ID to find
595
     *
596
     * @return object
597
     */
598
    public function find($className, $id)
599
    {
600
        $type = $this->resolveTypeName($className);
601
602
        $params = [
603
            'index' => $this->getIndexName(),
604
            'type' => $type,
605
            'id' => $id,
606
        ];
607
608
        try {
609
            $result = $this->getClient()->get($params);
610
        } catch (Missing404Exception $e) {
611
            return null;
612
        }
613
614
        return $this->getConverter()->convertToDocument($result, $this);
0 ignored issues
show
Documentation introduced by
$result is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
615
    }
616
617
    /**
618
     * Executes given search.
619
     *
620
     * @param array  $types
621
     * @param Search $search
622
     * @param string $resultsType
623
     *
624
     * @return DocumentIterator|RawIterator|array
625
     */
626
    public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT)
627
    {
628
        foreach ($types as &$type) {
629
            $type = $this->resolveTypeName($type);
630
        }
631
632
        $results = $this->search($types, $search->toArray(), $search->getQueryParams());
633
634
        return $this->parseResult($results, $resultsType, $search->getScroll());
0 ignored issues
show
Documentation introduced by
$results is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
635
    }
636
637
    /**
638
     * Parses raw result.
639
     *
640
     * @param array  $raw
641
     * @param string $resultsType
642
     * @param string $scrollDuration
643
     *
644
     * @return DocumentIterator|RawIterator|array
645
     *
646
     * @throws \Exception
647
     */
648
    private function parseResult($raw, $resultsType, $scrollDuration = null)
649
    {
650
        $scrollConfig = [];
651
        if (isset($raw['_scroll_id'])) {
652
            $scrollConfig['_scroll_id'] = $raw['_scroll_id'];
653
            $scrollConfig['duration'] = $scrollDuration;
654
        }
655
656
        switch ($resultsType) {
657
            case Result::RESULTS_OBJECT:
658
                return new DocumentIterator($raw, $this, $scrollConfig);
659
            case Result::RESULTS_ARRAY:
660
                return $this->convertToNormalizedArray($raw);
661
            case Result::RESULTS_RAW:
662
                return $raw;
663
            case Result::RESULTS_RAW_ITERATOR:
664
                return new RawIterator($raw, $this, $scrollConfig);
665
            default:
666
                throw new \Exception('Wrong results type selected');
667
        }
668
    }
669
670
    /**
671
     * Normalizes response array.
672
     *
673
     * @param array $data
674
     *
675
     * @return array
676
     */
677
    private function convertToNormalizedArray($data)
678
    {
679
        if (array_key_exists('_source', $data)) {
680
            return $data['_source'];
681
        }
682
683
        $output = [];
684
685
        if (isset($data['hits']['hits'][0]['_source'])) {
686
            foreach ($data['hits']['hits'] as $item) {
687
                $output[] = $item['_source'];
688
            }
689
        } elseif (isset($data['hits']['hits'][0]['fields'])) {
690
            foreach ($data['hits']['hits'] as $item) {
691
                $output[] = array_map('reset', $item['fields']);
692
            }
693
        }
694
695
        return $output;
696
    }
697
698
    /**
699
     * Fetches next set of results.
700
     *
701
     * @param string $scrollId
702
     * @param string $scrollDuration
703
     * @param string $resultsType
704
     *
705
     * @return AbstractResultsIterator
706
     *
707
     * @throws \Exception
708
     */
709
    public function scroll(
710
        $scrollId,
711
        $scrollDuration = '5m',
712
        $resultsType = Result::RESULTS_OBJECT
713
    ) {
714
        $results = $this->getClient()->scroll(['scroll_id' => $scrollId, 'scroll' => $scrollDuration]);
715
716
        return $this->parseResult($results, $resultsType, $scrollDuration);
0 ignored issues
show
Documentation introduced by
$results is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
717
    }
718
719
    /**
720
     * Clears scroll.
721
     *
722
     * @param string $scrollId
723
     */
724
    public function clearScroll($scrollId)
725
    {
726
        $this->getClient()->clearScroll(['scroll_id' => $scrollId]);
727
    }
728
729
    /**
730
     * Resolves type name by class name.
731
     *
732
     * @param string $className
733
     *
734
     * @return string
735
     */
736
    private function resolveTypeName($className)
737
    {
738
        if (strpos($className, ':') !== false || strpos($className, '\\') !== false) {
739
            return $this->getMetadataCollector()->getDocumentType($className);
740
        }
741
742
        return $className;
743
    }
744
}
745