1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
4
|
|
|
|
5
|
|
|
use Elastica\Document; |
6
|
|
|
use Elastica\Exception\InvalidException; |
7
|
|
|
use Elastica\Query\Terms; |
8
|
|
|
use Elastica\Test\Base as BaseTest; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
* @group unit |
13
|
|
|
*/ |
14
|
|
|
class TermsTest extends BaseTest |
15
|
|
|
{ |
16
|
|
|
public function testSetTermsLookup(): void |
17
|
|
|
{ |
18
|
|
|
$terms = [ |
19
|
|
|
'index' => 'index_name', |
20
|
|
|
'id' => '1', |
21
|
|
|
'path' => 'terms', |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
$query = new Terms('name'); |
25
|
|
|
$query->setTermsLookup('index_name', '1', 'terms'); |
26
|
|
|
|
27
|
|
|
$data = $query->toArray(); |
28
|
|
|
$this->assertEquals($terms, $data['terms']['name']); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testInvalidParams(): void |
32
|
|
|
{ |
33
|
|
|
$query = new Terms('field', ['aaa', 'bbb']); |
34
|
|
|
$query->setTermsLookup('index', '1', 'path'); |
35
|
|
|
|
36
|
|
|
$this->expectException(InvalidException::class); |
37
|
|
|
$query->toArray(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testEmptyField(): void |
41
|
|
|
{ |
42
|
|
|
$this->expectException(InvalidException::class); |
43
|
|
|
new Terms(''); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @group functional |
48
|
|
|
*/ |
49
|
|
|
public function testFilteredSearch(): void |
50
|
|
|
{ |
51
|
|
|
$index = $this->_createIndex(); |
52
|
|
|
|
53
|
|
|
$index->addDocuments([ |
54
|
|
|
new Document(1, ['name' => 'hello world']), |
55
|
|
|
new Document(2, ['name' => 'nicolas ruflin']), |
56
|
|
|
new Document(3, ['name' => 'ruflin']), |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$query = new Terms('name', ['nicolas', 'hello']); |
60
|
|
|
|
61
|
|
|
$index->refresh(); |
62
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->assertEquals(2, $resultSet->count()); |
65
|
|
|
|
66
|
|
|
$query->addTerm('ruflin'); |
67
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->assertEquals(3, $resultSet->count()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @group functional |
74
|
|
|
*/ |
75
|
|
|
public function testFilteredSearchWithLookup(): void |
76
|
|
|
{ |
77
|
|
|
$index = $this->_createIndex(); |
78
|
|
|
|
79
|
|
|
$lookupIndex = $this->_createIndex('lookup_index'); |
80
|
|
|
$lookupIndex->addDocuments([ |
81
|
|
|
new Document(1, ['terms' => ['ruflin', 'nicolas']]), |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$index->addDocuments([ |
85
|
|
|
new Document(1, ['name' => 'hello world']), |
86
|
|
|
new Document(2, ['name' => 'nicolas ruflin']), |
87
|
|
|
new Document(3, ['name' => 'ruflin']), |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$query = new Terms('name'); |
91
|
|
|
$query->setTermsLookup($lookupIndex->getName(), '1', 'terms'); |
92
|
|
|
$index->refresh(); |
93
|
|
|
$lookupIndex->refresh(); |
94
|
|
|
|
95
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->assertEquals(2, $resultSet->count()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @group functional |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function testVariousDataTypesViaConstructor(): void |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$index = $this->_createIndex(); |
106
|
|
|
|
107
|
|
|
$index->addDocuments([ |
108
|
|
|
new Document(1, ['some_numeric_field' => 9876]), |
109
|
|
|
]); |
110
|
|
|
$index->refresh(); |
111
|
|
|
|
112
|
|
|
// string |
113
|
|
|
$query = new Terms('some_numeric_field', ['9876']); |
114
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
115
|
|
|
$this->assertEquals(1, $resultSet->count()); |
116
|
|
|
|
117
|
|
|
// int |
118
|
|
|
$query = new Terms('some_numeric_field', [9876]); |
119
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
120
|
|
|
$this->assertEquals(1, $resultSet->count()); |
121
|
|
|
|
122
|
|
|
// float |
123
|
|
|
$query = new Terms('some_numeric_field', [9876.0]); |
124
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
125
|
|
|
$this->assertEquals(1, $resultSet->count()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @group functional |
130
|
|
|
*/ |
131
|
|
|
public function testVariousMixedDataTypesViaConstructor(): void |
132
|
|
|
{ |
133
|
|
|
$index = $this->_createIndex(); |
134
|
|
|
|
135
|
|
|
$index->addDocuments([ |
136
|
|
|
new Document(1, ['some_numeric_field' => 9876]), |
137
|
|
|
new Document(2, ['some_numeric_field' => 5678]), |
138
|
|
|
new Document(3, ['some_numeric_field' => 1234]), |
139
|
|
|
new Document(4, ['some_numeric_field' => 8899]), |
140
|
|
|
]); |
141
|
|
|
$index->refresh(); |
142
|
|
|
|
143
|
|
|
$query = new Terms('some_numeric_field', ['9876', 1234, 5678.0]); |
144
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
145
|
|
|
$this->assertEquals(3, $resultSet->count()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @group functional |
150
|
|
|
*/ |
151
|
|
|
public function testVariousDataTypesViaAddTerm(): void |
152
|
|
|
{ |
153
|
|
|
$index = $this->_createIndex(); |
154
|
|
|
|
155
|
|
|
$index->addDocuments([ |
156
|
|
|
new Document(1, ['some_numeric_field' => 9876]), |
157
|
|
|
]); |
158
|
|
|
$index->refresh(); |
159
|
|
|
|
160
|
|
|
// string |
161
|
|
|
$query = new Terms('some_numeric_field'); |
162
|
|
|
$query->addTerm('9876'); |
163
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
164
|
|
|
$this->assertEquals(1, $resultSet->count()); |
165
|
|
|
|
166
|
|
|
// int |
167
|
|
|
$query = new Terms('some_numeric_field'); |
168
|
|
|
$query->addTerm(9876); |
169
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
170
|
|
|
$this->assertEquals(1, $resultSet->count()); |
171
|
|
|
|
172
|
|
|
// float |
173
|
|
|
$query = new Terms('some_numeric_field'); |
174
|
|
|
$query->addTerm(9876.0); |
175
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
176
|
|
|
$this->assertEquals(1, $resultSet->count()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testAddTermTypeError(): void |
180
|
|
|
{ |
181
|
|
|
$this->expectException(\TypeError::class); |
182
|
|
|
$this->expectExceptionMessage('Argument 1 passed to "Elastica\Query\Terms::addTerm()" must be of type float|int|string, stdClass given.'); |
183
|
|
|
|
184
|
|
|
$query = new Terms('some_numeric_field'); |
185
|
|
|
$query->addTerm(new \stdClass()); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
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: