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\Exception\ResponseException; |
15
|
|
|
use Elastica\Index; |
16
|
|
|
use Elastica\Type\Mapping; |
17
|
|
|
use FOS\ElasticaBundle\Configuration\ManagerInterface; |
18
|
|
|
use FOS\ElasticaBundle\Event\IndexResetEvent; |
19
|
|
|
use FOS\ElasticaBundle\Event\TypeResetEvent; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Deletes and recreates indexes. |
24
|
|
|
*/ |
25
|
|
|
class Resetter |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var AliasProcessor |
29
|
|
|
*/ |
30
|
|
|
private $aliasProcessor; |
31
|
|
|
|
32
|
|
|
/*** |
33
|
|
|
* @var ManagerInterface |
34
|
|
|
*/ |
35
|
|
|
private $configManager; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EventDispatcherInterface |
39
|
|
|
*/ |
40
|
|
|
private $dispatcher; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var IndexManager |
44
|
|
|
*/ |
45
|
|
|
private $indexManager; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var MappingBuilder |
49
|
|
|
*/ |
50
|
|
|
private $mappingBuilder; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ManagerInterface $configManager |
54
|
|
|
* @param IndexManager $indexManager |
55
|
|
|
* @param AliasProcessor $aliasProcessor |
56
|
|
|
* @param MappingBuilder $mappingBuilder |
57
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
58
|
|
|
*/ |
59
|
15 |
View Code Duplication |
public function __construct( |
60
|
|
|
ManagerInterface $configManager, |
61
|
|
|
IndexManager $indexManager, |
62
|
|
|
AliasProcessor $aliasProcessor, |
63
|
|
|
MappingBuilder $mappingBuilder, |
64
|
|
|
EventDispatcherInterface $eventDispatcher |
65
|
|
|
) { |
66
|
15 |
|
$this->aliasProcessor = $aliasProcessor; |
67
|
15 |
|
$this->configManager = $configManager; |
68
|
15 |
|
$this->dispatcher = $eventDispatcher; |
69
|
15 |
|
$this->indexManager = $indexManager; |
70
|
15 |
|
$this->mappingBuilder = $mappingBuilder; |
71
|
15 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Deletes and recreates all indexes. |
75
|
|
|
* |
76
|
|
|
* @param bool $populating |
77
|
|
|
* @param bool $force |
78
|
|
|
*/ |
79
|
1 |
|
public function resetAllIndexes($populating = false, $force = false) |
80
|
|
|
{ |
81
|
1 |
|
foreach ($this->configManager->getIndexNames() as $name) { |
82
|
1 |
|
$this->resetIndex($name, $populating, $force); |
83
|
|
|
} |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Deletes and recreates the named index. If populating, creates a new index |
88
|
|
|
* with a randomised name for an alias to be set after population. |
89
|
|
|
* |
90
|
|
|
* @param string $indexName |
91
|
|
|
* @param bool $populating |
92
|
|
|
* @param bool $force If index exists with same name as alias, remove it |
93
|
|
|
* |
94
|
|
|
* @throws \InvalidArgumentException if no index exists for the given name |
95
|
|
|
*/ |
96
|
12 |
|
public function resetIndex($indexName, $populating = false, $force = false) |
97
|
|
|
{ |
98
|
12 |
|
$indexConfig = $this->configManager->getIndexConfiguration($indexName); |
99
|
11 |
|
$index = $this->indexManager->getIndex($indexName); |
100
|
|
|
|
101
|
11 |
|
if ($indexConfig->isUseAlias()) { |
102
|
1 |
|
$this->aliasProcessor->setRootName($indexConfig, $index); |
103
|
|
|
} |
104
|
|
|
|
105
|
11 |
|
$event = new IndexResetEvent($indexName, $populating, $force); |
106
|
11 |
|
$this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event); |
107
|
|
|
|
108
|
11 |
|
$mapping = $this->mappingBuilder->buildIndexMapping($indexConfig); |
109
|
11 |
|
$index->create($mapping, true); |
110
|
|
|
|
111
|
6 |
|
if (!$populating and $indexConfig->isUseAlias()) { |
112
|
1 |
|
$this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); |
113
|
|
|
} |
114
|
|
|
|
115
|
6 |
|
$this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event); |
116
|
6 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Deletes and recreates a mapping type for the named index. |
120
|
|
|
* |
121
|
|
|
* @param string $indexName |
122
|
|
|
* @param string $typeName |
123
|
|
|
* |
124
|
|
|
* @throws \InvalidArgumentException if no index or type mapping exists for the given names |
125
|
|
|
* @throws ResponseException |
126
|
|
|
*/ |
127
|
5 |
|
public function resetIndexType($indexName, $typeName) |
128
|
|
|
{ |
129
|
5 |
|
$typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName); |
130
|
|
|
|
131
|
4 |
|
$this->resetIndex($indexName, true); |
132
|
|
|
|
133
|
2 |
|
$index = $this->indexManager->getIndex($indexName); |
134
|
2 |
|
$type = $index->getType($typeName); |
135
|
|
|
|
136
|
2 |
|
$event = new TypeResetEvent($indexName, $typeName); |
137
|
2 |
|
$this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event); |
138
|
|
|
|
139
|
2 |
|
$mapping = new Mapping(); |
140
|
2 |
|
foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) { |
141
|
|
|
$mapping->setParam($name, $field); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
$type->setMapping($mapping); |
145
|
|
|
|
146
|
2 |
|
$this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event); |
147
|
2 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* A command run when a population has finished. |
151
|
|
|
* |
152
|
|
|
* @param string $indexName |
153
|
|
|
* |
154
|
|
|
* @deprecated |
155
|
|
|
*/ |
156
|
|
|
public function postPopulate($indexName) |
157
|
|
|
{ |
158
|
|
|
$this->switchIndexAlias($indexName); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Switching aliases. |
163
|
|
|
* |
164
|
|
|
* @param string $indexName |
165
|
|
|
* @param bool $delete Delete or close index |
166
|
|
|
* |
167
|
|
|
* @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException |
168
|
|
|
*/ |
169
|
2 |
|
public function switchIndexAlias($indexName, $delete = true) |
170
|
|
|
{ |
171
|
2 |
|
$indexConfig = $this->configManager->getIndexConfiguration($indexName); |
172
|
|
|
|
173
|
2 |
|
if ($indexConfig->isUseAlias()) { |
174
|
1 |
|
$index = $this->indexManager->getIndex($indexName); |
175
|
1 |
|
$this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete); |
176
|
|
|
} |
177
|
2 |
|
} |
178
|
|
|
} |
179
|
|
|
|