PHP code example of jfxy / elasticsearch-query-builder
1. Go to this page and download the library: Download jfxy/elasticsearch-query-builder 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/ */
jfxy / elasticsearch-query-builder example snippets
public function select($fields) :self
->select('id','name')
->select(['id','name'])
public function where($field, $operator = null, $value = null, $boolean = 'and', $not = false, $filter = false) :self
public function orWhere($field, $operator = null, $value = null) :self
public function whereNot($field, $value = null) :self
public function orWhereNot($field, $value = null) :self
->where('id',1)
->where('id','=',1)
->where('id',[1,2]) // 等同于 ->whereIn('id',[1,2])
->where('news_postdate','<=','2020-09-01') // 等同于 ->whereBetween('news_postdate',['<=' => '2020-09-01'])
// 闭包用法
->where(function($query){
return $query->where('id',1)->orWhere('status','>',0);
})
->orWhere(function($query){
return $query->where('id',1)->orWhere('status','>',0);
})
// 数组用法,下面两种写法类似,数组用法下的time条件顺序跟直接传入where方法顺序一致即可
->where(['id' => 1,'status' => [0,1],['time','>=','2020-09-01']])
->where(function($query){
$query->where('id',1)->where('status',[0,1])->where('time','>=','2020-09-01');
})
// whereNot实现 a != 1 and b != 2
->whereNot('a',1)->whereNot('b',2)
// whereNot实现 a != 1 or b != 2,即not(a=1 and b=2)
->whereNot(['a'=>1,'b'=>2])
->whereNot(function($query){
$query->where('a',1)->where('b',2);
})
public function filter($field, $operator = null, $value = null, $boolean = 'and',$not = false) :self
public function orFilter($field, $operator = null, $value = null) :self
public function filterNot($field, $value = null) :self
public function orFilterNot($field, $value = null) :self
public function whereIn($field, array $value, $boolean = 'and', $not = false) :self
public function whereNotIn($field, array $value, $boolean = 'and') :self
public function orWhereIn($field, array $value) :self
public function orWhereNotIn($field, array $value) :self
->whereIn('id',[1,2])
public function whereBetween($field, array $value, $boolean = 'and', $not = false) :self
public function whereNotBetween($field, array $value, $boolean = 'and') :self
public function orWhereBetween($field, array $value) :self
public function orWhereNotBetween($field, array $value) :self
->whereBetween('id',[1,10]) // 1 <= id <= 10
->whereBetween('id',[1,'<' => 10]) // 1 <= id < 10
->whereBetween('id',['>=' => 1,'<' => 10]) // 1 <= id < 10
public function whereExists($field,$boolean = 'and', $not = false) :self
public function whereNotExists($field) :self
public function orWhereExists($field) :self
public function orWhereNotExists($field) :self
->whereExists('news_uuid')
public function wherePrefix($field, $value, $appendParams = [], $boolean = 'and', $not = false) :self
public function whereNotPrefix($field, $value, $appendParams = []) :self
public function orWherePrefix($field, $value, $appendParams = []) :self
public function orWhereNotPrefix($field, $value, $appendParams = []) :self
->wherePrefix('news_url','http://www.baidu.com')
public function whereWildcard($field, $value, $appendParams = [], $boolean = 'and', $not = false) :self
public function whereNotWildcard($field, $value, $appendParams = []) :self
public function orWhereWildcard($field, $value, $appendParams = []) :self
public function orWhereNotWildcard($field, $value, $appendParams = []) :self
->whereWildcard('media_name','*公安')
public function whereRegexp($field, $value, $appendParams = [], $boolean = 'and', $not = false) :self
public function whereNotRegexp($field, $value, $appendParams = []) :self
public function orWhereRegexp($field, $value, $appendParams = []) :self
public function orWhereNotRegexp($field, $value, $appendParams = []) :self
->whereRegexp('media_name','.*公安')
public function whereFuzzy($field, $value, $appendParams = [], $boolean = 'and', $not = false) :self
public function whereNotFuzzy($field, $value, $appendParams = []) :self
public function orWhereFuzzy($field, $value, $appendParams = []) :self
public function orWhereNotFuzzy($field, $value, $appendParams = []) :self
->whereFuzzy('news_title','安徽合肥')
public function aggsFilter($alias,$wheres,... $subGroups) :self
->aggsFilter('alias1',function(Es $query){
$query->where('platform','web');
},function(Es $query){
$query->groupBy('platform_name',['size'=>30]);
})
->aggsFilter('alias2',['platform'=>'web','news_title'=>'合肥',['news_postdate','>=','2020-09-01']],function(Es $query){
$query->groupBy('platform_name',['size'=>30]);
})
public function raw($dsl) :self
->raw(['query'=>['match_all' => new \stdClass()]])->get()
->raw(json_encode(['query'=>['match_all' => new \stdClass()]]))->get()