RangeQuery.Class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php // vim:set ts=4 sw=4 et:
  2. namespace Mall\Framework\SearchClient\DSL;
  3. /**
  4. * This file is part of the ElasticSearch PHP client
  5. *
  6. * (c) Raymond Julin <raymond.julin@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Range queries
  13. *
  14. * @author Raymond Julin <raymond.julin@gmail.com>
  15. * @package ElasticSearchClient
  16. * @since 0.1
  17. * Created: 2010-07-24
  18. */
  19. class RangeQuery {
  20. protected $fieldname = null;
  21. protected $from = null;
  22. protected $to = null;
  23. protected $includeLower = null;
  24. protected $includeUpper = null;
  25. protected $boost = null;
  26. /**
  27. * Construct new RangeQuery component
  28. *
  29. * @return \Mall\Framework\SearchClient\DSL\RangeQuery
  30. * @param array $options
  31. */
  32. public function __construct(array $options=array()) {
  33. $this->fieldname = key($options);
  34. $values = current($options);
  35. if (is_array($values)) {
  36. foreach ($values as $key => $val)
  37. $this->$key = $val;
  38. }
  39. }
  40. /**
  41. * Setters
  42. *
  43. * @return \Mall\Framework\SearchClient\DSL\RangeQuery
  44. * @param mixed $value
  45. */
  46. public function fieldname($value) {
  47. $this->fieldname = $value;
  48. return $this;
  49. }
  50. /**
  51. * @param $value
  52. * @return \Mall\Framework\SearchClient\DSL\RangeQuery $this
  53. */
  54. public function from($value) {
  55. $this->from = $value;
  56. return $this;
  57. }
  58. /**
  59. * @param $value
  60. * @return \Mall\Framework\SearchClient\DSL\RangeQuery $this
  61. */
  62. public function to($value) {
  63. $this->to = $value;
  64. return $this;
  65. }
  66. /**
  67. * @param $value
  68. * @return \Mall\Framework\SearchClient\DSL\RangeQuery $this
  69. */
  70. public function includeLower($value) {
  71. $this->includeLower = $value;
  72. return $this;
  73. }
  74. /**
  75. * @param $value
  76. * @return \Mall\Framework\SearchClient\DSL\RangeQuery $this
  77. */
  78. public function includeUpper($value) {
  79. $this->includeUpper = $value;
  80. return $this;
  81. }
  82. /**
  83. * @param $value
  84. * @return \Mall\Framework\SearchClient\DSL\RangeQuery $this
  85. */
  86. public function boost($value) {
  87. $this->boost = $value;
  88. return $this;
  89. }
  90. /**
  91. * Build to array
  92. *
  93. * @throws \Mall\Framework\SearchClient\Exception
  94. * @return array
  95. */
  96. public function build() {
  97. $built = array();
  98. if ($this->fieldname) {
  99. $built[$this->fieldname] = array();
  100. foreach (array("from","to","includeLower","includeUpper", "boost") as $opt) {
  101. if ($this->$opt !== null)
  102. $built[$this->fieldname][$opt] = $this->$opt;
  103. }
  104. if (count($built[$this->fieldname]) == 0)
  105. throw new \ElasticSearch\Exception("Empty RangeQuery cant be created");
  106. }
  107. return $built;
  108. }
  109. }