Skip to content

Commit 7b0e220

Browse files
committed
Expose getIndexMapping() to be public
Retrieve index mapping when necessary, instead of in IndexManager constructor
1 parent 9b06c0a commit 7b0e220

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

Manager/IndexManager.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ class IndexManager
5656
/**
5757
* @var array
5858
*/
59-
protected $indexMapping;
59+
protected $indexMapping = null;
60+
61+
/**
62+
* @var array
63+
*/
64+
private $indexSettings;
6065

6166
/**
6267
* @var Repository[]
@@ -108,7 +113,7 @@ public function __construct(
108113
$this->finder = $finder;
109114
$this->documentConverter = $documentConverter;
110115
$this->useAliases = $indexSettings['use_aliases'];
111-
$this->indexMapping = $this->getIndexMapping($managerName, $indexSettings);
116+
$this->indexSettings = $indexSettings;
112117

113118
$this->readAlias = $this->getBaseIndexName();
114119
$this->writeAlias = $this->getBaseIndexName();
@@ -134,19 +139,31 @@ public function setEventDispatcher($eventDispatcher)
134139
$this->eventDispatcher = $eventDispatcher;
135140
}
136141

142+
/**
143+
* @return array
144+
*/
145+
public function getIndexMapping()
146+
{
147+
if (is_null($this->indexMapping)) {
148+
$this->indexMapping = $this->buildIndexMapping($this->managerName);
149+
}
150+
151+
return $this->indexMapping;
152+
}
153+
137154
/**
138155
* Returns mapping array for index
139156
*
140-
* @param string $indexManagerName
141-
* @param array $indexSettings
157+
* @param string $indexManagerName
158+
*
142159
* @return array
143160
*/
144-
private function getIndexMapping($indexManagerName, array $indexSettings)
161+
private function buildIndexMapping($indexManagerName)
145162
{
146-
$index = ['index' => $indexSettings['name']];
163+
$index = ['index' => $this->indexSettings['name']];
147164

148-
if (!empty($indexSettings['settings'])) {
149-
$index['body']['settings'] = $indexSettings['settings'];
165+
if (!empty($this->indexSettings['settings'])) {
166+
$index['body']['settings'] = $this->indexSettings['settings'];
150167
}
151168

152169
$mappings = [];
@@ -245,6 +262,7 @@ public function getRepository($documentClass)
245262
* Returns the data provider object for a type (provided in short class notation, e.g AppBundle:Product)
246263
*
247264
* @param string $documentClass The document class for the type
265+
*
248266
* @return ProviderInterface
249267
*/
250268
public function getDataProvider($documentClass)
@@ -264,11 +282,13 @@ public function getDataProvider($documentClass)
264282
*/
265283
private function getBaseIndexName()
266284
{
267-
return $this->indexMapping['index'];
285+
return $this->indexSettings['name'];
268286
}
269287

270288
/**
271289
* Return a name for a new index, which does not already exist
290+
*
291+
* @return string
272292
*/
273293
private function getUniqueIndexName()
274294
{
@@ -288,6 +308,7 @@ private function getUniqueIndexName()
288308
* Returns the live physical index name, verifying that it exists
289309
*
290310
* @return string
311+
*
291312
* @throws Exception If live index is not found
292313
*/
293314
public function getLiveIndex()
@@ -321,7 +342,7 @@ public function getLiveIndex()
321342
*/
322343
public function createIndex()
323344
{
324-
$settings = $this->indexMapping;
345+
$settings = $this->getIndexMapping();
325346

326347
if (true === $this->getUseAliases()) {
327348
// Make sure the read and write aliases do not exist already as aliases or physical indices
@@ -421,7 +442,7 @@ public function rebuildIndex($deleteOld = false, $cancelExistingRebuild = false)
421442
}
422443

423444
// Create a new index
424-
$settings = $this->indexMapping;
445+
$settings = $this->getIndexMapping();
425446
$oldIndex = $this->getLiveIndex();
426447
$newIndex = $this->getUniqueIndexName();
427448
$settings['index'] = $newIndex;
@@ -532,6 +553,7 @@ public function rebuildIndex($deleteOld = false, $cancelExistingRebuild = false)
532553
* Makes sure the index exists in Elasticsearch and its aliases (if using such) are properly set up
533554
*
534555
* @param bool|true $exceptionIfRebuilding
556+
*
535557
* @throws NoReadAliasException When read alias does not exist
536558
* @throws IndexRebuildingException When the index is rebuilding, according to the current aliases
537559
* @throws Exception When any other problem with the index or aliases mappings exists

0 commit comments

Comments
 (0)