PHPackages                             feekk/elastic-php-simple - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. feekk/elastic-php-simple

ActiveLibrary

feekk/elastic-php-simple
========================

A ElasticSearch PHP Dsl builder

1.0.4(7y ago)5171MITPHP

Since Apr 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/feekk/elastic-php-simple)[ Packagist](https://packagist.org/packages/feekk/elastic-php-simple)[ Docs](https://github.com/feekk)[ RSS](/packages/feekk-elastic-php-simple/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)DependenciesVersions (6)Used By (0)

elastic-php-simple
==================

[](#elastic-php-simple)

**elastic-php-simple** is a simple elasticsearch dsl builder for php.

descrpition
===========

[](#descrpition)

There are two different libraries：Buider and Model。

**Builder** is original

**Model** is package from **Builder** than can be extended by other schema model

Usage
=====

[](#usage)

`**Builder**`

```
$builder = new Builder();
$a = $builder->match('a', 3);
$b = $builder->match('b', 5);
#and conditions
$builder->_and($a, $b);
$builder->orderBy('c', 'asc');
$builder->offset(0, 10);
$dsl = $builder->build();

```

`**Model**`

```
use ElasticPhpSimple\ResultParse;

class EsModel extends Model{
    protected $id;
    public $buckets;
    public $aggs;
    public $result;

    public function __construct(){
        parent::__construct();
    }

    public function getBuckets(){
        if(is_array($this->_builder->buckets) && count($this->_builder->buckets)>0){
            foreach($this->_builder->buckets as $k=>$v){
                $this->buckets[] = $v->field;
            }
        }
    }

    public function getMertics(){
        if(is_array($this->_builder->mertics) && count($this->_builder->mertics)>0){
            foreach($this->_builder->mertics as $k=>$v){
                $this->aggs[] = $v->as;
            }
        }
    }

    protected function getParams(){
        $this->getBuckets();
        $this->getMertics();
    }

    public function getList($orderName = '', $orderType = ''){
        $this->getParams();
        $dsl = $this->dsl();
        $this->result = Handle::getInstance()->search($dsl);
        $parse = new ResultParse($this);
        return $parse->getList($orderName, $orderType);
    }

    public function getAggs(){
        $this->getParams();
        $dsl = $this->dsl();
        $this->result = Handle::getInstance()->search($dsl);
        $parse = new ResultParse($this);
        return $parse->getAggs();
    }

    public function find(){
        $dsl = $this->dsl();
        $ret = Handle::getInstance()->search($dsl);
        return $ret;
    }

    public function findById($id=null){
        if(is_null($id)){
            $id = $this->id;
        }
        $this->match('Id',$id);
        return $this->find();
    }

    public function Id(){
        return $this->id;
    }
}

class UserModel extends EsModel{
    public function _Fileds(){
        return [
            'Id'=>'',
            'Name'=>'',
            'Age'=>'',
        ];
    }
}

$user = new UserModel();

$user->match('Id', 3);
$user->match('Name', 'feek')->withInner('Id', 'or');
$user->match('Age', 18)->withOuter('Id', 'and'); //like sql: (Id = 3 or Name='feek') and Age=18
$list = $user->getList();

```

\#API

\##Builder

\###conditions

**`match`**

```
/**
 * equal condition
 * return array condition dsl
 */
function match(string $name, string $value)

```

**`notMatch`**

```
/**
 * not equal condition
 * return array condition dsl
 */
function notMatch(string $name, string $value)

```

**`in`**

```
/**
 * in condition
 * return array condition dsl
 */
function in(int $name, array $value)

```

**`notIn`**

```
/**
 * not in condition
 * return array condition dsl
 */
function notIn(int $name, array $value)

```

**`range`**

```
/**
 * range condition
 * exsample: $value = array(">=" => 3, "
