PHP code example of vae / php-elasticsearch-orm

1. Go to this page and download the library: Download vae/php-elasticsearch-orm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

vae / php-elasticsearch-orm example snippets


    //config = 
    $builder = Factory::builder($config);

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

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

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

    $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

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

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

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

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

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

    $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');

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

composer