Builder.Class.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Helper stuff for working with the ElasticSearch DSL
  13. * How to build a mildly complex query:
  14. * $dsl = new ElasticSearchDSL;
  15. * $bool = $dsl->bool(); // Return a new bool structure
  16. *
  17. * @author Raymond Julin <raymond.julin@gmail.com>
  18. * @package ElasticSearchClient
  19. * @since 0.1
  20. * Created: 2010-07-23
  21. */
  22. class Builder {
  23. protected $dsl = array();
  24. private $explain = null;
  25. private $from = null;
  26. private $size = null;
  27. private $fields = null;
  28. private $query = null;
  29. private $facets = null;
  30. private $sort = null;
  31. /**
  32. * Construct DSL object
  33. *
  34. * @return \Mall\Framework\SearchClient\DSL\Builder
  35. * @param array $options
  36. */
  37. public function __construct(array $options=array()) {
  38. foreach ($options as $key => $value)
  39. $this->$key = $value;
  40. }
  41. /**
  42. * Add array clause, can only be one
  43. *
  44. * @return \Mall\Framework\SearchClient\DSL\Query
  45. * @param array $options
  46. */
  47. public function query(array $options=array()) {
  48. if (!($this->query instanceof Query))
  49. $this->query = new Query($options);
  50. return $this->query;
  51. }
  52. /**
  53. * Build the DSL as array
  54. *
  55. * @throws \ElasticSearch\Exception
  56. * @return array
  57. */
  58. public function build() {
  59. $built = array();
  60. if ($this->from != null)
  61. $built['from'] = $this->from;
  62. if ($this->size != null)
  63. $built['size'] = $this->size;
  64. if ($this->sort && is_array($this->sort))
  65. $built['sort'] = $this->sort;
  66. if (!$this->query)
  67. throw new \ElasticSearch\Exception("Query must be specified");
  68. else
  69. $built['query'] = $this->query->build();
  70. return $built;
  71. }
  72. }