1
|
|
|
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\FullText; |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument; |
4
|
|
|
use Nord\Lumen\Elasticsearch\Search\Traits\HasField; |
5
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasType; |
6
|
|
|
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasValue; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A family of match queries that accepts text/numerics/dates, analyzes them, and constructs a query. |
10
|
|
|
* |
11
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html |
12
|
|
|
*/ |
13
|
|
|
class MatchQuery extends AbstractQuery |
14
|
|
|
{ |
15
|
|
|
use HasField; |
16
|
|
|
use HasType; |
17
|
|
|
use HasValue; |
18
|
|
|
|
19
|
|
|
const OPERATOR_OR = 'or'; |
20
|
|
|
const OPERATOR_AND = 'and'; |
21
|
|
|
|
22
|
|
|
const ZERO_TERM_QUERY_NONE = 'none'; |
23
|
|
|
const ZERO_TERM_QUERY_ALL = 'all'; |
24
|
|
|
|
25
|
|
|
const TYPE_PHRASE = 'phrase'; |
26
|
|
|
const TYPE_PHRASE_PREFIX = 'phrase_prefix'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string The operator flag can be set to "or" or "and" to control the boolean clauses (defaults to "or"). |
30
|
|
|
*/ |
31
|
|
|
private $operator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string If the analyzer used removes all tokens in a query like a "stop" filter does, the default behavior |
35
|
|
|
* is to match no documents at all. In order to change that the zero_terms_query option can be used, which accepts |
36
|
|
|
* "none" (default) and "all" which corresponds to a "match_all" query. |
37
|
|
|
*/ |
38
|
|
|
private $zeroTermsQuery; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var float The match query supports a cutoff_frequency that allows specifying an absolute or relative document |
42
|
|
|
* frequency where high frequency terms are moved into an optional subquery and are only scored if one of the low |
43
|
|
|
* frequency (below the cutoff) terms in the case of an or operator or all of the low frequency terms in the case |
44
|
|
|
* of an and operator match. |
45
|
|
|
*/ |
46
|
|
|
private $cutOffFrequency; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int A phrase query matches terms up to a configurable slop (which defaults to 0) in any order. |
50
|
|
|
* Transposed terms have a slop of 2. |
51
|
|
|
*/ |
52
|
|
|
private $slop; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int A "phrase_prefix" query option that controls to how many prefixes the last term will be expanded. |
56
|
|
|
* It is highly recommended to set it to an acceptable value to control the execution time of the query. |
57
|
|
|
*/ |
58
|
|
|
private $maxExpansions; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string The analyzer can be set to control which analyzer will perform the analysis process on the text. It |
62
|
|
|
* defaults to the field explicit mapping definition, or the default search analyzer, |
63
|
|
|
*/ |
64
|
|
|
private $analyzer; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function toArray() |
70
|
|
|
{ |
71
|
|
|
$match = ['query' => $this->getValue()]; |
72
|
|
|
|
73
|
|
|
$match = $this->applyOptions($match); |
74
|
|
|
|
75
|
|
|
if (count($match) === 1 && isset($match['query'])) { |
76
|
|
|
$match = $match['query']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return ['match' => [$this->getField() => $match]]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $operator |
85
|
|
|
* @return MatchQuery |
86
|
|
|
* @throws InvalidArgument |
87
|
|
|
*/ |
88
|
|
|
public function setOperator($operator) |
89
|
|
|
{ |
90
|
|
|
$this->operator = $operator; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getOperator() |
99
|
|
|
{ |
100
|
|
|
return $this->operator; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $zeroTermsQuery |
106
|
|
|
* @return MatchQuery |
107
|
|
|
* @throws InvalidArgument |
108
|
|
|
*/ |
109
|
|
|
public function setZeroTermsQuery($zeroTermsQuery) |
110
|
|
|
{ |
111
|
|
|
$this->zeroTermsQuery = $zeroTermsQuery; |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getZeroTermsQuery() |
120
|
|
|
{ |
121
|
|
|
return $this->zeroTermsQuery; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param float $cutOffFrequency |
127
|
|
|
* @return MatchQuery |
128
|
|
|
* @throws InvalidArgument |
129
|
|
|
*/ |
130
|
|
|
public function setCutOffFrequency($cutOffFrequency) |
131
|
|
|
{ |
132
|
|
|
$this->cutOffFrequency = $cutOffFrequency; |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return float |
139
|
|
|
*/ |
140
|
|
|
public function getCutOffFrequency() |
141
|
|
|
{ |
142
|
|
|
return $this->cutOffFrequency; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $type |
148
|
|
|
* @return MatchQuery |
149
|
|
|
* @throws InvalidArgument |
150
|
|
|
*/ |
151
|
|
|
public function setType($type) |
152
|
|
|
{ |
153
|
|
|
$this->type = $type; |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param int $slop |
160
|
|
|
* @return MatchQuery |
161
|
|
|
* @throws InvalidArgument |
162
|
|
|
*/ |
163
|
|
|
public function setSlop($slop) |
164
|
|
|
{ |
165
|
|
|
$this->slop = $slop; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return int |
172
|
|
|
*/ |
173
|
|
|
public function getSlop() |
174
|
|
|
{ |
175
|
|
|
return $this->slop; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param int $maxExpansions |
181
|
|
|
* @return MatchQuery |
182
|
|
|
* @throws InvalidArgument |
183
|
|
|
*/ |
184
|
|
|
public function setMaxExpansions($maxExpansions) |
185
|
|
|
{ |
186
|
|
|
$this->maxExpansions = $maxExpansions; |
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return int |
193
|
|
|
*/ |
194
|
|
|
public function getMaxExpansions() |
195
|
|
|
{ |
196
|
|
|
return $this->maxExpansions; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $analyzer |
202
|
|
|
* @return MatchQuery |
203
|
|
|
*/ |
204
|
|
|
public function setAnalyzer($analyzer) |
205
|
|
|
{ |
206
|
|
|
$this->analyzer = $analyzer; |
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getAnalyzer() |
215
|
|
|
{ |
216
|
|
|
return $this->analyzer; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param array $match |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
protected function applyOptions(array $match) |
225
|
|
|
{ |
226
|
|
|
$operator = $this->getOperator(); |
227
|
|
|
if (!is_null($operator)) { |
228
|
|
|
$match['operator'] = $operator; |
229
|
|
|
} |
230
|
|
|
$zeroTermsQuery = $this->getZeroTermsQuery(); |
231
|
|
|
if (!is_null($zeroTermsQuery)) { |
232
|
|
|
$match['zero_terms_query'] = $zeroTermsQuery; |
233
|
|
|
} |
234
|
|
|
$cutOffFreq = $this->getCutOffFrequency(); |
235
|
|
|
if (!is_null($cutOffFreq)) { |
236
|
|
|
$match['cutoff_frequency'] = $cutOffFreq; |
237
|
|
|
} |
238
|
|
|
$type = $this->getType(); |
239
|
|
|
if (!is_null($type)) { |
240
|
|
|
$match['type'] = $type; |
241
|
|
|
$slop = $this->getSlop(); |
242
|
|
|
if (!is_null($slop)) { |
243
|
|
|
$match['slop'] = $slop; |
244
|
|
|
} |
245
|
|
|
if ($match['type'] === self::TYPE_PHRASE_PREFIX) { |
246
|
|
|
$maxExp = $this->getMaxExpansions(); |
247
|
|
|
if (!is_null($maxExp)) { |
248
|
|
|
$match['max_expansions'] = $maxExp; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
$analyzer = $this->getAnalyzer(); |
253
|
|
|
if (!is_null($analyzer)) { |
254
|
|
|
$match['analyzer'] = $analyzer; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $match; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|