1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\Histogram; |
6
|
|
|
use Elastica\Document; |
7
|
|
|
use Elastica\Index; |
8
|
|
|
use Elastica\Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
class HistogramTest extends BaseAggregationTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @group functional |
17
|
|
|
*/ |
18
|
|
|
public function testHistogramAggregation(): void |
19
|
|
|
{ |
20
|
|
|
$agg = new Histogram('hist', 'price', 10); |
21
|
|
|
$agg->setMinimumDocumentCount(0); // should return empty buckets |
22
|
|
|
|
23
|
|
|
$query = new Query(); |
24
|
|
|
$query->addAggregation($agg); |
25
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
26
|
|
|
|
27
|
|
|
$buckets = $results['buckets']; |
28
|
|
|
$this->assertCount(5, $buckets); |
29
|
|
|
$this->assertEquals(30, $buckets[3]['key']); |
30
|
|
|
$this->assertEquals(2, $buckets[3]['doc_count']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @group functional |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function testHistogramKeyedAggregation(): void |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$agg = new Histogram('hist', 'price', 10); |
39
|
|
|
$agg->setMinimumDocumentCount(0); // should return empty buckets |
40
|
|
|
$agg->setKeyed(); |
41
|
|
|
|
42
|
|
|
$query = new Query(); |
43
|
|
|
$query->addAggregation($agg); |
44
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
45
|
|
|
|
46
|
|
|
$expected = [ |
47
|
|
|
'0.0', |
48
|
|
|
'10.0', |
49
|
|
|
'20.0', |
50
|
|
|
'30.0', |
51
|
|
|
'40.0', |
52
|
|
|
]; |
53
|
|
|
$this->assertSame($expected, \array_keys($results['buckets'])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function _getIndexForTest(): Index |
57
|
|
|
{ |
58
|
|
|
$index = $this->_createIndex(); |
59
|
|
|
|
60
|
|
|
$index->addDocuments([ |
61
|
|
|
new Document(1, ['price' => 5, 'color' => 'blue']), |
62
|
|
|
new Document(2, ['price' => 8, 'color' => 'blue']), |
63
|
|
|
new Document(3, ['price' => 1, 'color' => 'red']), |
64
|
|
|
new Document(4, ['price' => 30, 'color' => 'green']), |
65
|
|
|
new Document(5, ['price' => 40, 'color' => 'red']), |
66
|
|
|
new Document(6, ['price' => 35, 'color' => 'green']), |
67
|
|
|
new Document(7, ['price' => 42, 'color' => 'red']), |
68
|
|
|
new Document(8, ['price' => 41, 'color' => 'blue']), |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$index->refresh(); |
72
|
|
|
|
73
|
|
|
return $index; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.