# elasticsearch-query-builder **Repository Path**: luofusheng/elasticsearch-query-builder ## Basic Information - **Project Name**: elasticsearch-query-builder - **Description**: a query builder for elasticsearch - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-10-22 - **Last Updated**: 2023-06-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # elasticsearch-query-builder ## 安装 本包是复制的jfxy/elasticsearch-query-builder包,为了解决github下载慢的问题。 ``` composer require luofusheng/elasticsearch-query-builder ``` ## 注意 * elasticsearch <= 6.8 * php >= 7.1 * 需要子类继承**luofusheng\ElasticSearch\Builder**并实现**query()** 和 **scrollQuery()** * 复杂的业务查询应该在子类中封装 * 下面将子类定义为**Es** ## 方法 #### select ```php public function select($fields) :self ->select('id','name') ->select(['id','name']) ``` #### where * 比较运算符支持 **=,>,>=,<,<=,!=,<>** * where、orWhere、whereNot、orWhereNot均支持闭包调用,而orWhere、whereNot、orWhereNot则是对闭包内的整体条件进行 or 和 not 的操作,闭包用法类似mysql中对闭包内的条件前后加上() * 在封装业务代码存在or关系时,应使用闭包包裹内部条件 ```php 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); }) ``` #### filter * 用法同where一致,不过条件会写在filter下 ```php 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 ``` #### in ```php 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]) ``` #### between * 默认为闭区间,比较运算符支持 **>,>=,<,<=** ```php 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 ``` #### exists * 字段不存在或为null ```php 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') ``` #### prefix 前缀匹配 ```php 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') ``` #### wildcard 通配符匹配 ```php 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','*公安') ``` #### regexp 正则匹配 ```php 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','.*公安') ``` #### fuzzy 模糊查询 ```php 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','安徽合肥') ``` #### whereRaw 原生条件 ```php public function whereRaw($where, $boolean = 'and', $not = false) :self public function orWhereRaw($where) :self // 下面的例子是由于where方法提供的term查询无法设置一些其他的参数,可以改为使用whereRaw ->whereRaw([ "term" => [ "news_title" => [ "value" => "安徽", "boost" => 2 ] ] ]) ->whereRaw([ 'bool' => [ 'must' => [ "term" => [ "news_title" => [ "value" => "安徽", "boost" => 2 ] ] ] ] ]) ``` #### match * whereMatch方法,$type=match、match_phrase、match_phrase_prefix * whereMultiMatch方法,$type=best_fields、most_fields、cross_fields、phrase、phrase_prefix ```php // 单字段 public function whereMatch($field, $value = null,$type = 'match',array $appendParams = [], $boolean = 'and', $not = false) :self public function orWhereMatch($field, $value = null,$type = 'match',array $appendParams = []) :self public function whereNotMatch($field, $value = null,$type = 'match',array $appendParams = []) :self public function orWhereNotMatch($field, $value = null,$type = 'match',array $appendParams = []) :self // 多字段 public function whereMultiMatch($field, $value = null,$type = 'best_fields',array $appendParams = [], $boolean = 'and', $not = false) :self public function orWhereMultiMatch($field, $value = null,$type = 'best_fields',array $appendParams = []) :self public function whereNotMultiMatch($field, $value = null,$type = 'best_fields',array $appendParams = []) :self public function orWhereNotMultiMatch($field, $value = null,$type = 'best_fields',array $appendParams = []) :self ->whereMatch('news_title','上海','match_phrase',['slop'=>1]) ->whereMultiMatch(['news_title','news_content'],'上海','phrase',["operator" => "OR"]) ``` #### minimumShouldMatch 最小匹配度 ```php 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%'); }) ``` #### whereNested nested类型字段查询 * 仅支持传入闭包和数组条件 ```php public function whereNested($path,$wheres,$appendParams = []) :self ->whereNested('skus',function(Es $query){ $query->where('skus.title','iphone')->where('skus.des','iphone'); },['inner_hits'=>['highlight' => ['fields'=>['skus.title'=>new \stdClass()]]]]); ->whereNested('skus',['skus.title' => 'iphone','skus.description' => 'iphone',['skus.price','>','100']],['inner_hits'=>['highlight' => ['fields'=>['skus.title'=>new \stdClass()]]]]); ``` #### postWhere 后置过滤器 * postWhere方法添加的条件会作用于post_filter查询,条件作用于聚合之后 * postWhere方法参数同where方法相同,复杂的检索可以传入数组或闭包 ```php public function postWhere($field, $operator = null, $value = null, $boolean = 'and',$not = false) :self ->postWhere('platform','wx') ->postWhere(['platform' => ['wx','web'],['news_posttime','>','2020-09-01 00:00:00']]) ->postWhere(function(Es $query){ $query->where('platform','wx')->whereNotMatch('news_title','安徽合肥')->orWhereIn('news_postdate',['2020-09-01','2020-09-02']); }) ``` #### when * $value为true时会执行$callback,否则当$default存在时会执行$default ```php public function when($value,$callback,$default = null) :self ->when(1 > 2,function($query){ return $query->whereBetween('news_postdate',['2020-05-01','2020-05-05']); },function($query){ return $query->whereBetween('news_postdate',['2020-05-09','2020-05-10']); }) ``` #### collapse 折叠 * 使用collapse方法并不会使返回的总数发生变化,计算折叠后的总数需要配合cardinality聚合使用 * collapse方法和paginator方法一起使用时,paginator方法内部会对折叠的字段做cardinality聚合,不需要考虑collapse的总数问题 ```php public function collapse(string $field,array $appendParams = []) :self ->collapse('news_sim_hash') ->collapse('news_sim_hash')->aggs('alias','cardinality',['field'=>'news_sim_hash']) ->collapse('news_sim_hash')->cardinality('news_sim_hash') ->collapse('news_sim_hash')->paginator() ``` #### from ```php public function from(int $value) :self ``` #### size ```php public function size(int $value) :self ``` #### orderBy 排序 ```php public function orderBy(string $field, $sort = 'asc') :self ->orderBy('news_posttime','asc')->orderBy('news_like_count','desc') ``` #### highlight 高亮 * 高亮配置及高亮字段 * 建议先在Es子类中设置highlightConfig通用属性 ```php // 根据自己的需要在子类中配置 public $highlightConfig = [ "require_field_match" => false, // 是否只高亮查询的字段 "number_of_fragments" => 0, // 高亮字段会被分段,返回分段的个数,设置0不分段 "pre_tags" => "", "post_tags" => "", ]; ``` * 使用highlightConfig方法会覆盖highlightConfig通用属性中的同键名配置 * highlight方法指定高亮字段并且设置指定字段的高亮属性 ```php public function highlight(string $field,array $params = []) public function highlightConfig(array $config = []) ->highlightConfig(['require_field_match'=>false,'number_of_fragments' => 0,'pre_tags'=>'