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

Manager::addIds()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 24
rs 5.7377
cc 8
eloc 15
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
354
        if(isset($bulkQueries['body'][0]['index'])) {
355
            if (isset($this->persistedObjects)) {
356
                $i = 0;
357
                foreach ($this->persistedObjects as $document) {
358
                    $document->id = $bulkResponse['items'][$indexing[$i]]['create']['_id'];
359
                    $i++;
360
                }
361
            }
362
            $this->persistedObjects = [];
363
        }
364
    }
365
366
    /**
367
     * Inserts the current query container to the index, used for bulk queries execution.
368
     *
369
     * @param array $params Parameters that will be passed to the flush or refresh queries.
370
     *
371
     * @return null|array
372
     */
373
    public function commit(array $params = [])
374
    {
375
        $this->isReadOnly('Commit');
376
377
        if (!empty($this->bulkQueries)) {
378
            $bulkQueries = array_merge($this->bulkQueries, $this->bulkParams);
379
            $this->bulkQueries = [];
380
381
            $bulkResponse = $this->client->bulk($bulkQueries);
382
383
            switch ($this->getCommitMode()) {
384
                case 'flush':
385
                    $this->flush($params);
386
                    break;
387
                case 'refresh':
388
                    $this->refresh($params);
389
                    break;
390
            }
391
392
            $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...
393
394
            return $bulkResponse;
395
        }
396
        return null;
397
    }
398
399
    /**
400
     * Adds query to bulk queries container.
401
     *
402
     * @param string       $operation One of: index, update, delete, create.
403
     * @param string|array $type      Elasticsearch type name.
404
     * @param array        $query     DSL to execute.
405
     *
406
     * @throws \InvalidArgumentException
407
     */
408
    public function bulk($operation, $type, array $query)
409
    {
410
        $this->isReadOnly('Bulk');
411
412
        if (!in_array($operation, ['index', 'create', 'update', 'delete'])) {
413
            throw new \InvalidArgumentException('Wrong bulk operation selected');
414
        }
415
416
        $this->bulkQueries['body'][] = [
417
            $operation => array_filter(
418
                [
419
                    '_index' => $this->getIndexName(),
420
                    '_type' => $type,
421
                    '_id' => isset($query['_id']) ? $query['_id'] : null,
422
                    '_ttl' => isset($query['_ttl']) ? $query['_ttl'] : null,
423
                    '_parent' => isset($query['_parent']) ? $query['_parent'] : null,
424
                ]
425
            ),
426
        ];
427
        unset($query['_id'], $query['_ttl'], $query['_parent']);
428
429
        switch ($operation) {
430
            case 'index':
431
            case 'create':
432
            case 'update':
433
                $this->bulkQueries['body'][] = $query;
434
                break;
435
            case 'delete':
436
                // Body for delete operation is not needed to apply.
437
            default:
438
                // Do nothing.
439
                break;
440
        }
441
442
        // We are using counter because there is to difficult to resolve this from bulkQueries array.
443
        $this->bulkCount++;
444
445
        if ($this->bulkCommitSize === $this->bulkCount) {
446
            $this->commit();
447
            $this->bulkCount = 0;
448
        }
449
    }
450
451
    /**
452
     * Optional setter to change bulk query params.
453
     *
454
     * @param array $params Possible keys:
455
     *                      ['consistency'] = (enum) Explicit write consistency setting for the operation.
456
     *                      ['refresh']     = (boolean) Refresh the index after performing the operation.
457
     *                      ['replication'] = (enum) Explicitly set the replication type.
458
     */
459
    public function setBulkParams(array $params)
460
    {
461
        $this->bulkParams = $params;
462
    }
463
464
    /**
465
     * Creates fresh elasticsearch index.
466
     *
467
     * @param bool $noMapping Determines if mapping should be included.
468
     *
469
     * @return array
470
     */
471
    public function createIndex($noMapping = false)
472
    {
473
        $this->isReadOnly('Create index');
474
475
        if ($noMapping) {
476
            unset($this->indexSettings['body']['mappings']);
477
        }
478
479
        return $this->getClient()->indices()->create($this->indexSettings);
480
    }
481
482
    /**
483
     * Drops elasticsearch index.
484
     */
485
    public function dropIndex()
486
    {
487
        $this->isReadOnly('Drop index');
488
489
        return $this->getClient()->indices()->delete(['index' => $this->getIndexName()]);
490
    }
491
492
    /**
493
     * Tries to drop and create fresh elasticsearch index.
494
     *
495
     * @param bool $noMapping Determines if mapping should be included.
496
     *
497
     * @return array
498
     */
499
    public function dropAndCreateIndex($noMapping = false)
500
    {
501
        try {
502
            $this->dropIndex();
503
        } catch (\Exception $e) {
504
            // Do nothing, our target is to create new index.
505
        }
506
507
        return $this->createIndex($noMapping);
508
    }
509
510
    /**
511
     * Checks if connection index is already created.
512
     *
513
     * @return bool
514
     */
515
    public function indexExists()
516
    {
517
        return $this->getClient()->indices()->exists(['index' => $this->getIndexName()]);
518
    }
519
520
    /**
521
     * Returns index name this connection is attached to.
522
     *
523
     * @return string
524
     */
525
    public function getIndexName()
526
    {
527
        return $this->indexSettings['index'];
528
    }
529
530
    /**
531
     * Sets index name for this connection.
532
     *
533
     * @param string $name
534
     */
535
    public function setIndexName($name)
536
    {
537
        $this->indexSettings['index'] = $name;
538
    }
539
540
    /**
541
     * Returns Elasticsearch version number.
542
     *
543
     * @return string
544
     */
545
    public function getVersionNumber()
546
    {
547
        return $this->client->info()['version']['number'];
548
    }
549
550
    /**
551
     * Clears elasticsearch client cache.
552
     */
553
    public function clearCache()
554
    {
555
        $this->isReadOnly('Clear cache');
556
557
        $this->getClient()->indices()->clearCache(['index' => $this->getIndexName()]);
558
    }
559
560
    /**
561
     * Set connection to read only state.
562
     *
563
     * @param bool $readOnly
564
     */
565
    public function setReadOnly($readOnly)
566
    {
567
        $this->readOnly = $readOnly;
568
    }
569
570
    /**
571
     * Checks if connection is read only.
572
     *
573
     * @param string $message Error message.
574
     *
575
     * @throws Forbidden403Exception
576
     */
577
    public function isReadOnly($message = '')
578
    {
579
        if ($this->readOnly) {
580
            throw new Forbidden403Exception("Manager is readonly! {$message} operation is not permitted.");
581
        }
582
    }
583
584
    /**
585
     * Returns a single document by ID. Returns NULL if document was not found.
586
     *
587
     * @param string $className Document class name or Elasticsearch type name
588
     * @param string $id        Document ID to find
589
     *
590
     * @return object
591
     */
592
    public function find($className, $id)
593
    {
594
        $type = $this->resolveTypeName($className);
595
596
        $params = [
597
            'index' => $this->getIndexName(),
598
            'type' => $type,
599
            'id' => $id,
600
        ];
601
602
        try {
603
            $result = $this->getClient()->get($params);
604
        } catch (Missing404Exception $e) {
605
            return null;
606
        }
607
608
        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...
609
    }
610
611
    /**
612
     * Executes given search.
613
     *
614
     * @param array  $types
615
     * @param Search $search
616
     * @param string $resultsType
617
     *
618
     * @return DocumentIterator|RawIterator|array
619
     */
620
    public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT)
621
    {
622
        foreach ($types as &$type) {
623
            $type = $this->resolveTypeName($type);
624
        }
625
626
        $results = $this->search($types, $search->toArray(), $search->getQueryParams());
627
628
        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...
629
    }
630
631
    /**
632
     * Parses raw result.
633
     *
634
     * @param array  $raw
635
     * @param string $resultsType
636
     * @param string $scrollDuration
637
     *
638
     * @return DocumentIterator|RawIterator|array
639
     *
640
     * @throws \Exception
641
     */
642
    private function parseResult($raw, $resultsType, $scrollDuration = null)
643
    {
644
        $scrollConfig = [];
645
        if (isset($raw['_scroll_id'])) {
646
            $scrollConfig['_scroll_id'] = $raw['_scroll_id'];
647
            $scrollConfig['duration'] = $scrollDuration;
648
        }
649
650
        switch ($resultsType) {
651
            case Result::RESULTS_OBJECT:
652
                return new DocumentIterator($raw, $this, $scrollConfig);
653
            case Result::RESULTS_ARRAY:
654
                return $this->convertToNormalizedArray($raw);
655
            case Result::RESULTS_RAW:
656
                return $raw;
657
            case Result::RESULTS_RAW_ITERATOR:
658
                return new RawIterator($raw, $this, $scrollConfig);
659
            default:
660
                throw new \Exception('Wrong results type selected');
661
        }
662
    }
663
664
    /**
665
     * Normalizes response array.
666
     *
667
     * @param array $data
668
     *
669
     * @return array
670
     */
671
    private function convertToNormalizedArray($data)
672
    {
673
        if (array_key_exists('_source', $data)) {
674
            return $data['_source'];
675
        }
676
677
        $output = [];
678
679
        if (isset($data['hits']['hits'][0]['_source'])) {
680
            foreach ($data['hits']['hits'] as $item) {
681
                $output[] = $item['_source'];
682
            }
683
        } elseif (isset($data['hits']['hits'][0]['fields'])) {
684
            foreach ($data['hits']['hits'] as $item) {
685
                $output[] = array_map('reset', $item['fields']);
686
            }
687
        }
688
689
        return $output;
690
    }
691
692
    /**
693
     * Fetches next set of results.
694
     *
695
     * @param string $scrollId
696
     * @param string $scrollDuration
697
     * @param string $resultsType
698
     *
699
     * @return AbstractResultsIterator
700
     *
701
     * @throws \Exception
702
     */
703
    public function scroll(
704
        $scrollId,
705
        $scrollDuration = '5m',
706
        $resultsType = Result::RESULTS_OBJECT
707
    ) {
708
        $results = $this->getClient()->scroll(['scroll_id' => $scrollId, 'scroll' => $scrollDuration]);
709
710
        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...
711
    }
712
713
    /**
714
     * Clears scroll.
715
     *
716
     * @param string $scrollId
717
     */
718
    public function clearScroll($scrollId)
719
    {
720
        $this->getClient()->clearScroll(['scroll_id' => $scrollId]);
721
    }
722
723
    /**
724
     * Resolves type name by class name.
725
     *
726
     * @param string $className
727
     *
728
     * @return string
729
     */
730
    private function resolveTypeName($className)
731
    {
732
        if (strpos($className, ':') !== false || strpos($className, '\\') !== false) {
733
            return $this->getMetadataCollector()->getDocumentType($className);
734
        }
735
736
        return $className;
737
    }
738
}
739