1<?php 2 3namespace Elastica\Query; 4 5/** 6 * QueryString query. 7 * 8 * @author Nicolas Ruflin <spam@ruflin.com>, Jasper van Wanrooy <jasper@vanwanrooy.net> 9 * 10 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html 11 */ 12class QueryString extends AbstractQuery 13{ 14 /** 15 * Query string. 16 * 17 * @var string Query string 18 */ 19 protected $_queryString; 20 21 /** 22 * Creates query string object. Calls setQuery with argument. 23 * 24 * @param string $queryString OPTIONAL Query string for object 25 */ 26 public function __construct(string $queryString = '') 27 { 28 $this->setQuery($queryString); 29 } 30 31 /** 32 * Sets a new query string for the object. 33 * 34 * @param string $query Query string 35 * 36 * @return $this 37 */ 38 public function setQuery(string $query = ''): self 39 { 40 return $this->setParam('query', $query); 41 } 42 43 /** 44 * Sets the default field. 45 * You cannot set fields and default_field. 46 * 47 * If no field is set, _all is chosen 48 * 49 * @param string $field Field 50 * 51 * @return $this 52 */ 53 public function setDefaultField(string $field): self 54 { 55 return $this->setParam('default_field', $field); 56 } 57 58 /** 59 * Sets the default operator AND or OR. 60 * 61 * If no operator is set, OR is chosen 62 * 63 * @param string $operator Operator 64 * 65 * @return $this 66 */ 67 public function setDefaultOperator(string $operator = 'or'): self 68 { 69 return $this->setParam('default_operator', $operator); 70 } 71 72 /** 73 * Sets the analyzer to analyze the query with. 74 * 75 * @param string $analyzer Analyser to use 76 * 77 * @return $this 78 */ 79 public function setAnalyzer(string $analyzer): self 80 { 81 return $this->setParam('analyzer', $analyzer); 82 } 83 84 /** 85 * Sets the parameter to allow * and ? as first characters. 86 * 87 * If not set, defaults to true. 88 * 89 * @return $this 90 */ 91 public function setAllowLeadingWildcard(bool $allow = true): self 92 { 93 return $this->setParam('allow_leading_wildcard', $allow); 94 } 95 96 /** 97 * Sets the parameter to enable the position increments in result queries. 98 * 99 * If not set, defaults to true. 100 * 101 * @return $this 102 */ 103 public function setEnablePositionIncrements(bool $enabled = true): self 104 { 105 return $this->setParam('enable_position_increments', $enabled); 106 } 107 108 /** 109 * Sets the fuzzy prefix length parameter. 110 * 111 * If not set, defaults to 0. 112 * 113 * @return $this 114 */ 115 public function setFuzzyPrefixLength(int $length = 0): self 116 { 117 return $this->setParam('fuzzy_prefix_length', $length); 118 } 119 120 /** 121 * Sets the fuzzy minimal similarity parameter. 122 * 123 * If not set, defaults to 0.5 124 * 125 * @return $this 126 */ 127 public function setFuzzyMinSim(float $minSim = 0.5): self 128 { 129 return $this->setParam('fuzzy_min_sim', $minSim); 130 } 131 132 /** 133 * Sets the phrase slop. 134 * 135 * If zero, exact phrases are required. 136 * If not set, defaults to zero. 137 * 138 * @return $this 139 */ 140 public function setPhraseSlop(int $phraseSlop = 0): self 141 { 142 return $this->setParam('phrase_slop', $phraseSlop); 143 } 144 145 /** 146 * Sets the boost value of the query. 147 * 148 * If not set, defaults to 1.0. 149 * 150 * @return $this 151 */ 152 public function setBoost(float $boost = 1.0): self 153 { 154 return $this->setParam('boost', $boost); 155 } 156 157 /** 158 * Allows analyzing of wildcard terms. 159 * 160 * If not set, defaults to true 161 * 162 * @return $this 163 */ 164 public function setAnalyzeWildcard(bool $analyze = true): self 165 { 166 return $this->setParam('analyze_wildcard', $analyze); 167 } 168 169 /** 170 * Sets the fields. If no fields are set, _all is chosen. 171 * You cannot set fields and default_field. 172 * 173 * @param array $fields Fields 174 * 175 * @return $this 176 */ 177 public function setFields(array $fields): self 178 { 179 return $this->setParam('fields', $fields); 180 } 181 182 /** 183 * Whether to use bool or dis_max queries to internally combine results for multi field search. 184 * 185 * @param bool $value Determines whether to use 186 * 187 * @return $this 188 */ 189 public function setUseDisMax(bool $value = true): self 190 { 191 return $this->setParam('use_dis_max', $value); 192 } 193 194 /** 195 * When using dis_max, the disjunction max tie breaker. 196 * 197 * If not set, defaults to 0.0. 198 * 199 * @return $this 200 */ 201 public function setTieBreaker(float $tieBreaker = 0.0): self 202 { 203 return $this->setParam('tie_breaker', $tieBreaker); 204 } 205 206 /** 207 * Set a re-write condition. See https://github.com/elasticsearch/elasticsearch/issues/1186 for additional information. 208 * 209 * @return $this 210 */ 211 public function setRewrite(string $rewrite = ''): self 212 { 213 return $this->setParam('rewrite', $rewrite); 214 } 215 216 /** 217 * Set timezone option. 218 * 219 * @return $this 220 */ 221 public function setTimezone(string $timezone): self 222 { 223 return $this->setParam('time_zone', $timezone); 224 } 225 226 /** 227 * {@inheritdoc} 228 */ 229 public function toArray(): array 230 { 231 return ['query_string' => \array_merge(['query' => $this->_queryString], $this->getParams())]; 232 } 233} 234