1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
4
|
|
|
|
5
|
|
|
use Elastica\Mapping; |
6
|
|
|
use Elastica\QueryBuilder\DSL\Query; |
7
|
|
|
use Elastica\Search; |
8
|
|
|
use Elastica\Test\Base as BaseTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
class ParentIdTest extends BaseTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @group unit |
17
|
|
|
*/ |
18
|
|
|
public function testToArray(): void |
19
|
|
|
{ |
20
|
|
|
$query = new ParentId('join', '1'); |
21
|
|
|
$expectedArray = [ |
22
|
|
|
'parent_id' => [ |
23
|
|
|
'type' => 'join', |
24
|
|
|
'id' => 1, |
25
|
|
|
'ignore_unmapped' => false, |
26
|
|
|
], |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
$this->assertEquals($expectedArray, $query->toArray()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @group functional |
34
|
|
|
*/ |
35
|
|
|
public function testParentId(): void |
36
|
|
|
{ |
37
|
|
|
$client = $this->_getClient(); |
38
|
|
|
$index = $client->getIndex('testparentid'); |
39
|
|
|
$index->create([], true); |
40
|
|
|
|
41
|
|
|
$mapping = new Mapping([ |
42
|
|
|
'firstname' => ['type' => 'text', 'store' => true], |
43
|
|
|
'lastname' => ['type' => 'text'], |
44
|
|
|
'my_join_field' => [ |
45
|
|
|
'type' => 'join', |
46
|
|
|
'relations' => [ |
47
|
|
|
'question' => 'answer', |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$index->setMapping($mapping); |
53
|
|
|
|
54
|
|
|
$expected = [ |
55
|
|
|
'properties' => [ |
56
|
|
|
'firstname' => ['type' => 'text', 'store' => true], |
57
|
|
|
'lastname' => ['type' => 'text'], |
58
|
|
|
'my_join_field' => [ |
59
|
|
|
'type' => 'join', |
60
|
|
|
'relations' => [ |
61
|
|
|
'question' => 'answer', |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->assertEquals($expected, $mapping->toArray()); |
68
|
|
|
$index->refresh(); |
69
|
|
|
|
70
|
|
|
$doc1 = $index->createDocument('1', [ |
71
|
|
|
'text' => 'this is the 1st question', |
72
|
|
|
'my_join_field' => [ |
73
|
|
|
'name' => 'question', |
74
|
|
|
], |
75
|
|
|
]); |
76
|
|
|
$doc2 = $index->createDocument('2', [ |
77
|
|
|
'text' => 'this is the 2nd question', |
78
|
|
|
'my_join_field' => [ |
79
|
|
|
'name' => 'question', |
80
|
|
|
], |
81
|
|
|
]); |
82
|
|
|
$index->addDocuments([$doc1, $doc2]); |
83
|
|
|
|
84
|
|
|
$doc3 = $index->createDocument('3', [ |
85
|
|
|
'text' => 'this is an answer, the 1st', |
86
|
|
|
'my_join_field' => [ |
87
|
|
|
'name' => 'answer', |
88
|
|
|
'parent' => 1, |
89
|
|
|
], |
90
|
|
|
]); |
91
|
|
|
$doc4 = $index->createDocument('4', [ |
92
|
|
|
'text' => 'this is an answer, the 2nd', |
93
|
|
|
'my_join_field' => [ |
94
|
|
|
'name' => 'answer', |
95
|
|
|
'parent' => '2', |
96
|
|
|
], |
97
|
|
|
]); |
98
|
|
|
$doc5 = $index->createDocument('5', [ |
99
|
|
|
'text' => 'this is an answer, the 3rd', |
100
|
|
|
'my_join_field' => [ |
101
|
|
|
'name' => 'answer', |
102
|
|
|
'parent' => '2', |
103
|
|
|
], |
104
|
|
|
]); |
105
|
|
|
$this->_getClient()->addDocuments([$doc3, $doc4, $doc5], ['routing' => 1]); |
106
|
|
|
$index->refresh(); |
107
|
|
|
|
108
|
|
|
$parentQuery = new ParentId('answer', 1, true); |
109
|
|
|
$search = new Search($index->getClient()); |
110
|
|
|
$results = $search->search($parentQuery); |
111
|
|
|
$this->assertEquals(1, $results->count()); |
112
|
|
|
|
113
|
|
|
$result = $results->current(); |
114
|
|
|
$data = $result->getData(); |
115
|
|
|
$this->assertEquals($data['text'], 'this is an answer, the 1st'); |
116
|
|
|
|
117
|
|
|
$parentQuery = new ParentId('answer', '2', true); |
118
|
|
|
$search = new Search($index->getClient()); |
119
|
|
|
$results = $search->search($parentQuery); |
120
|
|
|
$this->assertEquals(2, $results->count()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @group unit |
125
|
|
|
*/ |
126
|
|
|
public function testQueryBuilderParentId(): void |
127
|
|
|
{ |
128
|
|
|
$client = $this->_getClient(); |
129
|
|
|
$index = $client->getIndex('testparentid'); |
130
|
|
|
$index->create([], true); |
131
|
|
|
|
132
|
|
|
$mapping = new Mapping([ |
133
|
|
|
'firstname' => ['type' => 'text', 'store' => true], |
134
|
|
|
'lastname' => ['type' => 'text'], |
135
|
|
|
'my_join_field' => [ |
136
|
|
|
'type' => 'join', |
137
|
|
|
'relations' => [ |
138
|
|
|
'question' => 'answer', |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
]); |
142
|
|
|
|
143
|
|
|
$index->setMapping($mapping); |
144
|
|
|
$index->refresh(); |
145
|
|
|
|
146
|
|
|
$doc1 = $index->createDocument('1', [ |
147
|
|
|
'text' => 'this is the 1st question', |
148
|
|
|
'my_join_field' => [ |
149
|
|
|
'name' => 'question', |
150
|
|
|
], |
151
|
|
|
]); |
152
|
|
|
|
153
|
|
|
$doc2 = $index->createDocument('2', [ |
154
|
|
|
'text' => 'this is the 2nd question', |
155
|
|
|
'my_join_field' => [ |
156
|
|
|
'name' => 'question', |
157
|
|
|
], |
158
|
|
|
]); |
159
|
|
|
$index->addDocuments([$doc1, $doc2]); |
160
|
|
|
|
161
|
|
|
$doc3 = $index->createDocument('3', [ |
162
|
|
|
'text' => 'this is an answer, the 1st', |
163
|
|
|
'my_join_field' => [ |
164
|
|
|
'name' => 'answer', |
165
|
|
|
'parent' => 1, |
166
|
|
|
], |
167
|
|
|
]); |
168
|
|
|
$doc4 = $index->createDocument('4', [ |
169
|
|
|
'text' => 'this is an answer, the 2nd', |
170
|
|
|
'my_join_field' => [ |
171
|
|
|
'name' => 'answer', |
172
|
|
|
'parent' => 2, |
173
|
|
|
], |
174
|
|
|
]); |
175
|
|
|
$doc5 = $index->createDocument('5', [ |
176
|
|
|
'text' => 'this is an answer, the 3rd', |
177
|
|
|
'my_join_field' => [ |
178
|
|
|
'name' => 'answer', |
179
|
|
|
'parent' => 2, |
180
|
|
|
], |
181
|
|
|
]); |
182
|
|
|
$this->_getClient()->addDocuments([$doc3, $doc4, $doc5], ['routing' => 1]); |
183
|
|
|
$index->refresh(); |
184
|
|
|
|
185
|
|
|
$queryDSL = new Query(); |
186
|
|
|
$parentId = $queryDSL->parent_id('answer', 1, true); |
187
|
|
|
$search = new Search($index->getClient()); |
188
|
|
|
$results = $search->search($parentId); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$this->assertEquals(1, $results->count()); |
191
|
|
|
|
192
|
|
|
$result = $results->current(); |
193
|
|
|
$data = $result->getData(); |
194
|
|
|
$this->assertEquals($data['text'], 'this is an answer, the 1st'); |
195
|
|
|
|
196
|
|
|
$parentId = $queryDSL->parent_id('answer', 2, true); |
197
|
|
|
$search = new Search($index->getClient()); |
198
|
|
|
$results = $search->search($parentId); |
|
|
|
|
199
|
|
|
|
200
|
|
|
$this->assertEquals(2, $results->count()); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: