Query.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. namespace Elastica\QueryBuilder\DSL;
  3. use Elastica\Exception\NotImplementedException;
  4. use Elastica\Query\AbstractSpanQuery;
  5. use Elastica\Query\BoolQuery;
  6. use Elastica\Query\Boosting;
  7. use Elastica\Query\Common;
  8. use Elastica\Query\ConstantScore;
  9. use Elastica\Query\DisMax;
  10. use Elastica\Query\Exists;
  11. use Elastica\Query\FunctionScore;
  12. use Elastica\Query\Fuzzy;
  13. use Elastica\Query\GeoDistance;
  14. use Elastica\Query\HasChild;
  15. use Elastica\Query\HasParent;
  16. use Elastica\Query\Ids;
  17. use Elastica\Query\Match;
  18. use Elastica\Query\MatchAll;
  19. use Elastica\Query\MatchNone;
  20. use Elastica\Query\MoreLikeThis;
  21. use Elastica\Query\MultiMatch;
  22. use Elastica\Query\Nested;
  23. use Elastica\Query\ParentId;
  24. use Elastica\Query\Percolate;
  25. use Elastica\Query\Prefix;
  26. use Elastica\Query\QueryString;
  27. use Elastica\Query\Range;
  28. use Elastica\Query\Regexp;
  29. use Elastica\Query\SimpleQueryString;
  30. use Elastica\Query\SpanContaining;
  31. use Elastica\Query\SpanFirst;
  32. use Elastica\Query\SpanMulti;
  33. use Elastica\Query\SpanNear;
  34. use Elastica\Query\SpanNot;
  35. use Elastica\Query\SpanOr;
  36. use Elastica\Query\SpanTerm;
  37. use Elastica\Query\SpanWithin;
  38. use Elastica\Query\Term;
  39. use Elastica\Query\Terms;
  40. use Elastica\Query\Type;
  41. use Elastica\Query\Wildcard;
  42. use Elastica\QueryBuilder\DSL;
  43. /**
  44. * elasticsearch query DSL.
  45. *
  46. * @author Manuel Andreo Garcia <andreo.garcia@googlemail.com>
  47. *
  48. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-queries.html
  49. */
  50. class Query implements DSL
  51. {
  52. /**
  53. * must return type for QueryBuilder usage.
  54. *
  55. * @return string
  56. */
  57. public function getType()
  58. {
  59. return self::TYPE_QUERY;
  60. }
  61. /**
  62. * match query.
  63. *
  64. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
  65. *
  66. * @param string $field
  67. * @param mixed $values
  68. *
  69. * @return Match
  70. */
  71. public function match($field = null, $values = null)
  72. {
  73. return new Match($field, $values);
  74. }
  75. /**
  76. * multi match query.
  77. *
  78. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html
  79. *
  80. * @return \Elastica\Query\MultiMatch
  81. */
  82. public function multi_match()
  83. {
  84. return new MultiMatch();
  85. }
  86. /**
  87. * bool query.
  88. *
  89. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
  90. *
  91. * @return \Elastica\Query\BoolQuery
  92. */
  93. public function bool()
  94. {
  95. return new BoolQuery();
  96. }
  97. /**
  98. * boosting query.
  99. *
  100. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html
  101. *
  102. * @return Boosting
  103. */
  104. public function boosting()
  105. {
  106. return new Boosting();
  107. }
  108. /**
  109. * common terms query.
  110. *
  111. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html
  112. *
  113. * @param string $field
  114. * @param string $query
  115. * @param float $cutoffFrequency percentage in decimal form (.001 == 0.1%)
  116. *
  117. * @return Common
  118. */
  119. public function common_terms($field, $query, $cutoffFrequency)
  120. {
  121. return new Common($field, $query, $cutoffFrequency);
  122. }
  123. /**
  124. * constant score query.
  125. *
  126. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
  127. *
  128. * @param \Elastica\Query\AbstractQuery|array|null $filter
  129. *
  130. * @return ConstantScore
  131. */
  132. public function constant_score($filter = null)
  133. {
  134. return new ConstantScore($filter);
  135. }
  136. /**
  137. * dis max query.
  138. *
  139. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html
  140. *
  141. * @return DisMax
  142. */
  143. public function dis_max()
  144. {
  145. return new DisMax();
  146. }
  147. /**
  148. * function score query.
  149. *
  150. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
  151. *
  152. * @return FunctionScore
  153. */
  154. public function function_score()
  155. {
  156. return new FunctionScore();
  157. }
  158. /**
  159. * fuzzy query.
  160. *
  161. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
  162. *
  163. * @param string $fieldName Field name
  164. * @param string $value String to search for
  165. *
  166. * @return Fuzzy
  167. */
  168. public function fuzzy($fieldName = null, $value = null)
  169. {
  170. return new Fuzzy($fieldName, $value);
  171. }
  172. /**
  173. * geo shape query.
  174. *
  175. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html
  176. */
  177. public function geo_shape()
  178. {
  179. throw new NotImplementedException();
  180. }
  181. /**
  182. * has child query.
  183. *
  184. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html
  185. *
  186. * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query
  187. * @param string $type Parent document type
  188. *
  189. * @return HasChild
  190. */
  191. public function has_child($query, $type = null)
  192. {
  193. return new HasChild($query, $type);
  194. }
  195. /**
  196. * has parent query.
  197. *
  198. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html
  199. *
  200. * @param string|\Elastica\Query|\Elastica\Query\AbstractQuery $query
  201. * @param string $type Parent document type
  202. *
  203. * @return HasParent
  204. */
  205. public function has_parent($query, $type)
  206. {
  207. return new HasParent($query, $type);
  208. }
  209. /**
  210. * ids query.
  211. *
  212. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html
  213. *
  214. * @param array $ids
  215. *
  216. * @return Ids
  217. */
  218. public function ids(array $ids = [])
  219. {
  220. return new Ids($ids);
  221. }
  222. /**
  223. * match all query.
  224. *
  225. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html
  226. *
  227. * @return MatchAll
  228. */
  229. public function match_all()
  230. {
  231. return new MatchAll();
  232. }
  233. /**
  234. * match none query.
  235. *
  236. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
  237. *
  238. * @return MatchNone
  239. */
  240. public function match_none()
  241. {
  242. return new MatchNone();
  243. }
  244. /**
  245. * more like this query.
  246. *
  247. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html
  248. *
  249. * @return MoreLikeThis
  250. */
  251. public function more_like_this()
  252. {
  253. return new MoreLikeThis();
  254. }
  255. /**
  256. * nested query.
  257. *
  258. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
  259. *
  260. * @return Nested
  261. */
  262. public function nested()
  263. {
  264. return new Nested();
  265. }
  266. /**
  267. * @param $type
  268. * @param $id
  269. * @param $ignoreUnmapped
  270. *
  271. * @return ParentId ParentId
  272. */
  273. public function parent_id($type, $id, $ignoreUnmapped = false)
  274. {
  275. return new ParentId($type, $id, $ignoreUnmapped);
  276. }
  277. /**
  278. * prefix query.
  279. *
  280. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html
  281. *
  282. * @param array $prefix Prefix array
  283. *
  284. * @return Prefix
  285. */
  286. public function prefix(array $prefix = [])
  287. {
  288. return new Prefix($prefix);
  289. }
  290. /**
  291. * query string query.
  292. *
  293. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
  294. *
  295. * @param string $queryString OPTIONAL Query string for object
  296. *
  297. * @return QueryString
  298. */
  299. public function query_string($queryString = '')
  300. {
  301. return new QueryString($queryString);
  302. }
  303. /**
  304. * simple_query_string query.
  305. *
  306. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
  307. *
  308. * @param string $query
  309. * @param array $fields
  310. *
  311. * @return SimpleQueryString
  312. */
  313. public function simple_query_string($query, array $fields = [])
  314. {
  315. return new SimpleQueryString($query, $fields);
  316. }
  317. /**
  318. * range query.
  319. *
  320. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html
  321. *
  322. * @param string $fieldName
  323. * @param array $args
  324. *
  325. * @return Range
  326. */
  327. public function range($fieldName = null, array $args = [])
  328. {
  329. return new Range($fieldName, $args);
  330. }
  331. /**
  332. * regexp query.
  333. *
  334. * @param string $key
  335. * @param string $value
  336. * @param float $boost
  337. *
  338. * @return Regexp
  339. *
  340. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html
  341. */
  342. public function regexp($key = '', $value = null, $boost = 1.0)
  343. {
  344. return new Regexp($key, $value, $boost);
  345. }
  346. /**
  347. * span first query.
  348. *
  349. * @param \Elastica\Query\AbstractQuery|array $match
  350. * @param int $end
  351. *
  352. * @return SpanFirst
  353. *
  354. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html
  355. */
  356. public function span_first($match = null, $end = null)
  357. {
  358. return new SpanFirst($match, $end);
  359. }
  360. /**
  361. * span multi term query.
  362. *
  363. * @param \Elastica\Query\AbstractQuery|array $match
  364. *
  365. * @return SpanMulti
  366. *
  367. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html
  368. */
  369. public function span_multi_term($match = null)
  370. {
  371. return new SpanMulti($match);
  372. }
  373. /**
  374. * span near query.
  375. *
  376. * @param array $clauses
  377. * @param int $slop
  378. * @param bool $inOrder
  379. *
  380. * @return SpanNear
  381. *
  382. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html
  383. */
  384. public function span_near($clauses = [], $slop = 1, $inOrder = false)
  385. {
  386. return new SpanNear($clauses, $slop, $inOrder);
  387. }
  388. /**
  389. * span not query.
  390. *
  391. * @param AbstractSpanQuery|null $include
  392. * @param AbstractSpanQuery|null $exclude
  393. *
  394. * @return SpanNot
  395. *
  396. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html
  397. */
  398. public function span_not(AbstractSpanQuery $include = null, AbstractSpanQuery $exclude = null)
  399. {
  400. return new SpanNot($include, $exclude);
  401. }
  402. /**
  403. * span_or query.
  404. *
  405. * @param array $clauses
  406. *
  407. * @return SpanOr
  408. *
  409. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html
  410. */
  411. public function span_or($clauses = [])
  412. {
  413. return new SpanOr($clauses);
  414. }
  415. /**
  416. * span_term query.
  417. *
  418. * @param array $term
  419. *
  420. * @return SpanTerm
  421. *
  422. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html
  423. */
  424. public function span_term(array $term = [])
  425. {
  426. return new SpanTerm($term);
  427. }
  428. /**
  429. * span_containing query.
  430. *
  431. * @param AbstractSpanQuery|null $little
  432. * @param AbstractSpanQuery|null $big
  433. *
  434. * @return SpanContaining
  435. *
  436. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html
  437. */
  438. public function span_containing(AbstractSpanQuery $little = null, AbstractSpanQuery $big = null)
  439. {
  440. return new SpanContaining($little, $big);
  441. }
  442. /**
  443. * span_within query.
  444. *
  445. * @param AbstractSpanQuery|null $little
  446. * @param AbstractSpanQuery|null $big
  447. *
  448. * @return SpanWithin
  449. *
  450. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html
  451. */
  452. public function span_within(AbstractSpanQuery $little = null, AbstractSpanQuery $big = null)
  453. {
  454. return new SpanWithin($little, $big);
  455. }
  456. /**
  457. * term query.
  458. *
  459. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
  460. *
  461. * @param array $term
  462. *
  463. * @return Term
  464. */
  465. public function term(array $term = [])
  466. {
  467. return new Term($term);
  468. }
  469. /**
  470. * terms query.
  471. *
  472. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
  473. *
  474. * @param string $key
  475. * @param array $terms
  476. *
  477. * @return Terms
  478. */
  479. public function terms($key = '', array $terms = [])
  480. {
  481. return new Terms($key, $terms);
  482. }
  483. /**
  484. * wildcard query.
  485. *
  486. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html
  487. *
  488. * @param string $key OPTIONAL Wildcard key
  489. * @param string $value OPTIONAL Wildcard value
  490. * @param float $boost OPTIONAL Boost value (default = 1)
  491. *
  492. * @return Wildcard
  493. */
  494. public function wildcard($key = '', $value = null, $boost = 1.0)
  495. {
  496. return new Wildcard($key, $value, $boost);
  497. }
  498. /**
  499. * geo distance query.
  500. *
  501. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html
  502. *
  503. * @param string $key
  504. * @param array|string $location
  505. * @param string $distance
  506. *
  507. * @return GeoDistance
  508. */
  509. public function geo_distance($key, $location, $distance)
  510. {
  511. return new GeoDistance($key, $location, $distance);
  512. }
  513. /**
  514. * exists query.
  515. *
  516. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
  517. *
  518. * @param string $field
  519. *
  520. * @return Exists
  521. */
  522. public function exists($field)
  523. {
  524. return new Exists($field);
  525. }
  526. /**
  527. * type query.
  528. *
  529. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html
  530. *
  531. * @param string $type Type name
  532. *
  533. * @return Type
  534. */
  535. public function type($type = null)
  536. {
  537. return new Type($type);
  538. }
  539. /**
  540. * type query.
  541. *
  542. * @see https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-percolate-query.html
  543. *
  544. * @return Percolate
  545. */
  546. public function percolate()
  547. {
  548. return new Percolate();
  549. }
  550. }