1. Go to this page and download the library: Download jfxy/elasticsearch 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/ */
public function setIndex($index)
->setIndex('index1')
->setIndex('index1,index2')
->setIndex(['index1','index2'])
public function index(array $data, string $id = null)
public function create(array $data, string $id)
->create(['id' => 1,'name' => 'jfxy'],1)
public function update(array $data, string $id)
->update(['id' => 1,'name' => 'jfxyl'],1)
public function delete(string $id)
public function select($columns) :self
->select('id','name')
->select(['id','name'])
public function where($column, $operator = null, $value = null, $match = 'term', $boolean = 'and',$not = false) :self
public function orWhere($column, $operator = null, $value = null) :self
public function whereNot($column, $value = null) :self
public function orWhereNot($column, $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($column, array $value, $boolean = 'and', $not = false) :self
public function whereNotIn($column, array $value, $boolean = 'and') :self
public function orWhereIn($column, array $value) :self
public function orWhereNotIn($column, array $value) :self
->whereIn('id',[1,2])
public function whereBetween($column, array $value, $boolean = 'and', $not = false) :self
public function whereNotBetween($column, array $value, $boolean = 'and') :self
public function orWhereBetween($column, array $value) :self
public function orWhereNotBetween($column, 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($column,$boolean = 'and', $not = false) :self
public function whereNotExists($column) :self
public function orWhereExists($column) :self
public function orWhereNotExists($column) :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 whereMatch($column, $value = null,$type = 'match',array $appendParams = [], $boolean = 'and', $not = false) :self
public function orWhereMatch($column, $value = null,$type = 'match',array $appendParams = []) :self
public function whereNotMatch($column, $value = null,$type = 'match',array $appendParams = []) :self
public function orWhereNotMatch($column, $value = null,$type = 'match',array $appendParams = []) :self
// 多字段
public function whereMultiMatch($column, $value = null,$type = 'best_fields',array $appendParams = [], $boolean = 'and', $not = false) :self
public function orWhereMultiMatch($column, $value = null,$type = 'best_fields',array $appendParams = []) :self
public function whereNotMultiMatch($column, $value = null,$type = 'best_fields',array $appendParams = []) :self
public function orWhereNotMultiMatch($column, $value = null,$type = 'best_fields',array $appendParams = []) :self
->whereMatch('news_title','上海','match_phrase',['slop'=>1])
->whereMultiMatch(['news_title','news_content'],'上海','phrase',["operator" => "OR"])
public function minimumShouldMatch($value) :self
->where('aa',1)->orWhere('bb',1)->orWhere('cc',1)->minimumShouldMatch(2)
->where(function(Es $query){
$query->where('aa',1)->orWhere('bb',1)->orWhere('cc',1)
->minimumShouldMatch('50%');
})
->postWhere(function(Es $query){
$query->where('aa',1)->orWhere('bb',1)->orWhere('cc',1)
->minimumShouldMatch('50%');
})
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()