Completed
Push — develop ( cc393a...0ae354 )
by Sam
12s
created

MatchQuery::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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