Passed
Push — trunk ( 325349...f71779 )
by Christian
12:37 queued 16s
created

IndexCreator::addFullText()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 1
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Elasticsearch\Framework\Indexing;
4
5
use OpenSearch\Client;
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\Log\Package;
9
use Shopware\Elasticsearch\Framework\AbstractElasticsearchDefinition;
10
use Shopware\Elasticsearch\Framework\Indexing\Event\ElasticsearchIndexConfigEvent;
11
use Shopware\Elasticsearch\Framework\Indexing\Event\ElasticsearchIndexCreatedEvent;
12
13
#[Package('core')]
14
class IndexCreator
15
{
16
    /**
17
     * @var array<mixed>
18
     */
19
    private readonly array $config;
20
21
    /**
22
     * @internal
23
     *
24
     * @param array<mixed> $config
25
     * @param array<mixed> $mapping
26
     */
27
    public function __construct(
28
        private readonly Client $client,
29
        array $config,
30
        private readonly array $mapping,
31
        private readonly EventDispatcherInterface $eventDispatcher
32
    ) {
33
        if (isset($config['settings']['index'])) {
34
            if (\array_key_exists('number_of_shards', $config['settings']['index']) && $config['settings']['index']['number_of_shards'] === null) {
35
                unset($config['settings']['index']['number_of_shards']);
36
            }
37
38
            if (\array_key_exists('number_of_replicas', $config['settings']['index']) && $config['settings']['index']['number_of_replicas'] === null) {
39
                unset($config['settings']['index']['number_of_replicas']);
40
            }
41
        }
42
43
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config is declared read-only in Shopware\Elasticsearch\F...k\Indexing\IndexCreator.
Loading history...
44
    }
45
46
    public function createIndex(AbstractElasticsearchDefinition $definition, string $index, string $alias, Context $context): void
47
    {
48
        // NEXT-21735 - does not execute if there's no index yet
49
        // @codeCoverageIgnoreStart
50
        if ($this->indexExists($index)) {
51
            $this->client->indices()->delete(['index' => $index]);
52
        }
53
        // @codeCoverageIgnoreEnd
54
55
        $mapping = $definition->getMapping($context);
56
57
        $mapping = array_merge_recursive($mapping, $this->mapping);
58
59
        $body = array_merge(
60
            $this->config,
61
            ['mappings' => $mapping]
62
        );
63
64
        $event = new ElasticsearchIndexConfigEvent($index, $body, $definition, $context);
65
        $this->eventDispatcher->dispatch($event);
66
67
        $this->client->indices()->create([
68
            'index' => $index,
69
            'body' => $event->getConfig(),
70
        ]);
71
72
        $this->createAliasIfNotExisting($index, $alias);
73
74
        $this->eventDispatcher->dispatch(new ElasticsearchIndexCreatedEvent($index, $definition));
75
    }
76
77
    public function aliasExists(string $alias): bool
78
    {
79
        return $this->client->indices()->existsAlias(['name' => $alias]);
80
    }
81
82
    private function indexExists(string $index): bool
83
    {
84
        return $this->client->indices()->exists(['index' => $index]);
85
    }
86
87
    private function createAliasIfNotExisting(string $index, string $alias): void
88
    {
89
        $exist = $this->client->indices()->existsAlias(['name' => $alias]);
90
91
        if ($exist) {
92
            return;
93
        }
94
95
        $this->client->indices()->refresh([
96
            'index' => $index,
97
        ]);
98
99
        $this->client->indices()->putAlias(['index' => $index, 'name' => $alias]);
100
    }
101
}
102