PHP code example of th-zhou / esorm

1. Go to this page and download the library: Download th-zhou/esorm 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/ */

    

th-zhou / esorm example snippets


$esSearch = EsSearch::getInstance([
    'hosts' => ['es:9200'],
]);

$esSearch->index('demo')->create([
    'name'     => 'create demo',
    'quantity' => 100,
]);

$esSearch->index('demo')->update('100', [
    'name' => 'update demo'
]);

$esSearch->index('demo')->delete('100');

$esSearch->index('demo')->find('100');

$esSearch->index('demo')->find('100', ['name']);
$esSearch->index('demo')->select(['name'])->find('100');

$esSearch->index('demo')->first();
$esSearch->index('demo')->first(['name']);

$esSearch->index('demo')->get();
$esSearch->index('demo')->get(['name']);
$esSearch->index('demo')->get(['name'], true); // true return total

$page = 1;
$pageSize = 10;
$esSearch->index('demo')->paginate($page, $pageSize);

$esSearch->index('demo')->chunk(function ($data) {
    // do
});

or

$limit = 1000;
$scroll = '5m';
$esSearch->index('demo')->chunk(function ($data) {
    // do
}, $limit, $scroll);

$esSearch->index('demo')->where(function (\EsORM\Query $query) {
    $query->where('name', 'create_demo')->orWhere('quantity', 100);
});

$esSearch->index('demo')->where('name', 'create_demo');

$esSearch->index('demo')->where('quantity' '>=', 100);

$esSearch->index('demo')->where(['name' => 'create_demo', 'quantity' => 100]);

$esSearch->index('demo')->where(['name' => 'create_demo', 'quantity' => [50, 150]]);

$esSearch->index('demo')->whereMatch('name', 'create_demo');

$esSearch->index('demo')->whereMatchKeyword('name', 'create_demo');

$esSearch->index('demo')->whereTerm('name', 'create_demo');

$esSearch->index('demo')->whereTermKeyword('name', 'create_demo');

$esSearch->index('demo')->whereTerms('quantity', [100, 200]);

$esSearch->index('demo')->whereIn('quantity', [100, 200]);

$esSearch->index('demo')->whereRange('quantity', [100, 200]);

$esSearch->index('demo')->whereBetween('quantity', [100, 200]);

$esSearch->index('demo')->whereExists('name');

$esSearch->index('demo')->select('name', 'quantity');

or

$esSearch->index('demo')->select(['name', 'quantity']);

$esSearch->index('demo')->orderBy('quantity', 'desc'); // asc

$esSearch->index('demo')->offset(0);

$esSearch->index('demo')->limit(100);

$esSearch->index('demo')->aggBy('quantity', 'sum');

or

$esSearch->index('demo')->aggBy(['single_avg_quantity' => ['avg' => ['field' => 'quantity']]]);

or

$esSearch->index('demo')->aggBy([['quantitys' => ['terms' => ['field' => 'quantity']]]])

or

$esSearch->index('demo')->aggBy([['single_avg_quantity' => ['avg' => ['field' => 'quantity']]], ['quantitys' => ['terms' => ['field' => 'quantity']]]]);