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