These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Elastica\Test\Aggregation; |
||
4 | |||
5 | use Elastica\Aggregation\Terms; |
||
6 | use Elastica\Document; |
||
7 | use Elastica\Index; |
||
8 | use Elastica\Mapping; |
||
9 | use Elastica\Query; |
||
10 | |||
11 | /** |
||
12 | * @group functional |
||
13 | * |
||
14 | * @internal |
||
15 | */ |
||
16 | class TermsTest extends BaseAggregationTest |
||
17 | { |
||
18 | /** |
||
19 | * @group unit |
||
20 | */ |
||
21 | public function testIncludePattern(): void |
||
22 | { |
||
23 | $agg = new Terms('terms'); |
||
24 | $agg->setInclude('pattern*'); |
||
25 | |||
26 | $this->assertSame($agg->getParam('include'), 'pattern*'); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @group unit |
||
31 | */ |
||
32 | View Code Duplication | public function testIncludeExactMatch(): void |
|
0 ignored issues
–
show
|
|||
33 | { |
||
34 | $agg = new Terms('terms'); |
||
35 | $agg->setIncludeAsExactMatch(['first', 'second']); |
||
36 | |||
37 | $this->assertSame($agg->getParam('include'), ['first', 'second']); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @group unit |
||
42 | */ |
||
43 | View Code Duplication | public function testIncludeWithPartitions(): void |
|
44 | { |
||
45 | $agg = new Terms('terms'); |
||
46 | $agg->setIncludeWithPartitions(1, 23); |
||
47 | |||
48 | $this->assertSame($agg->getParam('include'), [ |
||
49 | 'partition' => 1, |
||
50 | 'num_partitions' => 23, |
||
51 | ]); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @group unit |
||
56 | */ |
||
57 | public function testExcludePattern(): void |
||
58 | { |
||
59 | $agg = new Terms('terms'); |
||
60 | $agg->setExclude('pattern*'); |
||
61 | |||
62 | $this->assertSame($agg->getParam('exclude'), 'pattern*'); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @group unit |
||
67 | */ |
||
68 | View Code Duplication | public function testExcludeExactMatch(): void |
|
69 | { |
||
70 | $agg = new Terms('terms'); |
||
71 | $agg->setExcludeAsExactMatch(['first', 'second']); |
||
72 | |||
73 | $this->assertSame($agg->getParam('exclude'), ['first', 'second']); |
||
74 | } |
||
75 | |||
76 | View Code Duplication | public function testTermsAggregation(): void |
|
77 | { |
||
78 | $agg = new Terms('terms'); |
||
79 | $agg->setField('color'); |
||
80 | |||
81 | $query = new Query(); |
||
82 | $query->addAggregation($agg); |
||
83 | $results = $this->getIndex()->search($query)->getAggregation('terms'); |
||
84 | |||
85 | $this->assertEquals(2, $results['buckets'][0]['doc_count']); |
||
86 | $this->assertEquals('blue', $results['buckets'][0]['key']); |
||
87 | } |
||
88 | |||
89 | View Code Duplication | public function testTermsSetOrder(): void |
|
90 | { |
||
91 | $agg = new Terms('terms'); |
||
92 | $agg->setField('color'); |
||
93 | $agg->setOrder('_count', 'asc'); |
||
94 | |||
95 | $query = new Query(); |
||
96 | $query->addAggregation($agg); |
||
97 | $results = $this->getIndex()->search($query)->getAggregation('terms'); |
||
98 | |||
99 | $this->assertEquals('blue', $results['buckets'][2]['key']); |
||
100 | } |
||
101 | |||
102 | public function testTermsSetOrders(): void |
||
103 | { |
||
104 | $agg = new Terms('terms'); |
||
105 | $agg->setField('color'); |
||
106 | $agg->setOrders([ |
||
107 | ['_count' => 'asc'], // 1. red, 2. green, 3. blue |
||
108 | ['_key' => 'asc'], // 1. green, 2. red, 3. blue |
||
109 | ]); |
||
110 | |||
111 | $query = new Query(); |
||
112 | $query->addAggregation($agg); |
||
113 | $results = $this->getIndex()->search($query)->getAggregation('terms'); |
||
114 | |||
115 | $this->assertSame('green', $results['buckets'][0]['key']); |
||
116 | $this->assertSame('red', $results['buckets'][1]['key']); |
||
117 | $this->assertSame('blue', $results['buckets'][2]['key']); |
||
118 | } |
||
119 | |||
120 | View Code Duplication | private function getIndex(): Index |
|
121 | { |
||
122 | $index = $this->_createIndex(); |
||
123 | |||
124 | $mapping = new Mapping([ |
||
125 | 'color' => ['type' => 'keyword'], |
||
126 | ]); |
||
127 | $index->setMapping($mapping); |
||
128 | |||
129 | $index->addDocuments([ |
||
130 | new Document(1, ['color' => 'blue']), |
||
131 | new Document(2, ['color' => 'blue']), |
||
132 | new Document(3, ['color' => 'red']), |
||
133 | new Document(4, ['color' => 'green']), |
||
134 | ]); |
||
135 | |||
136 | $index->refresh(); |
||
137 | |||
138 | return $index; |
||
139 | } |
||
140 | } |
||
141 |
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.