PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Elastica/Facet/Range.php

https://github.com/nordsee/Elastica
PHP | 131 lines | 52 code | 13 blank | 66 comment | 6 complexity | 38934bad13ed44e5bbcf11c367216b46 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. namespace Elastica\Facet;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Implements the range facet.
  6. *
  7. * @category Xodoa
  8. * @package Elastica
  9. * @author Jasper van Wanrooy <jasper@vanwanrooy.net>
  10. * @link http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html
  11. */
  12. class Range extends AbstractFacet
  13. {
  14. /**
  15. * Sets the field for the range.
  16. *
  17. * @param string $field The name of the field for range.
  18. * @return \Elastica\Facet\Range
  19. */
  20. public function setField($field)
  21. {
  22. return $this->setParam('field', $field);
  23. }
  24. /**
  25. * Sets the fields by their separate key and value fields.
  26. *
  27. * @param string $keyField The key_field param for the range.
  28. * @param string $valueField The key_value param for the range.
  29. * @return \Elastica\Facet\Range
  30. */
  31. public function setKeyValueFields($keyField, $valueField)
  32. {
  33. return $this->setParam('key_field', $keyField)
  34. ->setParam('value_field', $valueField);
  35. }
  36. /**
  37. * Sets the key and value for this facet by script.
  38. *
  39. * @param string $keyScript Script to check whether it falls into the range.
  40. * @param string $valueScript Script to use for statistical calculations.
  41. *
  42. * @return \Elastica\Facet\Range
  43. */
  44. public function setKeyValueScripts($keyScript, $valueScript)
  45. {
  46. return $this->setParam('key_script', $keyScript)
  47. ->setParam('value_script', $valueScript);
  48. }
  49. /**
  50. * Sets the ranges for the facet all at once. Sample ranges:
  51. * array (
  52. * array('to' => 50),
  53. * array('from' => 20, 'to' 70),
  54. * array('from' => 70, 'to' => 120),
  55. * array('from' => 150)
  56. * )
  57. *
  58. * @param array $ranges Numerical array with range definitions.
  59. * @return \Elastica\Facet\Range
  60. */
  61. public function setRanges(array $ranges)
  62. {
  63. return $this->setParam('ranges', $ranges);
  64. }
  65. /**
  66. * Adds a range to the range facet.
  67. *
  68. * @param mixed $from The from for the range.
  69. * @param mixed $to The to for the range.
  70. * @return \Elastica\Facet\Range
  71. */
  72. public function addRange($from = null, $to = null)
  73. {
  74. if (!isset($this->_params['ranges']) || !is_array($this->_params['ranges'])) {
  75. $this->_params['ranges'] = array();
  76. }
  77. $this->_params['ranges'][] = array('from' => $from, 'to' => $to);
  78. return $this;
  79. }
  80. /**
  81. * Creates the full facet definition, which includes the basic
  82. * facet definition of the parent.
  83. *
  84. * @see \Elastica\Facet\AbstractFacet::toArray()
  85. * @throws \Elastica\Exception\InvalidException When the right fields haven't been set.
  86. * @return array
  87. */
  88. public function toArray()
  89. {
  90. /**
  91. * Check the facet for validity.
  92. * There are three ways to set the key and value field for the range:
  93. * - a single field for both key and value; or
  94. * - separate fields for key and value; or
  95. * - separate scripts for key and value.
  96. */
  97. $fieldTypesSet = 0;
  98. if (isset($this->_params['field'])) {
  99. $fieldTypesSet++;
  100. }
  101. if (isset($this->_params['key_field'])) {
  102. $fieldTypesSet++;
  103. }
  104. if (isset($this->_params['key_script'])) {
  105. $fieldTypesSet++;
  106. }
  107. if ($fieldTypesSet === 0) {
  108. throw new InvalidException('Neither field, key_field nor key_script is set.');
  109. } elseif ($fieldTypesSet > 1) {
  110. throw new InvalidException('Either field, key_field and key_value or key_script and value_script should be set.');
  111. }
  112. /**
  113. * Set the range in the abstract as param.
  114. */
  115. $this->_setFacetParam('range', $this->_params);
  116. return parent::toArray();
  117. }
  118. }