1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of PhpStorm. |
4
|
|
|
* |
5
|
|
|
* (c) PHPinnacle Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace PHPinnacle\Elastics\Traits; |
14
|
|
|
|
15
|
|
|
use PHPinnacle\Elastics\Exception; |
16
|
|
|
|
17
|
|
|
trait Match |
18
|
|
|
{ |
19
|
|
|
use |
20
|
|
|
Fuzzy, |
21
|
|
|
Analyzer |
22
|
|
|
; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $operator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Boolean |
31
|
|
|
*/ |
32
|
|
|
protected $lenient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $zeroTermsQuery; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var float |
41
|
|
|
*/ |
42
|
|
|
protected $cutoffFrequency; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $minimumShouldMatch; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $operator |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
6 |
|
public function operator(string $operator): self |
55
|
|
|
{ |
56
|
6 |
|
$this->guardOperator($operator); |
57
|
|
|
|
58
|
3 |
|
$this->operator = $operator; |
59
|
|
|
|
60
|
3 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Boolean $lenient |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
3 |
|
public function lenient(bool $lenient): self |
69
|
|
|
{ |
70
|
3 |
|
$this->lenient = $lenient; |
71
|
|
|
|
72
|
3 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $zeroTerms |
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
6 |
|
public function zeroTerms(string $zeroTerms): self |
81
|
|
|
{ |
82
|
6 |
|
$this->guardZeroTerms($zeroTerms); |
83
|
|
|
|
84
|
3 |
|
$this->zeroTermsQuery = $zeroTerms; |
85
|
|
|
|
86
|
3 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param float $cutoffFrequency |
91
|
|
|
* |
92
|
|
|
* @return self |
93
|
|
|
*/ |
94
|
3 |
|
public function cutoffFrequency(float $cutoffFrequency): self |
95
|
|
|
{ |
96
|
3 |
|
$this->cutoffFrequency = $cutoffFrequency; |
97
|
|
|
|
98
|
3 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param int|string $shouldMatch |
103
|
|
|
* |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
3 |
|
public function shouldMatch($shouldMatch): self |
107
|
|
|
{ |
108
|
3 |
|
$this->minimumShouldMatch = $shouldMatch; |
109
|
|
|
|
110
|
3 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param array $fields |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
35 |
|
private function options(array $fields = []): array |
119
|
|
|
{ |
120
|
35 |
|
$options = []; |
121
|
|
|
|
122
|
35 |
|
if ($this->boost !== null) { |
123
|
3 |
|
$options[\ELASTICS_FIELD_BOOST] = $this->boost; |
124
|
|
|
} |
125
|
|
|
|
126
|
35 |
|
if ($this->prefixLength !== null) { |
127
|
3 |
|
$options[\ELASTICS_FIELD_PREFIX_LENGTH] = $this->prefixLength; |
128
|
|
|
} |
129
|
|
|
|
130
|
35 |
|
if ($this->maxExpansions !== null) { |
131
|
3 |
|
$options[\ELASTICS_FIELD_MAX_EXPANSIONS] = $this->maxExpansions; |
132
|
|
|
} |
133
|
|
|
|
134
|
35 |
|
if ($this->fuzziness !== null) { |
135
|
3 |
|
$options[\ELASTICS_FIELD_FUZZINESS] = $this->fuzziness; |
136
|
|
|
} |
137
|
|
|
|
138
|
35 |
|
if ($this->transpositions !== null) { |
139
|
3 |
|
$options[\ELASTICS_FIELD_TRANSPOSITIONS] = $this->transpositions; |
140
|
|
|
} |
141
|
|
|
|
142
|
35 |
|
if ($this->operator !== null) { |
143
|
3 |
|
$options[\ELASTICS_FIELD_OPERATOR] = $this->operator; |
144
|
|
|
} |
145
|
|
|
|
146
|
35 |
|
if ($this->lenient !== null) { |
147
|
3 |
|
$options[\ELASTICS_FIELD_LENIENT] = $this->lenient; |
148
|
|
|
} |
149
|
|
|
|
150
|
35 |
|
if ($this->zeroTermsQuery !== null) { |
151
|
3 |
|
$options[\ELASTICS_FIELD_ZERO_TERMS] = $this->zeroTermsQuery; |
152
|
|
|
} |
153
|
|
|
|
154
|
35 |
|
if ($this->cutoffFrequency !== null) { |
155
|
3 |
|
$options[\ELASTICS_FIELD_CUTOFF_FREQUENCY] = $this->cutoffFrequency; |
156
|
|
|
} |
157
|
|
|
|
158
|
35 |
|
if ($this->minimumShouldMatch !== null) { |
159
|
3 |
|
$options[\ELASTICS_FIELD_MINIMUM_MATCH] = $this->minimumShouldMatch; |
160
|
|
|
} |
161
|
|
|
|
162
|
35 |
|
foreach ($fields as $field => $name) { |
163
|
35 |
|
if (!isset($options[$field])) { |
164
|
34 |
|
continue; |
165
|
|
|
} |
166
|
|
|
|
167
|
9 |
|
$options[$name] = $options[$field]; |
168
|
|
|
|
169
|
9 |
|
unset($options[$field]); |
170
|
|
|
} |
171
|
|
|
|
172
|
35 |
|
return $options; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $value |
177
|
|
|
* |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
6 |
|
private function guardOperator(string $value) |
181
|
|
|
{ |
182
|
6 |
|
if ($value !== \ELASTICS_OPERATOR_OR && $value !== \ELASTICS_OPERATOR_AND) { |
183
|
3 |
|
throw new Exception\InvalidOperator($value); |
184
|
|
|
} |
185
|
3 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $value |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
6 |
|
private function guardZeroTerms(string $value) |
193
|
|
|
{ |
194
|
6 |
|
if ($value !== \ELASTICS_ZERO_TERMS_ALL && $value !== \ELASTICS_ZERO_TERMS_NONE) { |
195
|
3 |
|
throw new Exception\InvalidZeroTerms($value); |
196
|
|
|
} |
197
|
3 |
|
} |
198
|
|
|
} |
199
|
|
|
|