-
Notifications
You must be signed in to change notification settings - Fork 62
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 #55 from Jeroen-G/index-updater
Index updater
- Loading branch information
Showing
27 changed files
with
642 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Index aliases | ||
Aliases allow you to use an index under a different name.This very useful for zero downtime deployments. | ||
|
||
There are three aliases created: a read alias, a write alias and a history alias. | ||
The read alias is used for reading, the write index is used for writing. | ||
The history aggregates all old indices, which can be pruned. | ||
When updating an index it is recreated to a new index with a unique name. | ||
The "write" alias is pointed to the new index and all Scout updates will be forwarded to the "write" index. | ||
After all entities are imported the "read" alias will also be pointed to the new index. | ||
|
||
If you wish to keep the old indices set `prune_old_aliases` to false in `config/explorer.php` | ||
|
||
## Using aliases | ||
A model is only using index aliases if it implements the Aliased interface or enabled in the configuration (see [mapping](mapping.md)). | ||
|
||
After that, any time you use the `scout:import` command a new index will be created and when the insertion of models is done the alias will be pointed to the new index. | ||
|
||
```php | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use JeroenG\Explorer\Application\Explored; | ||
use JeroenG\Explorer\Application\Aliased; | ||
use Laravel\Scout\Searchable; | ||
|
||
class Post extends Model implements Explored, Aliased | ||
{ | ||
use HasFactory; | ||
use Searchable; | ||
|
||
//... | ||
} | ||
``` | ||
|
||
```php | ||
return [ | ||
'indexes' => [ | ||
'posts' => [ | ||
'aliased' => true, | ||
'properties' => [ | ||
'id' => 'keyword', | ||
'title' => 'text', | ||
'created_at' => 'date', | ||
'published' => 'boolean', | ||
'author' => 'nested', | ||
], | ||
], | ||
], | ||
]; | ||
``` | ||
|
||
Be aware that if you currently already have indices and would like to move to using aliases you will need to delete those indices before configuring the aliases. | ||
In Elasticsearch a given name can only be either an index or alias, not both and this cannot be changed on-the-fly. |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Application; | ||
|
||
interface Aliased | ||
{ | ||
} |
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,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\IndexManagement; | ||
|
||
final class IndexAliasConfiguration implements IndexAliasConfigurationInterface | ||
{ | ||
private string $name; | ||
|
||
private bool $pruneOldAliases; | ||
|
||
private function __construct(string $name, bool $pruneOldAliases) | ||
{ | ||
$this->name = $name; | ||
$this->pruneOldAliases = $pruneOldAliases; | ||
} | ||
|
||
public static function create(string $name, bool $pruneOldAliases = true): IndexAliasConfiguration | ||
{ | ||
return new self($name, $pruneOldAliases); | ||
} | ||
|
||
public function shouldOldAliasesBePruned(): bool | ||
{ | ||
return $this->pruneOldAliases; | ||
} | ||
|
||
public function getIndexName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getAliasName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getHistoryAliasName(): string | ||
{ | ||
return $this->name . '-history'; | ||
} | ||
|
||
public function getWriteAliasName(): string | ||
{ | ||
return $this->name . '-write'; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Domain/IndexManagement/IndexAliasConfigurationInterface.php
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\IndexManagement; | ||
|
||
interface IndexAliasConfigurationInterface | ||
{ | ||
public function shouldOldAliasesBePruned(): bool; | ||
|
||
public function getIndexName(): string; | ||
|
||
public function getAliasName(): string; | ||
|
||
public function getHistoryAliasName(): string; | ||
|
||
public function getWriteAliasName(): string; | ||
} |
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
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.