|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica\Test\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Document; |
|
6
|
|
|
use Elastica\Query\MatchQuery; |
|
7
|
|
|
use Elastica\Test\Base as BaseTest; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
class MatchQueryTest extends BaseTest |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @group unit |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testToArray(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$field = 'test'; |
|
20
|
|
|
$testQuery = 'Nicolas Ruflin'; |
|
21
|
|
|
$operator = 'and'; |
|
22
|
|
|
$analyzer = 'myanalyzer'; |
|
23
|
|
|
$boost = 2.0; |
|
24
|
|
|
$minimumShouldMatch = 2; |
|
25
|
|
|
$fuzziness = 0.3; |
|
26
|
|
|
$fuzzyRewrite = 'constant_score_boolean'; |
|
27
|
|
|
$prefixLength = 3; |
|
28
|
|
|
$maxExpansions = 12; |
|
29
|
|
|
|
|
30
|
|
|
$query = new MatchQuery(); |
|
31
|
|
|
$query->setFieldQuery($field, $testQuery); |
|
32
|
|
|
$this->hideDeprecated(); |
|
33
|
|
|
$this->showDeprecated(); |
|
34
|
|
|
$query->setFieldOperator($field, $operator); |
|
35
|
|
|
$query->setFieldAnalyzer($field, $analyzer); |
|
36
|
|
|
$query->setFieldBoost($field, $boost); |
|
37
|
|
|
$query->setFieldMinimumShouldMatch($field, $minimumShouldMatch); |
|
38
|
|
|
$query->setFieldFuzziness($field, $fuzziness); |
|
39
|
|
|
$query->setFieldFuzzyRewrite($field, $fuzzyRewrite); |
|
40
|
|
|
$query->setFieldPrefixLength($field, $prefixLength); |
|
41
|
|
|
$query->setFieldMaxExpansions($field, $maxExpansions); |
|
42
|
|
|
|
|
43
|
|
|
$expectedArray = [ |
|
44
|
|
|
'match' => [ |
|
45
|
|
|
$field => [ |
|
46
|
|
|
'query' => $testQuery, |
|
47
|
|
|
'operator' => $operator, |
|
48
|
|
|
'analyzer' => $analyzer, |
|
49
|
|
|
'boost' => $boost, |
|
50
|
|
|
'minimum_should_match' => $minimumShouldMatch, |
|
51
|
|
|
'fuzziness' => $fuzziness, |
|
52
|
|
|
'fuzzy_rewrite' => $fuzzyRewrite, |
|
53
|
|
|
'prefix_length' => $prefixLength, |
|
54
|
|
|
'max_expansions' => $maxExpansions, |
|
55
|
|
|
], |
|
56
|
|
|
], |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals($expectedArray, $query->toArray()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @group functional |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function testMatch(): void |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$client = $this->_getClient(); |
|
68
|
|
|
$index = $client->getIndex('test'); |
|
69
|
|
|
$index->create([], true); |
|
70
|
|
|
|
|
71
|
|
|
$index->addDocuments([ |
|
72
|
|
|
new Document(1, ['name' => 'Basel-Stadt']), |
|
73
|
|
|
new Document(2, ['name' => 'New York']), |
|
74
|
|
|
new Document(3, ['name' => 'New Hampshire']), |
|
75
|
|
|
new Document(4, ['name' => 'Basel Land']), |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
$index->refresh(); |
|
79
|
|
|
|
|
80
|
|
|
$field = 'name'; |
|
81
|
|
|
$operator = 'or'; |
|
82
|
|
|
|
|
83
|
|
|
$query = new MatchQuery(); |
|
84
|
|
|
$query->setFieldQuery($field, 'Basel New'); |
|
85
|
|
|
$query->setFieldOperator($field, $operator); |
|
86
|
|
|
|
|
87
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
$this->assertEquals(4, $resultSet->count()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @group functional |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function testMatchSetFieldBoost(): void |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$client = $this->_getClient(); |
|
98
|
|
|
$index = $client->getIndex('test'); |
|
99
|
|
|
$index->create([], true); |
|
100
|
|
|
|
|
101
|
|
|
$index->addDocuments([ |
|
102
|
|
|
new Document(1, ['name' => 'Basel-Stadt']), |
|
103
|
|
|
new Document(2, ['name' => 'New York']), |
|
104
|
|
|
new Document(3, ['name' => 'New Hampshire']), |
|
105
|
|
|
new Document(4, ['name' => 'Basel Land']), |
|
106
|
|
|
]); |
|
107
|
|
|
|
|
108
|
|
|
$index->refresh(); |
|
109
|
|
|
|
|
110
|
|
|
$field = 'name'; |
|
111
|
|
|
$operator = 'or'; |
|
112
|
|
|
|
|
113
|
|
|
$query = new MatchQuery(); |
|
114
|
|
|
$query->setFieldQuery($field, 'Basel New'); |
|
115
|
|
|
$query->setFieldOperator($field, $operator); |
|
116
|
|
|
$query->setFieldBoost($field, 1.2); |
|
117
|
|
|
|
|
118
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$this->assertEquals(4, $resultSet->count()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @group functional |
|
125
|
|
|
*/ |
|
126
|
|
View Code Duplication |
public function testMatchSetFieldBoostWithString(): void |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
$client = $this->_getClient(); |
|
129
|
|
|
$index = $client->getIndex('test'); |
|
130
|
|
|
$index->create([], true); |
|
131
|
|
|
|
|
132
|
|
|
$index->addDocuments([ |
|
133
|
|
|
new Document(1, ['name' => 'Basel-Stadt']), |
|
134
|
|
|
new Document(2, ['name' => 'New York']), |
|
135
|
|
|
new Document(3, ['name' => 'New Hampshire']), |
|
136
|
|
|
new Document(4, ['name' => 'Basel Land']), |
|
137
|
|
|
]); |
|
138
|
|
|
|
|
139
|
|
|
$index->refresh(); |
|
140
|
|
|
|
|
141
|
|
|
$field = 'name'; |
|
142
|
|
|
$operator = 'or'; |
|
143
|
|
|
|
|
144
|
|
|
$query = new MatchQuery(); |
|
145
|
|
|
$query->setFieldQuery($field, 'Basel New'); |
|
146
|
|
|
$query->setFieldOperator($field, $operator); |
|
147
|
|
|
$query->setFieldBoost($field, '1.2'); |
|
148
|
|
|
|
|
149
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
$this->assertEquals(4, $resultSet->count()); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @group functional |
|
156
|
|
|
*/ |
|
157
|
|
|
public function testMatchZeroTerm(): void |
|
158
|
|
|
{ |
|
159
|
|
|
$client = $this->_getClient(); |
|
160
|
|
|
$index = $client->getIndex('test'); |
|
161
|
|
|
$index->create([], true); |
|
162
|
|
|
|
|
163
|
|
|
$index->addDocuments([ |
|
164
|
|
|
new Document(1, ['name' => 'Basel-Stadt']), |
|
165
|
|
|
new Document(2, ['name' => 'New York']), |
|
166
|
|
|
]); |
|
167
|
|
|
|
|
168
|
|
|
$index->refresh(); |
|
169
|
|
|
|
|
170
|
|
|
$query = new MatchQuery(); |
|
171
|
|
|
$query->setFieldQuery('name', ''); |
|
172
|
|
|
$query->setFieldZeroTermsQuery('name', MatchQuery::ZERO_TERM_ALL); |
|
173
|
|
|
|
|
174
|
|
|
$resultSet = $index->search($query); |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
$this->assertEquals(2, $resultSet->count()); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @group unit |
|
181
|
|
|
*/ |
|
182
|
|
|
public function testMatchFuzzinessType(): void |
|
183
|
|
|
{ |
|
184
|
|
|
$field = 'test'; |
|
185
|
|
|
$query = new MatchQuery(); |
|
186
|
|
|
|
|
187
|
|
|
$fuzziness = 'AUTO'; |
|
188
|
|
|
$query->setFieldFuzziness($field, $fuzziness); |
|
189
|
|
|
|
|
190
|
|
|
$parameters = $query->getParam($field); |
|
191
|
|
|
$this->assertEquals($fuzziness, $parameters['fuzziness']); |
|
192
|
|
|
|
|
193
|
|
|
$fuzziness = 0.3; |
|
194
|
|
|
$query->setFieldFuzziness($field, $fuzziness); |
|
195
|
|
|
|
|
196
|
|
|
$parameters = $query->getParam($field); |
|
197
|
|
|
$this->assertEquals($fuzziness, $parameters['fuzziness']); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @group unit |
|
202
|
|
|
*/ |
|
203
|
|
|
public function testConstruct(): void |
|
204
|
|
|
{ |
|
205
|
|
|
$match = new MatchQuery(null, 'values'); |
|
206
|
|
|
$this->assertEquals(['match' => []], $match->toArray()); |
|
207
|
|
|
|
|
208
|
|
|
$match = new MatchQuery('field', null); |
|
209
|
|
|
$this->assertEquals(['match' => []], $match->toArray()); |
|
210
|
|
|
|
|
211
|
|
|
$match1 = new MatchQuery('field', 'values'); |
|
212
|
|
|
$match2 = new MatchQuery(); |
|
213
|
|
|
$match2->setField('field', 'values'); |
|
214
|
|
|
$this->assertEquals($match1->toArray(), $match2->toArray()); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
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.