PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Elastica/Query/FunctionScore.php

http://github.com/ruflin/Elastica
PHP | 370 lines | 210 code | 43 blank | 117 comment | 23 complexity | 12f9a42863b0c6feb68103a8c2aff229 MD5 | raw file
  1. <?php
  2. namespace Elastica\Query;
  3. use Elastica\Exception\DeprecatedException;
  4. use Elastica\Exception\InvalidException;
  5. use Elastica\Filter\AbstractFilter;
  6. use Elastica\Script\AbstractScript;
  7. /**
  8. * Class FunctionScore.
  9. *
  10. * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
  11. */
  12. class FunctionScore extends AbstractQuery
  13. {
  14. const BOOST_MODE_MULTIPLY = 'multiply';
  15. const BOOST_MODE_REPLACE = 'replace';
  16. const BOOST_MODE_SUM = 'sum';
  17. const BOOST_MODE_AVERAGE = 'avg';
  18. const BOOST_MODE_MAX = 'max';
  19. const BOOST_MODE_MIN = 'min';
  20. const SCORE_MODE_MULTIPLY = 'multiply';
  21. const SCORE_MODE_SUM = 'sum';
  22. const SCORE_MODE_AVERAGE = 'avg';
  23. const SCORE_MODE_FIRST = 'first';
  24. const SCORE_MODE_MAX = 'max';
  25. const SCORE_MODE_MIN = 'min';
  26. const DECAY_GAUSS = 'gauss';
  27. const DECAY_EXPONENTIAL = 'exp';
  28. const DECAY_LINEAR = 'linear';
  29. const FIELD_VALUE_FACTOR_MODIFIER_NONE = 'none';
  30. const FIELD_VALUE_FACTOR_MODIFIER_LOG = 'log';
  31. const FIELD_VALUE_FACTOR_MODIFIER_LOG1P = 'log1p';
  32. const FIELD_VALUE_FACTOR_MODIFIER_LOG2P = 'log2p';
  33. const FIELD_VALUE_FACTOR_MODIFIER_LN = 'ln';
  34. const FIELD_VALUE_FACTOR_MODIFIER_LN1P = 'ln1p';
  35. const FIELD_VALUE_FACTOR_MODIFIER_LN2P = 'ln2p';
  36. const FIELD_VALUE_FACTOR_MODIFIER_SQUARE = 'square';
  37. const FIELD_VALUE_FACTOR_MODIFIER_SQRT = 'sqrt';
  38. const FIELD_VALUE_FACTOR_MODIFIER_RECIPROCAL = 'reciprocal';
  39. const MULTI_VALUE_MODE_MIN = 'min';
  40. const MULTI_VALUE_MODE_MAX = 'max';
  41. const MULTI_VALUE_MODE_AVG = 'avg';
  42. const MULTI_VALUE_MODE_SUM = 'sum';
  43. protected $_functions = [];
  44. /**
  45. * Set the child query for this function_score query.
  46. *
  47. * @param AbstractQuery $query
  48. *
  49. * @return $this
  50. */
  51. public function setQuery(AbstractQuery $query)
  52. {
  53. return $this->setParam('query', $query);
  54. }
  55. /**
  56. * @param AbstractFilter $filter
  57. *
  58. * @deprecated Elastica\Query\FunctionScore::setFilter is deprecated. Use setQuery instead
  59. *
  60. * @return $this
  61. */
  62. public function setFilter(AbstractFilter $filter)
  63. {
  64. throw new DeprecatedException('Deprecated: Elastica\Query\FunctionScore::setFilter is deprecated. Use setQuery instead.');
  65. }
  66. /**
  67. * Add a function to the function_score query.
  68. *
  69. * @param string $functionType valid values are DECAY_* constants and script_score
  70. * @param array|float $functionParams the body of the function. See documentation for proper syntax.
  71. * @param AbstractQuery $filter optional filter to apply to the function
  72. * @param float $weight function weight
  73. *
  74. * @return $this
  75. */
  76. public function addFunction($functionType, $functionParams, $filter = null, $weight = null)
  77. {
  78. $function = [
  79. $functionType => $functionParams,
  80. ];
  81. if (!is_null($filter)) {
  82. if ($filter instanceof AbstractFilter) {
  83. trigger_error('Deprecated: Elastica\Query\FunctionScore::addFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  84. } elseif (!($filter instanceof AbstractQuery)) {
  85. throw new InvalidException('Filter must be instance of AbstractQuery');
  86. }
  87. $function['filter'] = $filter;
  88. }
  89. if ($weight !== null) {
  90. $function['weight'] = $weight;
  91. }
  92. $this->_functions[] = $function;
  93. return $this;
  94. }
  95. /**
  96. * Add a script_score function to the query.
  97. *
  98. * @param \Elastica\Script\AbstractScript $script a Script object
  99. * @param AbstractQuery $filter an optional filter to apply to the function
  100. * @param float $weight the weight of the function
  101. *
  102. * @return $this
  103. */
  104. public function addScriptScoreFunction(AbstractScript $script, $filter = null, $weight = null)
  105. {
  106. if (null !== $filter) {
  107. if ($filter instanceof AbstractFilter) {
  108. trigger_error('Deprecated: Elastica\Query\FunctionScore::addScriptScoreFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  109. } elseif (!($filter instanceof AbstractQuery)) {
  110. throw new InvalidException('Filter must be instance of AbstractQuery');
  111. }
  112. }
  113. return $this->addFunction('script_score', $script, $filter, $weight);
  114. }
  115. /**
  116. * Add a decay function to the query.
  117. *
  118. * @param string $function see DECAY_* constants for valid options
  119. * @param string $field the document field on which to perform the decay function
  120. * @param string $origin the origin value for this decay function
  121. * @param string $scale a scale to define the rate of decay for this function
  122. * @param string $offset If defined, this function will only be computed for documents with a distance from the origin greater than this value
  123. * @param float $decay optionally defines how documents are scored at the distance given by the $scale parameter
  124. * @param float $weight optional factor by which to multiply the score at the value provided by the $scale parameter
  125. * @param AbstractQuery $filter a filter associated with this function
  126. * @param string $multiValueMode see MULTI_VALUE_MODE_* constants for valid options
  127. *
  128. * @return $this
  129. */
  130. public function addDecayFunction(
  131. $function,
  132. $field,
  133. $origin,
  134. $scale,
  135. $offset = null,
  136. $decay = null,
  137. $weight = null,
  138. $filter = null,
  139. $multiValueMode = null
  140. ) {
  141. if (null !== $filter) {
  142. if ($filter instanceof AbstractFilter) {
  143. trigger_error('Deprecated: Elastica\Query\FunctionScore::addDecayFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  144. } elseif (!($filter instanceof AbstractQuery)) {
  145. throw new InvalidException('Filter must be instance of AbstractQuery');
  146. }
  147. }
  148. $functionParams = [
  149. $field => [
  150. 'origin' => $origin,
  151. 'scale' => $scale,
  152. ],
  153. ];
  154. if (!is_null($offset)) {
  155. $functionParams[$field]['offset'] = $offset;
  156. }
  157. if (!is_null($decay)) {
  158. $functionParams[$field]['decay'] = (float) $decay;
  159. }
  160. if (null !== $multiValueMode) {
  161. $functionParams['multi_value_mode'] = $multiValueMode;
  162. }
  163. return $this->addFunction($function, $functionParams, $filter, $weight);
  164. }
  165. public function addFieldValueFactorFunction(
  166. $field,
  167. $factor = null,
  168. $modifier = null,
  169. $missing = null,
  170. $weight = null,
  171. $filter = null
  172. ) {
  173. if (null !== $filter) {
  174. if ($filter instanceof AbstractFilter) {
  175. trigger_error('Deprecated: Elastica\Query\FunctionScore::addFieldValueFactorFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  176. } elseif (!($filter instanceof AbstractQuery)) {
  177. throw new InvalidException('Filter must be instance of AbstractQuery');
  178. }
  179. }
  180. $functionParams = [
  181. 'field' => $field,
  182. ];
  183. if (!is_null($factor)) {
  184. $functionParams['factor'] = $factor;
  185. }
  186. if (!is_null($modifier)) {
  187. $functionParams['modifier'] = $modifier;
  188. }
  189. if (!is_null($missing)) {
  190. $functionParams['missing'] = $missing;
  191. }
  192. return $this->addFunction('field_value_factor', $functionParams, $filter, $weight);
  193. }
  194. /**
  195. * Add a boost_factor function to the query.
  196. *
  197. * @param float $boostFactor the boost factor value
  198. * @param AbstractQuery $filter a filter associated with this function
  199. *
  200. * @deprecated Use addWeightFunction instead. This method will be removed in further Elastica releases
  201. */
  202. public function addBoostFactorFunction($boostFactor, $filter = null)
  203. {
  204. if (null !== $filter) {
  205. if ($filter instanceof AbstractFilter) {
  206. trigger_error('Deprecated: Elastica\Query\FunctionScore::addBoostFactorFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  207. } elseif (!($filter instanceof AbstractQuery)) {
  208. throw new InvalidException('Filter must be instance of AbstractQuery');
  209. }
  210. }
  211. trigger_error('Query\FunctionScore::addBoostFactorFunction is deprecated. Use addWeightFunction instead. This method will be removed in further Elastica releases', E_USER_DEPRECATED);
  212. $this->addWeightFunction($boostFactor, $filter);
  213. }
  214. /**
  215. * @param float $weight the weight of the function
  216. * @param AbstractQuery $filter a filter associated with this function
  217. */
  218. public function addWeightFunction($weight, $filter = null)
  219. {
  220. if (null !== $filter) {
  221. if ($filter instanceof AbstractFilter) {
  222. trigger_error('Deprecated: Elastica\Query\FunctionScore::addWeightFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  223. } elseif (!($filter instanceof AbstractQuery)) {
  224. throw new InvalidException('Filter must be instance of AbstractQuery');
  225. }
  226. }
  227. $this->addFunction('weight', $weight, $filter);
  228. }
  229. /**
  230. * Add a random_score function to the query.
  231. *
  232. * @param number $seed the seed value
  233. * @param AbstractQuery $filter a filter associated with this function
  234. * @param float $weight an optional boost value associated with this function
  235. */
  236. public function addRandomScoreFunction($seed, $filter = null, $weight = null)
  237. {
  238. if (null !== $filter) {
  239. if ($filter instanceof AbstractFilter) {
  240. trigger_error('Deprecated: Elastica\Query\FunctionScore::addRandomScoreFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.', E_USER_DEPRECATED);
  241. } elseif (!($filter instanceof AbstractQuery)) {
  242. throw new InvalidException('Filter must be instance of AbstractQuery');
  243. }
  244. }
  245. $this->addFunction('random_score', ['seed' => $seed], $filter, $weight);
  246. }
  247. /**
  248. * Set an overall boost value for this query.
  249. *
  250. * @param float $boost
  251. *
  252. * @return $this
  253. */
  254. public function setBoost($boost)
  255. {
  256. return $this->setParam('boost', (float) $boost);
  257. }
  258. /**
  259. * Restrict the combined boost of the function_score query and its child query.
  260. *
  261. * @param float $maxBoost
  262. *
  263. * @return $this
  264. */
  265. public function setMaxBoost($maxBoost)
  266. {
  267. return $this->setParam('max_boost', (float) $maxBoost);
  268. }
  269. /**
  270. * The boost mode determines how the score of this query is combined with that of the child query.
  271. *
  272. * @param string $mode see BOOST_MODE_* constants for valid options. Default is multiply.
  273. *
  274. * @return $this
  275. */
  276. public function setBoostMode($mode)
  277. {
  278. return $this->setParam('boost_mode', $mode);
  279. }
  280. /**
  281. * If set, this query will return results in random order.
  282. *
  283. * @param int $seed Set a seed value to return results in the same random order for consistent pagination.
  284. *
  285. * @return $this
  286. */
  287. public function setRandomScore($seed = null)
  288. {
  289. $seedParam = new \stdClass();
  290. if (!is_null($seed)) {
  291. $seedParam->seed = $seed;
  292. }
  293. return $this->setParam('random_score', $seedParam);
  294. }
  295. /**
  296. * Set the score method.
  297. *
  298. * @param string $mode see SCORE_MODE_* constants for valid options. Default is multiply.
  299. *
  300. * @return $this
  301. */
  302. public function setScoreMode($mode)
  303. {
  304. return $this->setParam('score_mode', $mode);
  305. }
  306. /**
  307. * Set min_score option.
  308. *
  309. * @param float $minScore
  310. *
  311. * @return $this
  312. */
  313. public function setMinScore($minScore)
  314. {
  315. return $this->setParam('min_score', (float) $minScore);
  316. }
  317. /**
  318. * @return array
  319. */
  320. public function toArray()
  321. {
  322. if (sizeof($this->_functions)) {
  323. $this->setParam('functions', $this->_functions);
  324. }
  325. return parent::toArray();
  326. }
  327. }