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
|
|
View Code Duplication |
public function testHistogramAggregation(): void |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$agg = new Histogram('hist', 'price'); |
21
|
|
|
$agg->setFixedInterval(10); |
22
|
|
|
$agg->setMinimumDocumentCount(0); // should return empty buckets |
23
|
|
|
|
24
|
|
|
$query = new Query(); |
25
|
|
|
$query->addAggregation($agg); |
26
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('hist'); |
27
|
|
|
|
28
|
|
|
$buckets = $results['buckets']; |
29
|
|
|
$this->assertCount(5, $buckets); |
30
|
|
|
$this->assertEquals(30, $buckets[3]['key']); |
31
|
|
|
$this->assertEquals(2, $buckets[3]['doc_count']); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @group unit |
36
|
|
|
* @group legacy |
37
|
|
|
*/ |
38
|
|
|
public function testHistogramAggregationWithIntervalTriggersADeprecation(): void |
39
|
|
|
{ |
40
|
|
|
$this->expectDeprecation('Since ruflin/elastica 7.1.0: Argument 3 passed to "__construct()" is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.'); |
|
|
|
|
41
|
|
|
new Histogram('hist', 'price', 10); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @group unit |
46
|
|
|
* @group legacy |
47
|
|
|
*/ |
48
|
|
|
public function testHistogramAggregationSetIntervalTriggersADeprecation(): void |
49
|
|
|
{ |
50
|
|
|
$agg = new Histogram('hist', 'price'); |
51
|
|
|
|
52
|
|
|
$this->expectDeprecation('Since ruflin/elastica 7.1.0: The "setInterval()" method is deprecated, use "setDateInterval()" or "setCalendarInterval()" instead. It will be removed in 8.0.'); |
|
|
|
|
53
|
|
|
|
54
|
|
|
$agg->setInterval(10); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function _getIndexForTest(): Index |
58
|
|
|
{ |
59
|
|
|
$index = $this->_createIndex(); |
60
|
|
|
|
61
|
|
|
$index->addDocuments([ |
62
|
|
|
new Document(1, ['price' => 5, 'color' => 'blue']), |
63
|
|
|
new Document(2, ['price' => 8, 'color' => 'blue']), |
64
|
|
|
new Document(3, ['price' => 1, 'color' => 'red']), |
65
|
|
|
new Document(4, ['price' => 30, 'color' => 'green']), |
66
|
|
|
new Document(5, ['price' => 40, 'color' => 'red']), |
67
|
|
|
new Document(6, ['price' => 35, 'color' => 'green']), |
68
|
|
|
new Document(7, ['price' => 42, 'color' => 'red']), |
69
|
|
|
new Document(8, ['price' => 41, 'color' => 'blue']), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$index->refresh(); |
73
|
|
|
|
74
|
|
|
return $index; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.