Last active
August 5, 2020 01:25
-
-
Save diloabininyeri/fa3e17688d2bac1191c197a4f7937611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Elastic; | |
use stdClass; | |
/** | |
* Class BoolQuery. | |
* @author dılo sürücü <[email protected]> | |
*/ | |
class BoolQuery | |
{ | |
/** | |
* @var array | |
*/ | |
private array $query; | |
/** | |
* @var int|null | |
*/ | |
private ?int $size = null; | |
/** | |
* @var int|null | |
*/ | |
private ?int $from = null; | |
/** | |
* @var int|null | |
*/ | |
private ?int $minShouldMatch=null; | |
private ?int $boost=null; | |
/** | |
* @var ElasticBuilderQuery | |
*/ | |
private ElasticBuilderQuery $builderQuery; | |
/** | |
* BoolQuery constructor. | |
* @param ElasticBuilderQuery $builderQuery | |
*/ | |
public function __construct(ElasticBuilderQuery $builderQuery) | |
{ | |
$this->builderQuery = $builderQuery; | |
} | |
/** | |
* @return $this | |
*/ | |
public function mustMatchAll(): self | |
{ | |
$this->query['must'][] = ['match_all' => new stdClass()]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* | |
* @return $this | |
*/ | |
public function mustMatch(string $key, $value): self | |
{ | |
$this->query['must'][] = ['match' => [trim($key) => $value]]; | |
return $this; | |
} | |
/** | |
* @param array $keys | |
* @param $value | |
* @param string $fuzziness | |
* @param int $maxExpansions | |
* @return $this | |
*/ | |
public function mustMultiMatch(array $keys, $value, $fuzziness='AUTO', $maxExpansions=50):self | |
{ | |
$this->query['must'][] = ['multi_match' => ['fields'=>$keys,'query'=>$value,'fuzziness'=>$fuzziness,'max_expansions'=>$maxExpansions]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* | |
* @return $this | |
*/ | |
public function mustTerm(string $key, $value): self | |
{ | |
$this->query['must'][] = ['term' => [trim($key) => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* | |
* @return $this | |
*/ | |
public function filterTerm(string $key, $value): self | |
{ | |
$this->query['filter'][] = ['term' => [trim($key) => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* | |
* @return $this | |
*/ | |
public function filterMatch(string $key, $value): self | |
{ | |
$this->query['filter'][] = ['match' => [trim($key) => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* | |
* @return $this | |
*/ | |
public function mustNotTerm(string $key, $value): self | |
{ | |
$this->query['must_not'][] = ['term' => [trim($key) => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* | |
* @return $this | |
*/ | |
public function mustNotMatch(string $key, $value): self | |
{ | |
$this->query['must_not'][] = ['match' => [trim($key) => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* @return $this | |
*/ | |
public function shouldTerm(string $key, $value): self | |
{ | |
$this->query['should'] [] = ['term' => [$key => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $value | |
* @return $this | |
*/ | |
public function shouldMatch(string $key, $value): self | |
{ | |
$this->query['should'] [] = ['match' => [$key => $value]]; | |
return $this; | |
} | |
/** | |
* @param string $key | |
* @param $logical | |
* @return $this | |
*/ | |
public function shouldRange(string $key, array $logical): self | |
{ | |
//$this->query['should'] [] = ['range' => [$key => ['gte' => 75]]]; | |
$this->query['should'] [] = ['range' => [$key => $logical]]; | |
return $this; | |
} | |
/** | |
* @param int $size | |
* @return $this | |
*/ | |
public function size(int $size): self | |
{ | |
$this->size = $size; | |
return $this; | |
} | |
/** | |
* @param int $int | |
* @return $this | |
*/ | |
public function from(int $int): self | |
{ | |
$this->from = $int; | |
return $this; | |
} | |
/** | |
* @param int $minimumMatch | |
* @return $this | |
*/ | |
public function minimumShouldMatch(int $minimumMatch):self | |
{ | |
$this->minShouldMatch=$minimumMatch; | |
return $this; | |
} | |
/** | |
* @param int $boost | |
* @return $this | |
*/ | |
public function boost(int $boost):self | |
{ | |
$this->boost=$boost; | |
return $this; | |
} | |
/** | |
* @return array | |
*/ | |
public function getQuery(): array | |
{ | |
return [ | |
'index' => $this->builderQuery->getModel()->getIndex(), | |
'body' => $this->bodyQuery(), | |
]; | |
} | |
/** | |
* @return \array[][] | |
*/ | |
private function bodyQuery(): array | |
{ | |
$body = [ | |
'query' => [ | |
'bool' => $this->query, | |
], | |
]; | |
if ($this->size) { | |
$body['size'] = $this->size; | |
} | |
if ($this->from) { | |
$body['from'] = $this->from; | |
} | |
if ($this->minShouldMatch) { | |
$body['query']['bool']['minimum_should_match']=$this->minShouldMatch; | |
} | |
if ($this->boost) { | |
$body['query']['bool']['boost']=$this->boost; | |
} | |
return $body; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using
$bool=new BoolQuery();
$bool->mustMatchAll()
->mustMatch('name','Dılo')
->mustNotMatch('surname','sürücü')
->shouldRange('age',['lt'=>58]);
echo $bool->getQuery();