guandeng / elasticsearch

elasticsearch 组件

dev-main 2024-06-05 06:12 UTC

This package is auto-updated.

Last update: 2024-09-05 06:43:01 UTC


README

Latest Stable Version Total Downloads GitHub license

elasticsearch 组件

安装

composer require guandeng/elasticsearch

发布配置

php bin/hyperf.php vendor:publish guandeng/elasticsearch

使用方法

索引

  • 创建
namespace App\Indices;

use Guandeng\Elasticsearch\Index\AbstractIndex;

class Test extends AbstractIndex
{
    protected $index = 'test';
}
  • 通过命令创建
php bin/hyperf.php gen:index test
  • 查询
use App\Indices\Test;

Test::query()->where(...)->search();
  • 按查询更新
use App\Indices\Test;

Test::query()->where(...)->script(['source' => 'ctx.source.xxx = value'])->updateByQuery();
  • 计数
use App\Indices\Test;

Test::query()->where(...)->count();

迁移

  • 索引
namespace App\Indices;

use Guandeng\Elasticsearch\Index\AbstractIndex;

class Test extends AbstractIndex
{
    protected $index = 'test';
    protected $type = '_doc';
    protected $settings = [
        // your settings
    ];
    protected $properties = [
        // your properties
    ];

    public function getMigration(): Closure
    {
        return function ($index) {
            // migrate data
        };
    }
}
  • 运行迁移
php bin/hyperf.php elasticsearch:migrate "App\\Indices\\Test" [--migrate] [--update] [--recreate]

客户端代理

namespace App\Proxy;

use Guandeng\Elasticsearch\ClientProxy;

class FooClient extends ClientProxy
{
    protected $poolName = 'foo';
}