wheres['must']['query'][]=['bool'=>['must'=>$shouldBuilder->getQueryData()]]; return $this; } $this->wheres['must']['where'][]=[ 'field'=>$field, 'value'=>$value ]; return $this; } /** * @param string|Closure $field * @param string|int $value * @return $this */ public function whereNot($field, $value) { if ($field instanceof Closure) { $shouldBuilder=new SubBuilder(); call_user_func($field, $shouldBuilder); $this->wheres['must_not']['query'][]=['bool'=>['must_not'=>$shouldBuilder->getQueryData()]]; return $this; } $this->wheres['must_not']['where'][]=[ 'field'=>$field, 'value'=>$value ]; return $this; } /** * @param string|Closure $field * @param string|int $value * @return $this */ public function whereOr($field, $value = null) { if ($field instanceof Closure) { $shouldBuilder=new SubBuilder(); call_user_func($field, $shouldBuilder); $this->wheres['should']['query'][]=['bool'=>['should'=>$shouldBuilder->getQueryData()]]; return $this; } $this->wheres['should']['where'][]=[ 'field'=>$field, 'value'=>$value ]; return $this; } /** * @param string $field * @param array $value * @param bool $isKeyword * @return $this */ public function whereIn(string $field, array $value, $isKeyword = true) { if ($isKeyword) { $this->wheres['in']['terms'][]=[ 'field'=>$field, 'value'=>$value ]; } else { $this->wheres['in']['phrase'][]=[ 'field'=>$field, 'value'=>$value ]; } return $this; } /** * @param string $field * @param array $value * @param bool $isKeyword * @return $this */ public function whereOrIn(string $field, array $value, $isKeyword = true) { if ($isKeyword) { $this->wheres['should_in']['terms'][]=[ 'field'=>$field, 'value'=>$value ]; } else { $this->wheres['should_in']['phrase'][]=[ 'field'=>$field, 'value'=>$value ]; } return $this; } /** * @param string $field * @param array $value * @param bool $isKeyword * @return $this */ public function whereNotIn(string $field, array $value, $isKeyword = true) { if ($isKeyword) { $this->wheres['not_in']['terms'][]=[ 'field'=>$field, 'value'=>$value ]; } else { $this->wheres['not_in']['phrase'][]=[ 'field'=>$field, 'value'=>$value ]; } return $this; } /** * @param string $field * @param null $min * @param null $max * @param bool $equal * @return $this */ public function whereRange(string $field, $min = null, $max = null, $equal = true) { $equal=$equal?'e':''; $range=[]; if (!is_null($min)) { $range['gt'.$equal]=$min; } if (!is_null($max)) { $range['lt'.$equal]=$max; } $this->wheres['must']['range'][]=[ 'field'=>$field, 'value'=>$range ]; return $this; } /** * @param string $field * @param null $min * @param null $max * @param bool $equal * @return $this */ public function whereOrRange(string $field, $min = null, $max = null, $equal = true) { $equal=$equal?'e':''; $range=[]; if (!is_null($min)) { $range['gt'.$equal]=$min; } if (!is_null($max)) { $range['lt'.$equal]=$max; } $this->wheres['should']['range'][]=[ 'field'=>$field, 'value'=>$range ]; return $this; } /** * @param string $field * @param null $min * @param null $max * @param bool $equal * @return $this */ public function whereNotRange(string $field, $min = null, $max = null, $equal = true) { $equal=$equal?'e':''; $range=[]; if (!is_null($min)) { $range['gt'.$equal]=$min; } if (!is_null($max)) { $range['lt'.$equal]=$max; } $this->wheres['must_not']['range'][]=[ 'field'=>$field, 'value'=>$range ]; return $this; } public function getQueryData() { $query = []; $must_data = []; $must_not_data = []; $should_data = []; if (empty($this->wheres)) { return $query; } //处理and逻辑 if (isset($this->wheres['must'])) { if (isset($this->wheres['must']['where'])) { foreach ($this->wheres['must']['where'] as $value) { $must_data[]=[ "match_phrase"=>[$value['field'] => $value['value']] ]; } } if (isset($this->wheres['must']['range'])) { foreach ($this->wheres['must']['range'] as $value) { $must_data[]=[ "range"=>[$value['field'] => $value['value']] ]; } } } if (isset($this->wheres['in'])) { if (isset($this->wheres['in']['terms'])) { foreach ($this->wheres['in']['terms'] as $value) { $must_data[]=[ "terms"=>[$value['field'] => $value['value']] ]; } } if (isset($this->wheres['in']['phrase'])) { $sub_data = []; foreach ($this->wheres['in']['phrase'] as $value) { foreach ($value['value'] as $v) { $sub_data[]=[ "match_phrase"=>[$value['field'] => $v] ]; } } $must_data[] = ['bool' => ['should'=>$sub_data]]; } } if (isset($this->wheres['query']['must'])) { $must_data=array_merge($must_data, $this->wheres['query']['must']); } //处理not逻辑 if (isset($this->wheres['must_not'])) { if (isset($this->wheres['must_not']['where'])) { foreach ($this->wheres['must_not']['where'] as $value) { $must_not_data[]=[ "match_phrase"=>[$value['field'] => $value['value']] ]; } } if (isset($this->wheres['must_not']['range'])) { foreach ($this->wheres['must_not']['range'] as $value) { $must_not_data[]=[ "range"=>[$value['field'] => $value['value']] ]; } } } if (isset($this->wheres['not_in'])) { if (isset($this->wheres['not_in']['terms'])) { foreach ($this->wheres['not_in']['terms'] as $value) { $must_not_data[]=[ "terms"=>[$value['field'] => $value['value']] ]; } } if (isset($this->wheres['not_in']['phrase'])) { foreach ($this->wheres['not_in']['phrase'] as $value) { foreach ($value['value'] as $v) { $must_not_data[]=[ "match_phrase"=>[$value['field'] => $v] ]; } } } } if (isset($this->wheres['query']['must_not'])) { $must_not_data=array_merge($must_not_data, $this->wheres['query']['must_not']); } //处理or逻辑 if (isset($this->wheres['should'])) { if (isset($this->wheres['should']['where'])) { foreach ($this->wheres['should']['where'] as $value) { $should_data[]=[ "match_phrase"=>[$value['field'] => $value['value']] ]; } } if (isset($this->wheres['should']['range'])) { foreach ($this->wheres['should']['range'] as $value) { $should_data[]=[ "range"=>[$value['field'] => $value['value']] ]; } } } if (isset($this->wheres['should_in'])) { if (isset($this->wheres['should_in']['terms'])) { foreach ($this->wheres['should_in']['terms'] as $value) { $should_data[]=[ "terms"=>[$value['field'] => $value['value']] ]; } } if (isset($this->wheres['should_in']['phrase'])) { foreach ($this->wheres['should_in']['phrase'] as $value) { foreach ($value['value'] as $v) { $should_data[]=[ "match_phrase"=>[$value['field'] => $v] ]; } } } } if (isset($this->wheres['query']['should'])) { $should_data=array_merge($should_data, $this->wheres['query']['should']); } if ($must_data) { $query[]=['bool'=>['must'=>$must_data]]; } if ($must_not_data) { $query[]=['bool'=>['must_not'=>$must_not_data]]; } if ($should_data) { $query[]=['bool'=>['should'=>$should_data]]; } return $query; } }