jackardios / elastic-migrations
Elasticsearch 迁移工具,适用于 Laravel
Requires
- php: ^7.3 || ^8.0
- babenkoivan/elastic-adapter: ^2.0
- babenkoivan/elastic-client: ^1.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- orchestra/testbench: ^6.12
- phpstan/phpstan: ^0.12.32
- phpunit/phpunit: ^9.5
README
Laravel 的 Elastic Migrations 允许您轻松地在应用的不同环境之间修改和共享索引模式。
内容
兼容性
Elastic Migrations 的当前版本已经与以下配置进行了测试
- PHP 7.3-8.0
- Elasticsearch 7.x
- Laravel 6.x-8.x
安装
您可以通过 Composer 安装此库
composer require babenkoivan/elastic-migrations
如果您想使用 Elastic Migrations 与 Lumen 框架,请参阅此指南。
配置
Elastic Migrations 使用 babenkoivan/elastic-client 作为依赖项。如果您想更改默认客户端设置(我非常确信您会这样做),则需要首先创建配置文件
php artisan vendor:publish --provider="ElasticClient\ServiceProvider"
您可以在 config/elastic.client.php
文件中更改 Elasticsearch 主机和其他客户端设置。有关更多详细信息,请参阅babenkoivan/elastic-client。
您还可以发布 Elastic Migrations 设置
php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"
这将在 config/elastic.migrations.php
文件中创建,允许您配置以下选项
table
- 迁移表名称connection
- 数据库连接storage_directory
- 迁移目录index_name_prefix
- 索引前缀alias_name_prefix
- 别名前缀
最后,不要忘记运行 Laravel 数据库迁移以创建 Elastic Migrations 表
php artisan migrate
编写迁移
您可以使用 Artisan 控制台命令轻松创建新的迁移文件
php artisan elastic:make:migration create_my_index
此命令在 elastic/migrations
目录中创建迁移类。
每个迁移都包含两个方法: up
和 down
。 up
用于修改索引模式,而 down
用于撤销该操作。
您可以使用 ElasticMigrations\Facades\Index
门面来执行对 Elasticsearch 索引的基本操作
创建索引
您可以使用默认设置创建索引
Index::create('my-index');
您可以使用修饰符来配置映射和设置
Index::create('my-index', function (Mapping $mapping, Settings $settings) { // to add a new field to the mapping use method name as a field type (in Camel Case), // first argument as a field name and optional second argument for additional field parameters $mapping->text('title', ['boost' => 2]); $mapping->float('price'); // you can define a dynamic template as follows $mapping->dynamicTemplate('my_template_name', [ 'match_mapping_type' => 'long', 'mapping' => [ 'type' => 'integer', ], ]); // you can also change the index settings and the analysis configuration $settings->index([ 'number_of_replicas' => 2, 'refresh_interval' => -1 ]); $settings->analysis([ 'analyzer' => [ 'title' => [ 'type' => 'custom', 'tokenizer' => 'whitespace' ] ] ]); });
您还可以使用 createRaw
方法
$mapping = [ 'properties' => [ 'title' => [ 'type' => 'text' ] ] ]; $settings = [ 'number_of_replicas' => 2 ]; Index::createRaw('my-index', $mapping, $settings);
最后,只有当索引不存在时才创建索引
// you can use a modifier as shown above Index::createIfNotExists('my-index', $modifier); // or you can use raw mapping and settings Index::createIfNotExistsRaw('my-index', $mapping, $settings);
更新映射
您可以使用修饰符来调整映射
Index::putMapping('my-index', function (Mapping $mapping) { $mapping->text('title', ['boost' => 2]); $mapping->float('price'); });
或者,您可以使用 putMappingRaw
方法
Index::putMappingRaw('my-index', [ 'properties' => [ 'title' => [ 'type' => 'text', 'boost' => 2 ], 'price' => [ 'price' => 'float' ] ] ]);
更新设置
您可以使用修饰符来更改索引配置
Index::putSettings('my-index', function (Settings $settings) { $settings->index([ 'number_of_replicas' => 2, 'refresh_interval' => -1 ]); });
您可以使用 putSettingsRaw
方法达到相同的结果
Index::putSettingsRaw('my-index', [ 'index' => [ 'number_of_replicas' => 2, 'refresh_interval' => -1 ] ]);
您只能在关闭的索引上更新分析设置。 pushSettings
方法会关闭索引,更新配置,然后再次打开索引
Index::pushSettings('my-index', function (Settings $settings) { $settings->analysis([ 'analyzer' => [ 'title' => [ 'type' => 'custom', 'tokenizer' => 'whitespace' ] ] ]); });
您可以使用 pushSettingsRaw
方法做到这一点
Index::pushSettingsRaw('my-index', [ 'analysis' => [ 'analyzer' => [ 'title' => [ 'type' => 'custom', 'tokenizer' => 'whitespace' ] ] ] ]);
删除索引
您可以无条件删除索引
Index::drop('my-index');
或者只有当索引存在时才删除它
Index::dropIfExists('my-index');
创建别名
您可以使用可选的过滤查询创建别名
Index::putAlias('my-index', 'my-alias', ['term' => ['user_id' => 1]]);
删除别名
您可以通过别名名称来删除别名
Index::deleteAlias('my-index', 'my-alias');
更多
最后,您可以在迁移构造函数中注入 Elasticsearch\Client
并执行客户端支持的所有操作。
运行迁移
您可以运行所有迁移
php artisan elastic:migrate
或者运行特定的一个
php artisan elastic:migrate 2018_12_01_081000_create_my_index
如果您想在生产环境中执行迁移,请使用 --force
选项
php artisan elastic:migrate --force
撤销迁移
您可以撤销最后执行的所有迁移
php artisan elastic:migrate:rollback
或者撤销特定的一个
php artisan elastic:migrate:rollback 2018_12_01_081000_create_my_index
如果您想撤销所有之前迁移的文件,请使用 elastic:migrate:reset
命令
php artisan elastic:migrate:reset
重新开始
有时候您可能只想从头开始,撤销所有更改并再次应用
php artisan elastic:migrate:refresh
或者您也可以删除所有现有索引并重新运行迁移
php artisan elastic:migrate:fresh
迁移状态
您可以使用 elastic:migrate:rollback
命令(最后一批)来检查哪些文件已经被迁移以及哪些可以撤销
php artisan elastic:migrate:status
零停机时间迁移
在不中断服务的情况下更改索引映射是一个复杂的过程,可能因项目而异。Elastic Migrations 库默认不包含此功能,但您可以通过遵循此指南在项目中实现它。
故障排除
如果您看到以下消息之一,请按照说明操作
迁移表尚未创建
- 运行php artisan migrate
命令迁移目录尚未创建
- 使用elastic:make:migration
命令创建迁移文件或手动创建migrations
目录
如果某个命令未按预期工作,请尝试发布配置
php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"