BoolQuery.php 2.6 KB

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