1<?php 2 3namespace Elastica\Query; 4 5use Elastica\Exception\InvalidException; 6 7/** 8 * Bool query. 9 * 10 * @author Nicolas Ruflin <spam@ruflin.com> 11 * 12 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html 13 */ 14class BoolQuery extends AbstractQuery 15{ 16 /** 17 * Add should part to query. 18 * 19 * @param AbstractQuery|array $args Should query 20 * 21 * @return $this 22 */ 23 public function addShould($args): self 24 { 25 return $this->_addQuery('should', $args); 26 } 27 28 /** 29 * Add must part to query. 30 * 31 * @param AbstractQuery|array $args Must query 32 * 33 * @return $this 34 */ 35 public function addMust($args): self 36 { 37 return $this->_addQuery('must', $args); 38 } 39 40 /** 41 * Add must not part to query. 42 * 43 * @param AbstractQuery|array $args Must not query 44 * 45 * @return $this 46 */ 47 public function addMustNot($args): self 48 { 49 return $this->_addQuery('must_not', $args); 50 } 51 52 /** 53 * Sets the filter. 54 * 55 * @return $this 56 */ 57 public function addFilter(AbstractQuery $filter): self 58 { 59 return $this->addParam('filter', $filter); 60 } 61 62 /** 63 * Sets boost value of this query. 64 * 65 * @param float $boost Boost value 66 * 67 * @return $this 68 */ 69 public function setBoost(float $boost): self 70 { 71 return $this->setParam('boost', $boost); 72 } 73 74 /** 75 * Sets the minimum number of should clauses to match. 76 * 77 * @param int|string $minimum Minimum value 78 * 79 * @return $this 80 */ 81 public function setMinimumShouldMatch($minimum): self 82 { 83 return $this->setParam('minimum_should_match', $minimum); 84 } 85 86 /** 87 * {@inheritdoc} 88 */ 89 public function toArray(): array 90 { 91 if (!$this->_params) { 92 $this->_params = new \stdClass(); 93 } 94 95 return parent::toArray(); 96 } 97 98 /** 99 * Adds a query to the current object. 100 * 101 * @param string $type Query type 102 * @param AbstractQuery|array $args Query 103 * 104 * @throws InvalidException If not valid query 105 * 106 * @return $this 107 */ 108 protected function _addQuery(string $type, $args): self 109 { 110 if (!\is_array($args) && !($args instanceof AbstractQuery)) { 111 throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractQuery'); 112 } 113 114 return $this->addParam($type, $args); 115 } 116} 117