/
ExtendedStatsBucketTest.php
85 lines (69 loc) · 2.57 KB
/
ExtendedStatsBucketTest.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
<?php
namespace Elastica\Test\Aggregation;
use Elastica\Aggregation\ExtendedStatsBucket;
use Elastica\Aggregation\Histogram;
use Elastica\Aggregation\Max;
use Elastica\Document;
use Elastica\Index;
use Elastica\Query;
/**
* @internal
*/
class ExtendedStatsBucketTest extends BaseAggregationTest
{
/**
* @group functional
*/
public function testExtendedStatBucketAggregation(): void
{
$bucketScriptAggregation = new ExtendedStatsBucket('result', 'age_groups>max_weight');
$histogramAggregation = new Histogram('age_groups', 'age');
$histogramAggregation->setFixedInterval(10);
$histogramAggregation->addAggregation((new Max('max_weight'))->setField('weight'));
$query = Query::create([])
->addAggregation($histogramAggregation)
->addAggregation($bucketScriptAggregation)
;
$results = $this->_getIndexForTest()->search($query)->getAggregation('result');
$this->assertEquals(3, $results['count']);
$this->assertEquals(50, $results['min']);
$this->assertEquals(70, $results['max']);
$this->assertEquals(60, $results['avg']);
$this->assertEquals(180, $results['sum']);
$this->assertEquals(11000, $results['sum_of_squares']);
$this->assertEquals(66.66666666666667, $results['variance']);
$this->assertEquals(8.16496580927726, $results['std_deviation']);
$this->assertEquals(['upper' => 76.32993161855453, 'lower' => 43.670068381445475], $results['std_deviation_bounds']);
}
/**
* @group unit
*/
public function testOverrideBucketsPathThroughSetters(): void
{
$serialDiffAgg = new ExtendedStatsBucket('bucket_part', 'foobar');
$serialDiffAgg
->setBucketsPath('age_groups>max_weight')
->setFormat('test_format')
->setGapPolicy(10)
;
$expected = [
'extended_stats_bucket' => [
'buckets_path' => 'age_groups>max_weight',
'format' => 'test_format',
'gap_policy' => 10,
],
];
$this->assertEquals($expected, $serialDiffAgg->toArray());
}
private function _getIndexForTest(): Index
{
$index = $this->_createIndex();
$index->addDocuments([
Document::create(['weight' => 60, 'height' => 180, 'age' => 25]),
Document::create(['weight' => 70, 'height' => 156, 'age' => 32]),
Document::create(['weight' => 50, 'height' => 155, 'age' => 45]),
]);
$index->refresh();
return $index;
}
}