PHP code example of yuancode / esbuilder

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

    

yuancode / esbuilder example snippets


use Elasticsearch\ClientBuilder;
use Yuancode\Es\Builder;

$client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();

$query = Builder::client($client);

Builder::client($client)->index('testindex');

$row = [
    'name'  => '张三',
    'age'   => 8,
    'created' => date("Y-m-d"),
];
$result = Builder::client($client)
    ->index('testes')
    ->id('1')  //设置id
    ->insert($row);

$rows   = [];
$grades = ['一年级', '二年级', '三年级'];
for ($i = 1; $i < 10; $i++) {
    $rows[] = [
        'id'    => $i,
        'name'  => '张三' . $i,
        'age'   => rand(7, 20),
        'grade' => $grades[array_rand($grades)],
    ];
}
$results = Builder::client($client)->index('testes')->insert($rows);

$row = [
    'name' => '张三2',
];
$result = Builder::client($client)
    ->index('testes')
    ->update($row, '1');

$result = Builder::client($client)
    ->index('testes')
    ->delete('1');

$info = Builder::client($client)
    ->index('testes')
    ->find('1');

$arr = Builder::client($client)
    ->index('testes')
    ->where('id', '1');
    ->get();

$arr = Builder::client($client)
    ->index('testes')
    ->filter('id', '1');
    ->get();

$arr = Builder::client($client)
    ->index('testes')
    ->filter('id', '1');
    ->where('age', '>', 10)
    ->where('age', '!=', 9)
    ->whereIn('id', [1,2,3])
    ->whereNotIn('id', [4,5,6])
    ->where('name', 'like', 'test') // match
    ->orWhere('name', 'zhangsna')
    ->get();

$arr = Builder::client($client)
    ->index('testes')
    ->where('age', '>', 10)
    ->orderBy('id', 'desc')
    ->distinct('name')
    ->select('name', 'age')
    ->get();

list($total, $arr) = Builder::client($client)
    ->index('testes')
    ->where('age', '>', 10)
    ->get(true);
// or
$total = Builder::client($client)->index('testes')->where('age', 10)->count();

$arr = Builder::client($client)
    ->index('testes')
    ->where('age', '>', 10)
    ->from(0)
    ->take(10)
    ->get();

//使用 limit 
$arr = Builder::client($client)
    ->index('testes')
    ->where('age', '>', 10)
    ->limit(0, 10)
    ->get();

$arr = Builder::client($client)
    ->index('testes')
    ->filter('name', 's')
    ->must('aaa', 'bbb')
    ->must('aaa2', 'term', 'bbb')
    ->mustNot('ccc', 'dddd')
    ->should('aaa','sdlfks')
    ->get();

$arr = Builder::client($client)
    ->index('testes')
    ->filter(function($query) {
        $query->where('name2', 'z')
            ->where('name3', 'a');
    })
    ->where('name', 'like', 'zhangsan')
    ->from(0)
    ->take(10)
    ->get();
//filter, where, orWhere, must, mustNot,should, 都支持嵌套查询

$arr = Builder::client($client)
    ->index('testes')
    ->groupBy('name')
    ->orderBy('age', 'desc')
    ->take(10)
    ->get();

$query = Builder::client($client)
    ->index('testes')
    ->where('name', 'lskdjfls')
    ->take(10)
    ->toQuery();

print_r($query);

$query = Builder::client($client)
    ->index('testes')
    ->orWhere('name', '=', 'aaa', 10)
    ->orWhere('name', '=', 'bbb', 20)
    ->orderBy('_score', 'desc')
    ->take(10)
    ->get();