This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from digiaonline/improve-reindex-settings
Improve re-indexing speed
- Loading branch information
Showing
17 changed files
with
376 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Nord\Lumen\Elasticsearch\Pipelines\Stages; | ||
|
||
use League\Pipeline\StageInterface; | ||
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; | ||
|
||
/** | ||
* Class ReIndexStage | ||
* @package Nord\Lumen\Elasticsearch\Pipelines\Stages | ||
*/ | ||
class ReIndexStage implements StageInterface | ||
{ | ||
|
||
/** | ||
* @var ElasticsearchServiceContract | ||
*/ | ||
private $elasticsearchService; | ||
|
||
/** | ||
* CheckIndexExistsStage constructor. | ||
* | ||
* @param ElasticsearchServiceContract $elasticsearchService | ||
*/ | ||
public function __construct(ElasticsearchServiceContract $elasticsearchService) | ||
{ | ||
$this->elasticsearchService = $elasticsearchService; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __invoke($payload) | ||
{ | ||
// Reindex data from the old index to the new, but only if the old index exists (not true on brand new setups) | ||
if ($this->elasticsearchService->indices()->exists(['index' => $payload->getIndexName()])) { | ||
// Temporarily change some index settings to speed up the process | ||
$this->elasticsearchService->indices()->putSettings([ | ||
'index' => $payload->getTargetVersionName(), | ||
'body' => [ | ||
'refresh_interval' => -1, | ||
'number_of_replicas' => 0, | ||
], | ||
]); | ||
|
||
$this->elasticsearchService->reindex([ | ||
'body' => [ | ||
'source' => [ | ||
'index' => $payload->getIndexName(), | ||
'size' => $payload->getBatchSize(), | ||
], | ||
'dest' => [ | ||
'index' => $payload->getTargetVersionName(), | ||
], | ||
], | ||
]); | ||
} | ||
|
||
return $payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Nord\Lumen\Elasticsearch\Pipelines\Stages; | ||
|
||
use League\Pipeline\StageInterface; | ||
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; | ||
use Nord\Lumen\Elasticsearch\Pipelines\Payloads\ApplyMigrationPayload; | ||
|
||
/** | ||
* Class StoreIndexSettingsStage | ||
* @package Nord\Lumen\Elasticsearch\Pipelines\Stages | ||
*/ | ||
class StoreIndexSettingsStage implements StageInterface | ||
{ | ||
|
||
/** | ||
* @var ElasticsearchServiceContract | ||
*/ | ||
private $elasticsearchService; | ||
|
||
/** | ||
* CheckIndexExistsStage constructor. | ||
* | ||
* @param ElasticsearchServiceContract $elasticsearchService | ||
*/ | ||
public function __construct(ElasticsearchServiceContract $elasticsearchService) | ||
{ | ||
$this->elasticsearchService = $elasticsearchService; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function __invoke($payload) | ||
{ | ||
/** @var ApplyMigrationPayload $payload */ | ||
|
||
// Store the current number_of_replicas setting value in the payload | ||
$settings = $this->elasticsearchService->indices()->getSettings([ | ||
'index' => $payload->getTargetVersionName(), | ||
]); | ||
|
||
$indexSettings = $settings[$payload->getTargetVersionName()]['settings']['index']; | ||
$payload->setNumberOfReplicas($indexSettings['number_of_replicas']); | ||
|
||
return $payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages; | ||
|
||
use Elasticsearch\Namespaces\IndicesNamespace; | ||
use Nord\Lumen\Elasticsearch\Contracts\ElasticsearchServiceContract; | ||
use Nord\Lumen\Elasticsearch\Tests\TestCase; | ||
|
||
/** | ||
* Class AbstractStageTestCase | ||
* @package Nord\Lumen\Elasticsearch\Tests\Pipelines\Stages; | ||
*/ | ||
abstract class AbstractStageTestCase extends TestCase | ||
{ | ||
|
||
/** | ||
* @param array $methods | ||
* | ||
* @return IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected function getMockedIndices($methods = []) | ||
{ | ||
return $this->getMockBuilder(IndicesNamespace::class) | ||
->disableOriginalConstructor() | ||
->setMethods($methods) | ||
->getMock(); | ||
} | ||
|
||
/** | ||
* @param IndicesNamespace|\PHPUnit_Framework_MockObject_MockObject $mockedIndices | ||
* | ||
* @return ElasticsearchServiceContract|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected function getMockedSearchService($mockedIndices) | ||
{ | ||
$searchService = $this->getMockBuilder(ElasticsearchServiceContract::class) | ||
->setMethods(['indices']) | ||
->getMockForAbstractClass(); | ||
|
||
$searchService->method('indices') | ||
->willReturn($mockedIndices); | ||
|
||
return $searchService; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.