2017 © Pedro Peláez
 

library elastic-php-simple

A ElasticSearch PHP Dsl builder

image

feekk/elastic-php-simple

A ElasticSearch PHP Dsl builder

  • Tuesday, April 24, 2018
  • by feekk
  • Repository
  • 1 Watchers
  • 4 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

elastic-php-simple

elastic-php-simple is a simple elasticsearch dsl builder for php., (*1)

descrpition

There are two different libraries:Buider and Model。, (*2)

Builder is original, (*3)

Model is package from Builder than can be extended by other schema model, (*4)

Usage

**Builder**, (*5)

$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**, (*6)

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, (*7)

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

notMatch, (*8)

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

in, (*9)

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

notIn, (*10)

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

range, (*11)

/**
 * range condition
 * exsample: $value = array(">=" => 3, "<"=> 8)  like this: $name >= 3 and $name < 8
 * return array condition dsl
 */
function range(string $name, array $value)

like, (*12)

/**
 * like condition
 * return array condition dsl
 */
function like(string $name, string $value)

notLike, (*13)

/**
 * not like condition
 * return array condition dsl
 */
function notLike(string $name, string $value)

Builder condition builder

this condition builder package condtions into dsl query body, (*14)

$a = $builder->match('a', 2);
$b = $builder->match('b', 3);
$builder->_and($a,$b); //this func will builder $a and $b conditon with and

result:
{ 
    "query": {
    "bool": {
      "must": {
        "bool": {
          "must": [
            {
              "match": {
                "a": {
                  "query": 2,
                  "type": "phrase"
                }
              }
            },
            {
              "match": {
                "b": {
                  "query": 3,
                  "type": "phrase"
                }
              }
            }
          ]
        }
      }
    }
  },
  "from": 0,
  "size": 10
}

_or, (*15)

/**
 * gather condition with or
 * return array (queryBody)
 */
function _or(args...) //allow mutil params

_and, (*16)

/**
 * gather condition with and
 * return array (queryBody)
 */
function _and(args...) //allow mutil params

aggs buckets

grouBy, (*17)

/**
 * aggs buckets by field
 * return ElasticPhpSimple\Base\TermsAggs
 */
function grouBy($field, $as = null, $flag=true) //grouBy('Name', 'otherName')

dateHistogram, (*18)

/**
 * aggs buckets  by field
 * return ElasticPhpSimple\Base\DateHistogramAggs
 */
function dateHistogram($field, $as = null, $flag=true)

histogram, (*19)

/**
 * aggs buckets  by field
 * return ElasticPhpSimple\Base\HistogramAggs
 */
function histogram($field, $as = null, $flag=true)

aggs mertics

min, (*20)

/**
 * aggs mertics by field
 * return ElasticPhpSimple\Base\MinAggs
 */
function min($field, $as = null, $flag=true)

max, (*21)

/**
 * aggs by field
 * return ElasticPhpSimple\Base\MaxAggs
 */
function max($field, $as = null, $flag=true)

sum, (*22)

/**
 * aggs by field
 * return ElasticPhpSimple\Base\SumAggs
 */
function sum($field, $as = null, $flag=true)

avg, (*23)

/**
 * aggs by field
 * return ElasticPhpSimple\Base\AvgAggs
 */
function avg($field, $as = null, $flag=true)    

count, (*24)

/**
 * aggs by field
 * return ElasticPhpSimple\Base\AvgAggs
 */
function count($field, $as = null, $flag=true)

cardinalCount, (*25)

/**
 * distinct count aggs by field
 * return ElasticPhpSimple\Base\CardinalAggs
 */
function cardinalCount($field, $as = null, $flag=true)

sort

orderBy, (*26)

/**
 * order query body result list
 * return array (queryBody)
 */
function orderBy($orderBy, $order) //orderBy('Name', 'asc')

grouByOrder, (*27)

/**
 * order aggs body result list
 * return array (queryBody)
 */
function grouByOrder($orderBy, $order) //grouByOrder('Name', 'asc')

other

build, (*28)

/**
 * return dls
 * return array 
 */
function build()

Model

Basically the model function is the same as the Builder except Builder function _or and _and replace by withInner/withOuter., (*29)

function withInner/withOuter is stick condtion's function, like (xxx and/or xxx) in sql, (*30)

exsample:, (*31)

#where ((a=3 and c=5) or m=5) and (d=6 or h=8)

$model->match('a', 3); //create first conditon, then we got and a variable

//withInner means condtion will be stick in same level. 
//it means we create other conditon and push it into a variable with and, 
//then a and c in same level. like (a=3 and c=5)
$model->match('c', 5)->withInner('a', 'and');

//withInner means condtion will be stick in highter level.
//a is and array with a and c in same level above
//this action means push a into m with same level
$model->match('m', 5)->withOuter('a', 'or');// we push m condition with same level with a

$model->match('d', 6)->withOuter('a', 'and');

$model->match('h', 8)-> withInner('d', 'or');

code:, (*32)

$model->match('a', 3);, (*33)

$a=array(
    'field'=>'a',
    'value'=>3,
    'type'=>'match'
)

$model->match('c', 5)->withInner('a', 'and');, (*34)

**//$a change!!!!**
$a=array(       
    '_and'=>array(
        array(
            'field'=>'a',
            'value'=>3,
            'type'=>'match'
        ),
        $c
    )
)
$c = array(
    'field'=>'c',
    'value'=>5,
    'type'=>'match'
)

$model->match('m', 5)->withOuter('a', 'or');, (*35)

**//$a change once again!!!!**
$a=array(       
    '_or'=>array(
        array(
            '_and'=>array(
                array(
                    'field'=>'a',
                    'value'=>3,
                    'type'=>'match'
                ),
                $c
            )
        ),
        $m
    )
)
$c = array(
    'field'=>'c',
    'value'=>5,
    'type'=>'match'
)
$m = array(
    'field'=>'m',
    'value'=>5,
    'type'=>'match'
)

$model->match('d', 6)->withOuter('a', 'and');, (*36)

**//$a change once more!!!!**
$a=array(       
    '_and'=>array(
        array(
            '_or'=>array(
                array(
                    '_and'=>array(
                        array(
                            'field'=>'a',
                            'value'=>3,
                            'type'=>'match'
                        ),
                        $c
                    )
                ),
                $m
            )
        ),
        $b
    )
)

$c = array(
    'field'=>'c',
    'value'=>5,
    'type'=>'match'
)
$m = array(
    'field'=>'m',
    'value'=>5,
    'type'=>'match'
)
$d = array(
    'field'=>'d',
    'value'=>6,
    'type'=>'match'
)

$model->match('h', 8)-> withInner('d', 'or');, (*37)

$a=array(
    '_and'=>array(
        array(
            '_or'=>array(
                array(
                    '_and'=>array(
                        array(
                            'field'=>'a',
                            'value'=>3,
                            'type'=>'match'
                        ),
                        $c
                    )
                ),
                $m
            )
        ),
        $b
    )
)

$c = array(
    'field'=>'c',
    'value'=>5,
    'type'=>'match'
)
$m = array(
    'field'=>'m',
    'value'=>5,
    'type'=>'match'
)
**//d change because withInner d**
$d = array( 
    '_or'=>array(
        array(
            'field'=>'d',
            'value'=>6,
            'type'=>'match'
        ),
        $h
    )
)   
$h = array(
    'field'=>'h',
    'value'=>8,
    'type'=>'match'
)

The Versions

24/04 2018

dev-master

9999999-dev https://github.com/feekk

A ElasticSearch PHP Dsl builder

  Sources   Download

MIT

by Avatar feekk

php dsl elastic

24/04 2018

1.0.3

1.0.3.0 https://github.com/feekk

A ElasticSearch PHP Dsl builder

  Sources   Download

MIT

by Avatar feekk

php dsl elastic

23/04 2018

1.0.2

1.0.2.0 https://github.com/feekk

A ElasticSearch PHP Dsl builder

  Sources   Download

MIT

by Avatar feekk

php dsl elastic

23/04 2018

1.0.1

1.0.1.0 https://github.com/feekk

A ElasticSearch PHP Dsl builder

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar feekk

php dsl elastic

23/04 2018

1.0.0

1.0.0.0 https://github.com/feekk

A ElasticSearch PHP Dsl builder

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Avatar feekk

php dsl elastic