panytsch/elastic-migrations-laravel

Laravel 的 Elasticsearch 迁移

v1.3.2 2021-02-09 20:12 UTC

This package is auto-updated.

Last update: 2024-09-10 04:02:37 UTC


README

Latest Stable Version Total Downloads License Tests Code style Static analysis Donate PayPal

Laravel 的 Elasticsearch 迁移允许您轻松地在应用程序的不同环境中修改和共享索引模式。

内容

兼容性

当前版本的 Elastic Migrations 已与以下配置进行了测试

  • PHP 7.2-7.4
  • Elasticsearch 7.x
  • Laravel 6.x-8.x

分支更改

此特定分支版本不需要 MySQL 保存迁移结果。您只需使用 Elasticsearch 即可。为此,您只需设置环境变量

SAVE_MIGRATIONS_TO_ELASTIC=true
MIGRATIONS_INDEX=name_of_index_to_store_migrations_result

或者

更改文件 elastic.migrations.php 中的此配置

之后,您必须创建此弹性索引(将在未来版本中自动化),并包含映射

{
    "mappings" : {
        "properties" : {
            "batch" : {
                "type" : "integer"
            },
            "migration" : {
                "type" : "keyword"
            }
        }
    }
}

安装

此库可以通过 Composer 安装

composer require babenkoivan/elastic-migrations

如果您想使用 Lumen 框架 与 Elastic Migrations 一起使用,请参阅 此指南

配置

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 文件中找到。

最后,别忘了运行 Laravel 数据库迁移来创建 Elastic Migrations 表

php artisan migrate

编写迁移

您可以使用 Artisan 控制台命令轻松创建新的迁移文件

php artisan elastic:make:migration create_my_index

此命令在 elastic/migrations 目录中创建一个迁移类。

每个迁移都包括两个方法:updownup 用于更改索引模式,而 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 as 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 
    $settings->index([
         'number_of_replicas' => 2,
         'refresh_interval' => -1
    ]);
    
    // and analisys configuration
    $settings->analysis([
        'analyzer' => [
            'title' => [
                'type' => 'custom',
                'tokenizer' => 'whitespace'    
            ]
        ]
    ]);
});

还可以选择仅在索引不存在时创建索引

Index::createIfNotExists('my-index');

更新映射

使用修饰符来调整映射

Index::putMapping('my-index', function (Mapping $mapping) {
    $mapping->text('title', ['boost' => 2]);
    $mapping->float('price');
});

更新设置

使用修饰符来更改索引配置

Index::putSettings('my-index', function (Settings $settings) {
    $settings->index([
         'number_of_replicas' => 2,
         'refresh_interval' => -1
    ]);
});

您只能在关闭的索引上更新分析设置。使用 putSettingsHard 方法关闭索引,更新配置,然后再次打开索引

Index::putSettingsHard('my-index', function (Settings $settings) {
    $settings->analysis([
        'analyzer' => [
            'title' => [
                'type' => 'custom',
                'tokenizer' => 'whitespace'
            ]
        ]
    ]);
});

删除索引

您可以无条件地删除索引

Index::drop('my-index');

或者仅在索引存在时删除它

Index::dropIfExists('my-index');

更多

最后,您可以自由地将 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

迁移状态

您可以使用elastic:migrate:rollback命令(最后一批)检查哪些文件已经被迁移,以及可以撤销哪些文件

php artisan elastic:migrate:status

故障排除

如果您看到以下消息之一,请执行提到的操作

  • 迁移表尚未创建 - 运行php artisan migrate命令
  • 迁移目录尚未创建 - 使用elastic:make:migration命令创建迁移文件,或者手动创建迁移目录