123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- namespace App\Search\Builders;
- use Closure;
- use Laravel\Scout\Builder;
- class ElasticsearchBuilder extends Builder
- {
- /**
- * @param string|Closure $field
- * @param mixed $value
- * @return $this
- */
- public function where($field, $value = null)
- {
- if ($field instanceof Closure) {
- $shouldBuilder=new SubBuilder();
- call_user_func($field, $shouldBuilder);
- $this->wheres['must']['query'][]=$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 = null)
- {
- if ($field instanceof Closure) {
- $shouldBuilder=new SubBuilder();
- call_user_func($field, $shouldBuilder);
- $this->wheres['must_not']['query'][]=$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'][]=$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;
- }
- /**
- * @param string $field
- * @param $lat
- * @param $lon
- * @param $distance
- * @return $this
- */
- public function whereLocation(string $field, $lat, $lon, $distance)
- {
- $this->wheres['locations'][]=['distance'=>$distance, $field=>['lat'=>$lat, "lon"=>$lon]];
- 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['must']['query'])) {
- $must_data=array_merge($must_data, $this->wheres['must']['query']);
- }
- //处理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['must_not']['query'])) {
- $must_not_data=array_merge($must_not_data, $this->wheres['must_not']['query']);
- }
- //处理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['should']['query'])) {
- $should_data=array_merge($should_data, $this->wheres['should']['query']);
- }
- if ($must_data) {
- $query['must']=$must_data;
- }
- if ($must_not_data) {
- $query['must_not']=$must_not_data;
- }
- if ($should_data) {
- $query['should']=$should_data;
- }
- if (isset($this->wheres['locations'])) {
- $data =[];
- foreach ($this->wheres['locations'] as $location) {
- $data[]=[
- "geo_distance"=>$location
- ];
- }
- $query['filter']=$data;
- }
- return $query;
- }
- }
|