BucketScript.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Elastica\Aggregation;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Class BucketScript.
  6. *
  7. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html
  8. */
  9. class BucketScript extends AbstractAggregation
  10. {
  11. /**
  12. * @param string $name
  13. * @param array|null $bucketsPath
  14. * @param string|null $script
  15. */
  16. public function __construct($name, $bucketsPath = null, $script = null)
  17. {
  18. parent::__construct($name);
  19. if (null !== $bucketsPath) {
  20. $this->setBucketsPath($bucketsPath);
  21. }
  22. if (null !== $script) {
  23. $this->setScript($script);
  24. }
  25. }
  26. /**
  27. * Set the buckets_path for this aggregation.
  28. *
  29. * @param array $bucketsPath
  30. *
  31. * @return $this
  32. */
  33. public function setBucketsPath($bucketsPath)
  34. {
  35. return $this->setParam('buckets_path', $bucketsPath);
  36. }
  37. /**
  38. * Set the script for this aggregation.
  39. *
  40. * @param string $script
  41. *
  42. * @return $this
  43. */
  44. public function setScript($script)
  45. {
  46. return $this->setParam('script', $script);
  47. }
  48. /**
  49. * Set the gap policy for this aggregation.
  50. *
  51. * @param string $gapPolicy
  52. *
  53. * @return $this
  54. */
  55. public function setGapPolicy($gapPolicy)
  56. {
  57. return $this->setParam('gap_policy', $gapPolicy);
  58. }
  59. /**
  60. * Set the format for this aggregation.
  61. *
  62. * @param string $format
  63. *
  64. * @return $this
  65. */
  66. public function setFormat($format)
  67. {
  68. return $this->setParam('format', $format);
  69. }
  70. /**
  71. * @throws InvalidException If buckets path or script is not set
  72. *
  73. * @return array
  74. */
  75. public function toArray()
  76. {
  77. if (!$this->hasParam('buckets_path')) {
  78. throw new InvalidException('Buckets path is required');
  79. } elseif (!$this->hasParam('script')) {
  80. throw new InvalidException('Script parameter is required');
  81. }
  82. return parent::toArray();
  83. }
  84. }