1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Aggregation; |
4
|
|
|
|
5
|
|
|
use Elastica\Aggregation\Nested; |
6
|
|
|
use Elastica\Aggregation\ReverseNested; |
7
|
|
|
use Elastica\Aggregation\Terms; |
8
|
|
|
use Elastica\Document; |
9
|
|
|
use Elastica\Index; |
10
|
|
|
use Elastica\Mapping; |
11
|
|
|
use Elastica\Query; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class ReverseNestedTest extends BaseAggregationTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @group unit |
20
|
|
|
*/ |
21
|
|
|
public function testPathNotSetIfNull(): void |
22
|
|
|
{ |
23
|
|
|
$agg = new ReverseNested('nested'); |
24
|
|
|
$this->assertFalse($agg->hasParam('path')); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @group unit |
29
|
|
|
*/ |
30
|
|
|
public function testPathSetIfNotNull(): void |
31
|
|
|
{ |
32
|
|
|
$agg = new ReverseNested('nested', 'some_field'); |
33
|
|
|
$this->assertEquals('some_field', $agg->getParam('path')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @group functional |
38
|
|
|
*/ |
39
|
|
|
public function testReverseNestedAggregation(): void |
40
|
|
|
{ |
41
|
|
|
$agg = new Nested('comments', 'comments'); |
42
|
|
|
$names = new Terms('name'); |
43
|
|
|
$names->setField('comments.name'); |
44
|
|
|
|
45
|
|
|
$tags = new Terms('tags'); |
46
|
|
|
$tags->setField('tags'); |
47
|
|
|
|
48
|
|
|
$reverseNested = new ReverseNested('main'); |
49
|
|
|
$reverseNested->addAggregation($tags); |
50
|
|
|
|
51
|
|
|
$names->addAggregation($reverseNested); |
52
|
|
|
|
53
|
|
|
$agg->addAggregation($names); |
54
|
|
|
|
55
|
|
|
$query = new Query(); |
56
|
|
|
$query->addAggregation($agg); |
57
|
|
|
$results = $this->_getIndexForTest()->search($query)->getAggregation('comments'); |
58
|
|
|
|
59
|
|
|
$this->assertArrayHasKey('name', $results); |
60
|
|
|
$nameResults = $results['name']; |
61
|
|
|
|
62
|
|
|
$this->assertCount(3, $nameResults['buckets']); |
63
|
|
|
|
64
|
|
|
// bob |
65
|
|
|
$this->assertEquals('bob', $nameResults['buckets'][0]['key']); |
66
|
|
|
$tags = [ |
67
|
|
|
['key' => 'foo', 'doc_count' => 2], |
68
|
|
|
['key' => 'bar', 'doc_count' => 1], |
69
|
|
|
['key' => 'baz', 'doc_count' => 1], |
70
|
|
|
]; |
71
|
|
|
$this->assertEquals($tags, $nameResults['buckets'][0]['main']['tags']['buckets']); |
72
|
|
|
|
73
|
|
|
// john |
74
|
|
|
$this->assertEquals('john', $nameResults['buckets'][1]['key']); |
75
|
|
|
$tags = [ |
76
|
|
|
['key' => 'bar', 'doc_count' => 1], |
77
|
|
|
['key' => 'foo', 'doc_count' => 1], |
78
|
|
|
]; |
79
|
|
|
$this->assertEquals($tags, $nameResults['buckets'][1]['main']['tags']['buckets']); |
80
|
|
|
|
81
|
|
|
// susan |
82
|
|
|
$this->assertEquals('susan', $nameResults['buckets'][2]['key']); |
83
|
|
|
$tags = [ |
84
|
|
|
['key' => 'baz', 'doc_count' => 1], |
85
|
|
|
['key' => 'foo', 'doc_count' => 1], |
86
|
|
|
]; |
87
|
|
|
$this->assertEquals($tags, $nameResults['buckets'][2]['main']['tags']['buckets']); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function _getIndexForTest(): Index |
91
|
|
|
{ |
92
|
|
|
$index = $this->_createIndex(); |
93
|
|
|
$mapping = new Mapping(); |
94
|
|
|
$mapping->setProperties([ |
95
|
|
|
'comments' => [ |
96
|
|
|
'type' => 'nested', |
97
|
|
|
'properties' => [ |
98
|
|
|
'name' => ['type' => 'keyword'], |
99
|
|
|
'body' => ['type' => 'text'], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
'tags' => ['type' => 'keyword'], |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$index->setMapping($mapping); |
106
|
|
|
|
107
|
|
|
$index->addDocuments([ |
108
|
|
|
new Document(1, [ |
109
|
|
|
'comments' => [ |
110
|
|
|
[ |
111
|
|
|
'name' => 'bob', |
112
|
|
|
'body' => 'this is bobs comment', |
113
|
|
|
], |
114
|
|
|
[ |
115
|
|
|
'name' => 'john', |
116
|
|
|
'body' => 'this is johns comment', |
117
|
|
|
], |
118
|
|
|
], |
119
|
|
|
'tags' => ['foo', 'bar'], |
120
|
|
|
]), |
121
|
|
|
new Document(2, [ |
122
|
|
|
'comments' => [ |
123
|
|
|
[ |
124
|
|
|
'name' => 'bob', |
125
|
|
|
'body' => 'this is another comment from bob', |
126
|
|
|
], |
127
|
|
|
[ |
128
|
|
|
'name' => 'susan', |
129
|
|
|
'body' => 'this is susans comment', |
130
|
|
|
], |
131
|
|
|
], |
132
|
|
|
'tags' => ['foo', 'baz'], |
133
|
|
|
]), |
134
|
|
|
]); |
135
|
|
|
|
136
|
|
|
$index->refresh(); |
137
|
|
|
|
138
|
|
|
return $index; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|