QueryString.php 6.5 KB

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