|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Document; |
|
6
|
|
|
use Elastica\Index; |
|
7
|
|
|
use Elastica\Query\BoolQuery; |
|
8
|
|
|
use Elastica\Query\Ids; |
|
9
|
|
|
use Elastica\Query\Term; |
|
10
|
|
|
use Elastica\Test\Base as BaseTest; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
class BoolQueryTest extends BaseTest |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @group unit |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testToArray(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$query = new BoolQuery(); |
|
23
|
|
|
|
|
24
|
|
|
$idsQuery1 = new Ids(); |
|
25
|
|
|
$idsQuery1->setIds(1); |
|
26
|
|
|
|
|
27
|
|
|
$idsQuery2 = new Ids(); |
|
28
|
|
|
$idsQuery2->setIds(2); |
|
29
|
|
|
|
|
30
|
|
|
$idsQuery3 = new Ids(); |
|
31
|
|
|
$idsQuery3->setIds(3); |
|
32
|
|
|
|
|
33
|
|
|
$filter1 = new Term(); |
|
34
|
|
|
$filter1->setTerm('test', '1'); |
|
35
|
|
|
|
|
36
|
|
|
$filter2 = new Term(); |
|
37
|
|
|
$filter2->setTerm('username', 'ruth'); |
|
38
|
|
|
|
|
39
|
|
|
$boost = 1.2; |
|
40
|
|
|
$minMatch = 2; |
|
41
|
|
|
|
|
42
|
|
|
$query->setBoost($boost); |
|
43
|
|
|
$query->setMinimumShouldMatch($minMatch); |
|
44
|
|
|
$query->addMust($idsQuery1); |
|
45
|
|
|
$query->addMustNot($idsQuery2); |
|
46
|
|
|
$query->addShould($idsQuery3->toArray()); |
|
47
|
|
|
$query->addFilter($filter1); |
|
48
|
|
|
$query->addFilter($filter2); |
|
49
|
|
|
|
|
50
|
|
|
$expectedArray = [ |
|
51
|
|
|
'bool' => [ |
|
52
|
|
|
'must' => [$idsQuery1->toArray()], |
|
53
|
|
|
'should' => [$idsQuery3->toArray()], |
|
54
|
|
|
'filter' => [$filter1->toArray(), $filter2->toArray()], |
|
55
|
|
|
'minimum_should_match' => $minMatch, |
|
56
|
|
|
'must_not' => [$idsQuery2->toArray()], |
|
57
|
|
|
'boost' => $boost, |
|
58
|
|
|
], |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals($expectedArray, $query->toArray()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Test to resolve the following issue. |
|
66
|
|
|
* |
|
67
|
|
|
* @see https://groups.google.com/forum/?fromgroups#!topic/elastica-php-client/zK_W_hClfvU |
|
68
|
|
|
* |
|
69
|
|
|
* @group unit |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testToArrayStructure(): void |
|
72
|
|
|
{ |
|
73
|
|
|
$boolQuery = new BoolQuery(); |
|
74
|
|
|
|
|
75
|
|
|
$term1 = new Term(); |
|
76
|
|
|
$term1->setParam('interests', 84); |
|
77
|
|
|
|
|
78
|
|
|
$term2 = new Term(); |
|
79
|
|
|
$term2->setParam('interests', 92); |
|
80
|
|
|
|
|
81
|
|
|
$boolQuery->addShould($term1)->addShould($term2); |
|
82
|
|
|
|
|
83
|
|
|
$jsonString = '{"bool":{"should":[{"term":{"interests":84}},{"term":{"interests":92}}]}}'; |
|
84
|
|
|
$this->assertEquals($jsonString, \json_encode($boolQuery->toArray())); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @group functional |
|
89
|
|
|
*/ |
|
90
|
|
|
public function testSearch(): void |
|
91
|
|
|
{ |
|
92
|
|
|
$client = $this->_getClient(); |
|
93
|
|
|
$index = new Index($client, 'test'); |
|
94
|
|
|
$index->create([], true); |
|
95
|
|
|
|
|
96
|
|
|
$doc = new Document(1, ['id' => 1, 'email' => '[email protected]', 'username' => 'hans', 'test' => ['2', '4', '5']]); |
|
97
|
|
|
$index->addDocument($doc); |
|
98
|
|
|
$doc = new Document(2, ['id' => 2, 'email' => '[email protected]', 'username' => 'emil', 'test' => ['1', '3', '6']]); |
|
99
|
|
|
$index->addDocument($doc); |
|
100
|
|
|
$doc = new Document(3, ['id' => 3, 'email' => '[email protected]', 'username' => 'ruth', 'test' => ['2', '3', '7']]); |
|
101
|
|
|
$index->addDocument($doc); |
|
102
|
|
|
$doc = new Document(4, ['id' => 4, 'email' => '[email protected]', 'username' => 'john', 'test' => ['2', '4', '8']]); |
|
103
|
|
|
$index->addDocument($doc); |
|
104
|
|
|
|
|
105
|
|
|
// Refresh index |
|
106
|
|
|
$index->refresh(); |
|
107
|
|
|
|
|
108
|
|
|
$boolQuery = new BoolQuery(); |
|
109
|
|
|
$termQuery1 = new Term(['test' => '2']); |
|
110
|
|
|
$boolQuery->addMust($termQuery1); |
|
111
|
|
|
$resultSet = $index->search($boolQuery); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
$this->assertEquals(3, $resultSet->count()); |
|
114
|
|
|
|
|
115
|
|
|
$termFilter = new Term(['test' => '4']); |
|
116
|
|
|
$boolQuery->addFilter($termFilter); |
|
117
|
|
|
$resultSet = $index->search($boolQuery); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals(2, $resultSet->count()); |
|
120
|
|
|
|
|
121
|
|
|
$termQuery2 = new Term(['test' => '5']); |
|
122
|
|
|
$boolQuery->addMust($termQuery2); |
|
123
|
|
|
$resultSet = $index->search($boolQuery); |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
126
|
|
|
|
|
127
|
|
|
$termQuery3 = new Term(['username' => 'hans']); |
|
128
|
|
|
$boolQuery->addMust($termQuery3); |
|
129
|
|
|
$resultSet = $index->search($boolQuery); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
$this->assertEquals(1, $resultSet->count()); |
|
132
|
|
|
|
|
133
|
|
|
$termQuery4 = new Term(['username' => 'emil']); |
|
134
|
|
|
$boolQuery->addMust($termQuery4); |
|
135
|
|
|
$resultSet = $index->search($boolQuery); |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$this->assertEquals(0, $resultSet->count()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @group functional |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testEmptyBoolQuery(): void |
|
144
|
|
|
{ |
|
145
|
|
|
$index = $this->_createIndex(); |
|
146
|
|
|
|
|
147
|
|
|
$docNumber = 3; |
|
148
|
|
|
for ($i = 0; $i < $docNumber; ++$i) { |
|
149
|
|
|
$doc = new Document($i, ['email' => '[email protected]']); |
|
150
|
|
|
$index->addDocument($doc); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$index->refresh(); |
|
154
|
|
|
|
|
155
|
|
|
$boolQuery = new BoolQuery(); |
|
156
|
|
|
|
|
157
|
|
|
$resultSet = $index->search($boolQuery); |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
$this->assertEquals($resultSet->count(), $docNumber); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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: