vae/php-elasticsearch-orm

Adapt the orm query method of elasticsearch for >=php7.4

v1.0.1 2022-06-27 02:33 UTC

This package is auto-updated.

Last update: 2022-07-27 03:33:16 UTC


README

Install

composer require vae/php-elasticsearch-orm

Support Elasticsearch Version

more than 7.0

Use

PHP

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

Laravel framework

Add the service provider config in config/app.php

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

Use in Code

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

Quickstart

Create

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

Update

    $builder->index('index')->update(['key' => 'value']) : bool

deleteById

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

delete

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

Select

    //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')

Count

    $builder->count() : int

Condition

whereTerm

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

whereLike(wildcard)

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

match

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

range

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

where in

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

nested

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

Where Support Operator

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

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

More

see file Vae\PhpElasticsearchOrm\Builder