Completed
Push — master ( 9146c8...39ecb4 )
by Nicolas
04:52
created

testSignificantTermsAggregationWithBackgroundFilterWithLegacyFilter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica\Test\Aggregation;
4
5
use Elastica\Aggregation\SignificantTerms;
6
use Elastica\Document;
7
use Elastica\Index;
8
use Elastica\Mapping;
9
use Elastica\Query;
10
use Elastica\Query\Terms;
11
12
/**
13
 * @internal
14
 */
15
class SignificantTermsTest extends BaseAggregationTest
16
{
17
    /**
18
     * @group functional
19
     */
20
    public function testSignificantTermsAggregation(): void
21
    {
22
        $agg = new SignificantTerms('significantTerms');
23
        $agg->setField('temperature');
24
        $agg->setSize(1);
25
26
        $termsQuery = new Terms('color', ['blue', 'red', 'green', 'yellow', 'white']);
27
28
        $query = new Query($termsQuery);
29
        $query->addAggregation($agg);
30
        $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms');
31
32
        $this->assertCount(1, $results['buckets']);
33
        $this->assertEquals(63, $results['buckets'][0]['doc_count']);
34
        $this->assertEquals(79, $results['buckets'][0]['bg_count']);
35
        $this->assertEquals('1500', $results['buckets'][0]['key']);
36
    }
37
38
    /**
39
     * @group functional
40
     */
41 View Code Duplication
    public function testSignificantTermsAggregationWithBackgroundFilter(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
42
    {
43
        $agg = new SignificantTerms('significantTerms');
44
        $agg->setField('temperature');
45
        $agg->setSize(1);
46
        $termsFilter = new Terms('color', ['blue', 'red', 'green', 'yellow']);
47
        $agg->setBackgroundFilter($termsFilter);
48
49
        $termsQuery = new Terms('color', ['blue', 'red', 'green', 'yellow', 'white']);
50
51
        $query = new Query($termsQuery);
52
        $query->addAggregation($agg);
53
        $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms');
54
55
        $this->assertEquals(15, $results['buckets'][0]['doc_count']);
56
        $this->assertEquals(12, $results['buckets'][0]['bg_count']);
57
        $this->assertEquals('4500', $results['buckets'][0]['key']);
58
    }
59
60
    /**
61
     * @group functional
62
     */
63 View Code Duplication
    public function testSignificantTermsAggregationWithBackgroundFilterWithLegacyFilter(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
64
    {
65
        $agg = new SignificantTerms('significantTerms');
66
        $agg->setField('temperature');
67
        $agg->setSize(1);
68
        $termsFilter = new Terms('color', ['blue', 'red', 'green', 'yellow']);
69
        $agg->setBackgroundFilter($termsFilter);
70
71
        $termsQuery = new Terms('color', ['blue', 'red', 'green', 'yellow', 'white']);
72
73
        $query = new Query($termsQuery);
74
        $query->addAggregation($agg);
75
        $results = $this->_getIndexForTest()->search($query)->getAggregation('significantTerms');
76
77
        $this->assertEquals(15, $results['buckets'][0]['doc_count']);
78
        $this->assertEquals(12, $results['buckets'][0]['bg_count']);
79
        $this->assertEquals('4500', $results['buckets'][0]['key']);
80
    }
81
82
    protected function _getIndexForTest(): Index
83
    {
84
        $index = $this->_createIndex();
85
        $colors = ['blue', 'blue', 'red', 'red', 'green', 'yellow', 'white', 'cyan', 'magenta'];
86
        $temperatures = [1500, 1500, 1500, 1500, 2500, 3500, 4500, 5500, 6500, 7500, 7500, 8500, 9500];
87
88
        $mapping = new Mapping([
89
            'color' => ['type' => 'keyword'],
90
            'temperature' => ['type' => 'keyword'],
91
        ]);
92
        $index->setMapping($mapping);
93
94
        $docs = [];
95
        for ($i = 0; $i < 250; ++$i) {
96
            $docs[] = new Document($i, ['color' => $colors[$i % \count($colors)], 'temperature' => $temperatures[$i % \count($temperatures)]]);
97
        }
98
        $index->addDocuments($docs);
99
        $index->refresh();
100
101
        return $index;
102
    }
103
}
104