Common.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Elastica\Query;
  3. /**
  4. * Class Common.
  5. *
  6. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html
  7. */
  8. class Common extends AbstractQuery
  9. {
  10. const OPERATOR_AND = 'and';
  11. const OPERATOR_OR = 'or';
  12. /**
  13. * @var string
  14. */
  15. protected $_field;
  16. /**
  17. * @var array
  18. */
  19. protected $_queryParams = [];
  20. /**
  21. * @param string $field the field on which to query
  22. * @param string $query the query string
  23. * @param float $cutoffFrequency percentage in decimal form (.001 == 0.1%)
  24. */
  25. public function __construct($field, $query, $cutoffFrequency)
  26. {
  27. $this->setField($field);
  28. $this->setQuery($query);
  29. $this->setCutoffFrequency($cutoffFrequency);
  30. }
  31. /**
  32. * Set the field on which to query.
  33. *
  34. * @param string $field the field on which to query
  35. *
  36. * @return $this
  37. */
  38. public function setField($field)
  39. {
  40. $this->_field = $field;
  41. return $this;
  42. }
  43. /**
  44. * Set the query string for this query.
  45. *
  46. * @param string $query
  47. *
  48. * @return $this
  49. */
  50. public function setQuery($query)
  51. {
  52. return $this->setQueryParam('query', $query);
  53. }
  54. /**
  55. * Set the frequency below which terms will be put in the low frequency group.
  56. *
  57. * @param float $frequency percentage in decimal form (.001 == 0.1%)
  58. *
  59. * @return $this
  60. */
  61. public function setCutoffFrequency($frequency)
  62. {
  63. return $this->setQueryParam('cutoff_frequency', (float) $frequency);
  64. }
  65. /**
  66. * Set the logic operator for low frequency terms.
  67. *
  68. * @param string $operator see OPERATOR_* class constants for options
  69. *
  70. * @return $this
  71. */
  72. public function setLowFrequencyOperator($operator)
  73. {
  74. return $this->setQueryParam('low_freq_operator', $operator);
  75. }
  76. /**
  77. * Set the logic operator for high frequency terms.
  78. *
  79. * @param string $operator see OPERATOR_* class constants for options
  80. *
  81. * @return $this
  82. */
  83. public function setHighFrequencyOperator($operator)
  84. {
  85. return $this->setQueryParam('high_frequency_operator', $operator);
  86. }
  87. /**
  88. * Set the minimum_should_match parameter.
  89. *
  90. * @param int|string $minimum minimum number of low frequency terms which must be present
  91. *
  92. * @return $this
  93. *
  94. * @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
  95. */
  96. public function setMinimumShouldMatch($minimum)
  97. {
  98. return $this->setQueryParam('minimum_should_match', $minimum);
  99. }
  100. /**
  101. * Set the boost for this query.
  102. *
  103. * @param float $boost
  104. *
  105. * @return $this
  106. */
  107. public function setBoost($boost)
  108. {
  109. return $this->setQueryParam('boost', (float) $boost);
  110. }
  111. /**
  112. * Set the analyzer for this query.
  113. *
  114. * @param string $analyzer
  115. *
  116. * @return $this
  117. */
  118. public function setAnalyzer($analyzer)
  119. {
  120. return $this->setQueryParam('analyzer', $analyzer);
  121. }
  122. /**
  123. * Set a parameter in the body of this query.
  124. *
  125. * @param string $key parameter key
  126. * @param mixed $value parameter value
  127. *
  128. * @return $this
  129. */
  130. public function setQueryParam($key, $value)
  131. {
  132. $this->_queryParams[$key] = $value;
  133. return $this;
  134. }
  135. /**
  136. * @return array
  137. */
  138. public function toArray()
  139. {
  140. $this->setParam($this->_field, $this->_queryParams);
  141. return parent::toArray();
  142. }
  143. }