vae/php-elasticsearch-orm

适配 >=php7.4 的 Elasticsearch ORM 查询方法

v1.0.6 2023-09-06 08:01 UTC

This package is auto-updated.

Last update: 2024-09-06 10:25:20 UTC


README

安装

composer require vae/php-elasticsearch-orm

支持的 Elasticsearch 版本

大于 7.0

使用

PHP

    //require elasticsearch config
    $config = require "elasticsearch.php";
    //instance
    $builder = Factory::builder($config);

Laravel 框架

config/app.php 中添加服务提供者配置

    'providers' => [
        Vae\PhpElasticsearchOrm\Laravel\ElasticsearchOrm\OrmProvider::class,
    ] 

代码中使用

    $builder = app(\Vae\PhpElasticsearchOrm\Builder::class);

Hyperf 框架

代码中使用

OrmElasticsearchClientFactory

    use \Vae\PhpElasticsearchOrm\Builder;
    use \Vae\PhpElasticsearchOrm\Query;
    use \Vae\PhpElasticsearchOrm\Grammar;
    class OrmElasticsearchClientFactory{
        public static function builder()
        {
            // 如果在协程环境下创建,则会自动使用协程版的 Handler,非协程环境下无改变
            $hyperfBuilder = ApplicationContext::getContainer()->get(ClientBuilderFactory::class)->create();
            $client = $hyperfBuilder->setHosts(['http://127.0.0.1:9200'])->build();
            return new Builder(new Query(new Grammar(), $client));
        }
    }

快速入门

创建

    $builder->index('index')->create(['key' => 'value']);
    //return collection
    $builder->index('index')->createCollection(['key' => 'value']);

批量创建

    $builder->index('index')->batchCreate(
        [
            'key1' => 'v1',
            'key2' => 'v2',
        ],
        [
            'key3' => 'v3',
            'key4' => 'v4',
        ]       
    );

更新

    $builder->index('index')->update(['key' => 'value']);
    $builder->index('index')->update(['key' => ['key2' => 'value']]);

批量更新或更新

    $builder->index('index')->batchUpdateOrCreate(
        [
            'id' => '1', 
            'key1' => 'v1',
            'key2' => 'v2',
        ],
        [
            'id' => 2,
            'key3' => 'v3',
            'key4' => 'v4',
        ]       
    );

按 ID 删除

    $builder->index('index')->deleteById($id) : bool

删除

    $builder->index('index')->delete()

选择

    //select one
    $builder->index('index')->first();
    //select all
    $builder->index('index')->get();
    //select with paginate
    $builder->index('index')->paginate($page, $size) : Collection
    //select by id
    $builder->byId($id) : stdClass
    //select by id if failed throw error
    $builder->byIdOrFail($id) : stdClass
    //select chunk
    $builder->chunk(callback $callback, $limit = 2000, $scroll = '10m')

计数

    $builder->count() : int

条件

whereTerm

    $builder->whereTerm('key', 'value');

whereLike(通配符)

    //value without add wildcard '*'
    $builder->whereLike('key', 'value');

match

    $builder->whereMatch('key', 'value');

范围

    $builder->whereBetween('key', ['value1', 'value2']);

where in

    $builder->whereIn('key', ['value1', 'value2', ...]);

nestedQuery

    $builder->where(function(Builder $query){
        $query->whereTerm('key', 'value');
    });

搜索嵌套数据

    $nestedColumn = "nested_column";
    $builder->whereNested($nestedColumn, function ($query) {
        $query->where('key1', 'value1')
        $query->where('key2', 'value2')
    });
    $nestedKey = "nested_key";
    $key = "key";
    $builder->where("{$nestedKey}@{$key}", '=', 'value');

支持的运算符

['=' => 'eq','>' => 'gt','>=' => 'gte','<' => 'lt','<=' => 'lte','!=' => 'ne',]

    $builder->where('key', '=', 'value');

更多

参见文件 Vae\PhpElasticsearchOrm\Builder