Query.Class.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. * Handle the query sub dsl
  13. *
  14. * @author Raymond Julin <raymond.julin@gmail.com>
  15. * @package ElasticSearchClient
  16. * @since 0.1
  17. * Created: 2010-07-24
  18. */
  19. class Query {
  20. protected $term = null;
  21. /**
  22. * @var RangeQuery
  23. */
  24. protected $range;
  25. protected $prefix = null;
  26. protected $wildcard = null;
  27. protected $matchAll = null;
  28. protected $queryString = null;
  29. protected $bool = null;
  30. protected $disMax = null;
  31. protected $constantScore = null;
  32. protected $filteredQuery = null;
  33. public function __construct(array $options=array()) {
  34. }
  35. /**
  36. * Add a term to this query
  37. *
  38. * @return \Mall\Framework\SearchClient\DSL\Query
  39. * @param string $term
  40. * @param bool|string $field
  41. */
  42. public function term($term, $field=false) {
  43. $this->term = ($field)
  44. ? array($field => $term)
  45. : $term;
  46. return $this;
  47. }
  48. /**
  49. * Add a wildcard to this query
  50. *
  51. * @return \Mall\Framework\SearchClient\DSL\Query
  52. * @param $val
  53. * @param bool|string $field
  54. */
  55. public function wildcard($val, $field=false) {
  56. $this->wildcard = ($field)
  57. ? array($field => $val)
  58. : $val;
  59. return $this;
  60. }
  61. /**
  62. * Add a range query
  63. *
  64. * @return \Mall\Framework\SearchClient\DSL\RangeQuery
  65. * @param array $options
  66. */
  67. public function range(array $options=array()) {
  68. $this->range = new RangeQuery($options);
  69. return $this->range;
  70. }
  71. /**
  72. * Build the DSL as array
  73. *
  74. * @return array
  75. */
  76. public function build() {
  77. $built = array();
  78. if ($this->term)
  79. $built['term'] = $this->term;
  80. elseif ($this->range)
  81. $built['range'] = $this->range->build();
  82. elseif ($this->wildcard)
  83. $built['wildcard'] = $this->wildcard;
  84. return $built;
  85. }
  86. }