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