PHP code example of kabunx / elastic

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

    

kabunx / elastic example snippets



namespace App\Entities;

use Carbon\Carbon;
use Kabunx\Elastic\Hydrate\EsEntity;
use Kabunx\Elastic\Hydrate\EsProperty;
use Kabunx\Elastic\Exceptions\EsException;

class UserEntity extends EsEntity
{

    #[EsProperty("userId")]
    public int $id;

    public string $name;

    public ?Carbon $date;

    /**
     * @var array|UserAddressEntity[]
     */
     #[EsProperty(UserAddressEntity::class)]
    public $address = [];
}


use App\Entities\UserEntity;

(new UserEntity())->getProperties()

(new Builder())
    ->select(['id'])
    ->from('index')
    ->term('is_new', 1)
    ->should(function (Builder $builder) { // bool
        $builder->term('is_new', 1);
        $builder->gte('update_time', 1);
    }, true)
    ->should(function (Builder $builder) {
        $builder
            ->term('function_type', 20057)
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师');
                $builder->like('name', '医疗');
            })
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师xx');
            });
    })
    ->orderByDesc('id')
    ->limit(2)
    ->toSearchParams();

User::elasticsearch()
    ->term('is_new', 1)
    ->should(function (Builder $builder) { // bool
        $builder->term('is_new', 1);
        $builder->gte('update_time', 1);
    }, true)
    ->should(function (Builder $builder) {
        $builder
            ->term('function_type', 20057)
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师');
                $builder->like('name', '医疗');
            })
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师xx');
            });
    })
    ->orderByDesc('id')
    ->limit(2)
    ->toSearchParams();

User::elasticsearch()->rawQuery([])

$esCollection = User::elasticsearch()->get()