定制 jackardios/elastic-migrations 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jackardios/elastic-migrations

Composer 安装命令:

composer require jackardios/elastic-migrations

包简介

Elasticsearch migrations for Laravel

README 文档

README

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

Support the project!

Elastic Migrations for Laravel allow you to easily modify and share indices schema across the application's environments.

Contents

Compatibility

The current version of Elastic Migrations has been tested with the following configuration:

  • PHP 7.3-8.0
  • Elasticsearch 7.x
  • Laravel 6.x-8.x

Installation

The library can be installed via Composer:

composer require babenkoivan/elastic-migrations

If you want to use Elastic Migrations with Lumen framework check this guide.

Configuration

Elastic Migrations uses babenkoivan/elastic-client as a dependency. If you want to change the default client settings (and I'm pretty sure you do), then you need to create the configuration file first:

php artisan vendor:publish --provider="ElasticClient\ServiceProvider"

You can change Elasticsearch host and other client settings in the config/elastic.client.php file. Please refer to babenkoivan/elastic-client for more details.

You can also publish Elastic Migrations settings:

php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"

This will create the config/elastic.migrations.php file, which allows you to configure the following options:

  • table - the migration table name
  • connection - the database connection
  • storage_directory - the migrations directory
  • index_name_prefix - the indices prefix
  • alias_name_prefix - the aliases prefix

Finally, don't forget to run Laravel database migrations to create Elastic Migrations table:

php artisan migrate

Writing Migrations

You can effortlessly create a new migration file using an Artisan console command:

php artisan elastic:make:migration create_my_index

This command creates a migration class in the elastic/migrations directory.

Every migration includes two methods: up and down. up is used to alternate the index schema and down is used to revert that action.

You can use ElasticMigrations\Facades\Index facade to perform basic operations over Elasticsearch indices:

Create Index

You can create an index with the default settings:

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

You can use a modifier to configure mapping and settings:

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'    
            ]
        ]
    ]);
});

There is also the createRaw method in your disposal:

$mapping = [
    'properties' => [
        'title' => [
            'type' => 'text'
        ]
    ]
];

$settings = [
    'number_of_replicas' => 2
];

Index::createRaw('my-index', $mapping, $settings);

Finally, it is possible to create an index only if it doesn't exist:

// 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);

Update Mapping

You can use a modifier to adjust the mapping:

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

Alternatively, you can use the putMappingRaw method as follows:

Index::putMappingRaw('my-index', [
    'properties' => [
        'title' => [
            'type' => 'text',
            'boost' => 2
        ],
        'price' => [
            'price' => 'float'
        ]      
    ]   
]);

Update Settings

You can use a modifier to change an index configuration:

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

The same result can be achieved with the putSettingsRaw method:

Index::putSettingsRaw('my-index', [
    'index' => [
        'number_of_replicas' => 2,
        'refresh_interval' => -1
    ]
]); 

It is possible to update analysis settings only on closed indices. The pushSettings method closes the index, updates the configuration and opens the index again:

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

The same can be done with the pushSettingsRaw method:

Index::pushSettingsRaw('my-index', [
    'analysis' => [
        'analyzer' => [
            'title' => [
                'type' => 'custom',
                'tokenizer' => 'whitespace'
            ]
        ]
    ]
]); 

Drop Index

You can unconditionally delete the index:

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

or delete it only if it exists:

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

Create Alias

You can create an alias with optional filter query:

Index::putAlias('my-index', 'my-alias', ['term' => ['user_id' => 1]]);

Delete Alias

You can delete an alias by its name:

Index::deleteAlias('my-index', 'my-alias');

More

Finally, you are free to inject Elasticsearch\Client in the migration constructor and execute any supported by client actions.

Running Migrations

You can either run all migrations:

php artisan elastic:migrate

or run a specific one:

php artisan elastic:migrate 2018_12_01_081000_create_my_index

Use the --force option if you want to execute migrations on production environment:

php artisan elastic:migrate --force

Reverting Migrations

You can either revert the last executed migrations:

php artisan elastic:migrate:rollback 

or rollback a specific one:

php artisan elastic:migrate:rollback 2018_12_01_081000_create_my_index

Use the elastic:migrate:reset command if you want to revert all previously migrated files:

php artisan elastic:migrate:reset 

Starting Over

Sometimes you just want to start over, rollback all the changes and apply them again:

php artisan elastic:migrate:refresh

Alternatively you can also drop all existing indices and rerun the migrations:

php artisan elastic:migrate:fresh

Migration Status

You can always check which files have been already migrated and what can be reverted by the elastic:migrate:rollback command (the last batch):

php artisan elastic:migrate:status

Zero Downtime Migration

Changing an index mapping with zero downtime is not a trivial process and might vary from one project to another. Elastic Migrations library doesn't include such feature out of the box, but you can implement it in your project by following this guide.

Troubleshooting

If you see one of the messages below, follow the instructions:

  • Migration table is not yet created - run the php artisan migrate command
  • Migration directory is not yet created - create a migration file using the elastic:make:migration command or create migrations directory manually

In case one of the commands doesn't work as expected, try to publish configuration:

php artisan vendor:publish --provider="ElasticMigrations\ServiceProvider"

jackardios/elastic-migrations 适用场景与选型建议

jackardios/elastic-migrations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「migrations」 「php」 「elasticsearch」 「laravel」 「elastic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jackardios/elastic-migrations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jackardios/elastic-migrations 我们能提供哪些服务?
定制开发 / 二次开发

基于 jackardios/elastic-migrations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 130
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 36
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-28