1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
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 FOS\ElasticaBundle\Index; |
13
|
|
|
|
14
|
|
|
use Elastica\Client; |
15
|
|
|
use Elastica\Request; |
16
|
|
|
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig; |
17
|
|
|
use FOS\ElasticaBundle\Configuration\ManagerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Template resetter. |
21
|
|
|
* |
22
|
|
|
* @author Dmitry Balabka <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class TemplateResetter implements ResetterInterface |
25
|
|
|
{ |
26
|
|
|
/*** |
27
|
|
|
* @var ManagerInterface |
28
|
|
|
*/ |
29
|
|
|
private $configManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var MappingBuilder |
33
|
|
|
*/ |
34
|
|
|
private $mappingBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Client |
38
|
|
|
*/ |
39
|
|
|
private $client; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Index template manager. |
43
|
|
|
* |
44
|
|
|
* @var IndexTemplateManager |
45
|
|
|
*/ |
46
|
|
|
private $indexTemplateManager; |
47
|
|
|
|
48
|
11 |
|
public function __construct( |
49
|
|
|
ManagerInterface $configManager, |
50
|
|
|
MappingBuilder $mappingBuilder, |
51
|
|
|
Client $client, |
52
|
|
|
IndexTemplateManager $indexTemplateManager |
53
|
|
|
) { |
54
|
11 |
|
$this->configManager = $configManager; |
55
|
11 |
|
$this->mappingBuilder = $mappingBuilder; |
56
|
11 |
|
$this->client = $client; |
57
|
11 |
|
$this->indexTemplateManager = $indexTemplateManager; |
58
|
11 |
|
} |
59
|
|
|
|
60
|
4 |
|
public function resetAllIndexes($deleteIndexes = false) |
61
|
|
|
{ |
62
|
4 |
|
foreach ($this->configManager->getIndexNames() as $name) { |
63
|
4 |
|
$this->resetIndex($name, $deleteIndexes); |
64
|
|
|
} |
65
|
4 |
|
} |
66
|
|
|
|
67
|
8 |
|
public function resetIndex(string $indexName, bool $deleteIndexes = false) |
68
|
|
|
{ |
69
|
8 |
|
$indexTemplateConfig = $this->configManager->getIndexConfiguration($indexName); |
70
|
8 |
|
if (!$indexTemplateConfig instanceof IndexTemplateConfig) { |
71
|
|
|
throw new \RuntimeException(\sprintf('Incorrect index configuration object. Expecting IndexTemplateConfig, but got: %s ', \get_class($indexTemplateConfig))); |
72
|
|
|
} |
73
|
8 |
|
$indexTemplate = $this->indexTemplateManager->getIndexTemplate($indexName); |
74
|
|
|
|
75
|
8 |
|
$mapping = $this->mappingBuilder->buildIndexTemplateMapping($indexTemplateConfig); |
76
|
8 |
|
if ($deleteIndexes) { |
77
|
4 |
|
$this->deleteTemplateIndexes($indexTemplateConfig); |
78
|
|
|
} |
79
|
8 |
|
$indexTemplate->create($mapping); |
80
|
8 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Delete all template indexes. |
84
|
|
|
*/ |
85
|
5 |
|
public function deleteTemplateIndexes(IndexTemplateConfig $template) |
86
|
|
|
{ |
87
|
5 |
|
$this->client->request($template->getTemplate().'/', Request::DELETE); |
88
|
5 |
|
} |
89
|
|
|
} |
90
|
|
|
|