Completed
Push — develop ( bdcd74...c796db )
by Sam
13s
created

ReIndexStage   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 27 2
A __construct() 0 3 1
1
<?php
2
3
namespace Nord\Lumen\Elasticsearch\Pipelines\Stages;
4
5
use League\Pipeline\StageInterface;
6
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract;
7
8
/**
9
 * Class ReIndexStage
10
 * @package Nord\Lumen\Elasticsearch\Pipelines\Stages
11
 */
12
class ReIndexStage implements StageInterface
13
{
14
15
    /**
16
     * @var ElasticsearchServiceContract
17
     */
18
    private $elasticsearchService;
19
20
    /**
21
     * CheckIndexExistsStage constructor.
22
     *
23
     * @param ElasticsearchServiceContract $elasticsearchService
24
     */
25
    public function __construct(ElasticsearchServiceContract $elasticsearchService)
26
    {
27
        $this->elasticsearchService = $elasticsearchService;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function __invoke($payload)
34
    {
35
        // Reindex data from the old index to the new, but only if the old index exists (not true on brand new setups)
36
        if ($this->elasticsearchService->indices()->exists(['index' => $payload->getIndexName()])) {
37
            // Temporarily change some index settings to speed up the process
38
            $this->elasticsearchService->indices()->putSettings([
39
                'index' => $payload->getTargetVersionName(),
40
                'body'  => [
41
                    'refresh_interval'   => -1,
42
                    'number_of_replicas' => 0,
43
                ],
44
            ]);
45
46
            $this->elasticsearchService->reindex([
47
                'body' => [
48
                    'source' => [
49
                        'index' => $payload->getIndexName(),
50
                        'size'  => $payload->getBatchSize(),
51
                    ],
52
                    'dest'   => [
53
                        'index' => $payload->getTargetVersionName(),
54
                    ],
55
                ],
56
            ]);
57
        }
58
59
        return $payload;
60
    }
61
}
62