|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Mapping; |
|
6
|
|
|
use Elastica\Query\HasParent; |
|
7
|
|
|
use Elastica\Query\MatchAll; |
|
8
|
|
|
use Elastica\Search; |
|
9
|
|
|
use Elastica\Test\Base as BaseTest; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
View Code Duplication |
class HasParentTest extends BaseTest |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @group unit |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testToArray(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$q = new MatchAll(); |
|
22
|
|
|
|
|
23
|
|
|
$type = 'test'; |
|
24
|
|
|
|
|
25
|
|
|
$query = new HasParent($q, $type); |
|
26
|
|
|
|
|
27
|
|
|
$expectedArray = [ |
|
28
|
|
|
'has_parent' => [ |
|
29
|
|
|
'query' => $q->toArray(), |
|
30
|
|
|
'parent_type' => $type, |
|
31
|
|
|
], |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
$this->assertEquals($expectedArray, $query->toArray()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @group unit |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testSetScope(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$q = new MatchAll(); |
|
43
|
|
|
|
|
44
|
|
|
$type = 'test'; |
|
45
|
|
|
|
|
46
|
|
|
$scope = 'foo'; |
|
47
|
|
|
|
|
48
|
|
|
$query = new HasParent($q, $type); |
|
49
|
|
|
$query->setScope($scope); |
|
50
|
|
|
|
|
51
|
|
|
$expectedArray = [ |
|
52
|
|
|
'has_parent' => [ |
|
53
|
|
|
'query' => $q->toArray(), |
|
54
|
|
|
'parent_type' => $type, |
|
55
|
|
|
'_scope' => $scope, |
|
56
|
|
|
], |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals($expectedArray, $query->toArray()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @group functional |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testHasParent(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$client = $this->_getClient(); |
|
68
|
|
|
$index = $client->getIndex('testhasparentjoin'); |
|
69
|
|
|
$index->create([], true); |
|
70
|
|
|
$mapping = new Mapping([ |
|
71
|
|
|
'text' => ['type' => 'keyword'], |
|
72
|
|
|
'name' => ['type' => 'keyword'], |
|
73
|
|
|
'my_join_field' => [ |
|
74
|
|
|
'type' => 'join', |
|
75
|
|
|
'relations' => [ |
|
76
|
|
|
'question' => 'answer', |
|
77
|
|
|
], |
|
78
|
|
|
], |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
$index->setMapping($mapping); |
|
82
|
|
|
$index->refresh(); |
|
83
|
|
|
|
|
84
|
|
|
$doc1 = $index->createDocument(1, [ |
|
85
|
|
|
'text' => 'this is the 1st question', |
|
86
|
|
|
'my_join_field' => [ |
|
87
|
|
|
'name' => 'question', |
|
88
|
|
|
], |
|
89
|
|
|
]); |
|
90
|
|
|
$doc2 = $index->createDocument(2, [ |
|
91
|
|
|
'text' => 'this is the 2nd question', |
|
92
|
|
|
'my_join_field' => [ |
|
93
|
|
|
'name' => 'question', |
|
94
|
|
|
], |
|
95
|
|
|
]); |
|
96
|
|
|
$index->addDocuments([$doc1, $doc2]); |
|
97
|
|
|
|
|
98
|
|
|
$doc3 = $index->createDocument(3, [ |
|
99
|
|
|
'text' => 'this is an answer, the 1st', |
|
100
|
|
|
'name' => 'rico', |
|
101
|
|
|
'my_join_field' => [ |
|
102
|
|
|
'name' => 'answer', |
|
103
|
|
|
'parent' => 1, |
|
104
|
|
|
], |
|
105
|
|
|
]); |
|
106
|
|
|
$doc4 = $index->createDocument(4, [ |
|
107
|
|
|
'text' => 'this is an answer, the 2nd', |
|
108
|
|
|
'name' => 'fede', |
|
109
|
|
|
'my_join_field' => [ |
|
110
|
|
|
'name' => 'answer', |
|
111
|
|
|
'parent' => 2, |
|
112
|
|
|
], |
|
113
|
|
|
]); |
|
114
|
|
|
$doc5 = $index->createDocument(5, [ |
|
115
|
|
|
'text' => 'this is an answer, the 3rd', |
|
116
|
|
|
'name' => 'fede', |
|
117
|
|
|
'my_join_field' => [ |
|
118
|
|
|
'name' => 'answer', |
|
119
|
|
|
'parent' => 2, |
|
120
|
|
|
], |
|
121
|
|
|
]); |
|
122
|
|
|
|
|
123
|
|
|
$index->addDocuments([$doc3, $doc4, $doc5], ['routing' => 1]); |
|
124
|
|
|
$index->refresh(); |
|
125
|
|
|
|
|
126
|
|
|
$parentQuery = new HasParent(new MatchAll(), 'question'); |
|
127
|
|
|
$search = new Search($index->getClient()); |
|
128
|
|
|
$results = $search->search($parentQuery); |
|
|
|
|
|
|
129
|
|
|
$this->assertEquals(3, $results->count()); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
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.