Completed
Push — master ( 39ecb4...66dc2f )
by Nicolas
03:00
created

MatchQuery::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
namespace Elastica\Query;
4
5
/**
6
 * Match query.
7
 *
8
 * @author F21
9
 * @author WONG Wing Lun <[email protected]>
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
    public const OPERATOR_OR = 'or';
16
    public const OPERATOR_AND = 'and';
17
18
    public const ZERO_TERM_NONE = 'none';
19
    public const ZERO_TERM_ALL = 'all';
20
21
    public const FUZZINESS_AUTO = 'AUTO';
22
23
    /**
24
     * @param string $field
25
     * @param mixed  $values
26
     */
27
    public function __construct(?string $field = null, $values = null)
28
    {
29
        if (null !== $field && null !== $values) {
30
            $this->setParam($field, $values);
31
        }
32
    }
33
34
    /**
35
     * Sets a param for the message array.
36
     *
37
     * @param mixed $values
38
     *
39
     * @return $this
40
     */
41
    public function setField(string $field, $values): self
42
    {
43
        return $this->setParam($field, $values);
44
    }
45
46
    /**
47
     * Sets a param for the given field.
48
     *
49
     * @return $this
50
     */
51
    public function setFieldParam(string $field, string $key, string $value): self
52
    {
53
        if (!isset($this->_params[$field])) {
54
            $this->_params[$field] = [];
55
        }
56
57
        $this->_params[$field][$key] = $value;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Sets the query string.
64
     *
65
     * @return $this
66
     */
67
    public function setFieldQuery(string $field, string $query): self
68
    {
69
        return $this->setFieldParam($field, 'query', $query);
70
    }
71
72
    /**
73
     * Set field operator.
74
     *
75
     * @return $this
76
     */
77
    public function setFieldOperator(string $field, string $operator = self::OPERATOR_OR): self
78
    {
79
        return $this->setFieldParam($field, 'operator', $operator);
80
    }
81
82
    /**
83
     * Set field analyzer.
84
     *
85
     * @return $this
86
     */
87
    public function setFieldAnalyzer(string $field, string $analyzer): self
88
    {
89
        return $this->setFieldParam($field, 'analyzer', $analyzer);
90
    }
91
92
    /**
93
     * Set field boost value.
94
     *
95
     * If not set, defaults to 1.0.
96
     *
97
     * @return $this
98
     */
99
    public function setFieldBoost(string $field, float $boost = 1.0): self
100
    {
101
        return $this->setFieldParam($field, 'boost', $boost);
102
    }
103
104
    /**
105
     * Set field minimum should match.
106
     *
107
     * @param int|string $minimumShouldMatch
108
     *
109
     * @return $this
110
     *
111
     * @see Possible values for minimum_should_match https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html
112
     */
113
    public function setFieldMinimumShouldMatch(string $field, $minimumShouldMatch): self
114
    {
115
        return $this->setFieldParam($field, 'minimum_should_match', $minimumShouldMatch);
116
    }
117
118
    /**
119
     * Set field fuzziness.
120
     *
121
     * @param mixed $fuzziness
122
     *
123
     * @return $this
124
     */
125
    public function setFieldFuzziness(string $field, $fuzziness): self
126
    {
127
        return $this->setFieldParam($field, 'fuzziness', $fuzziness);
128
    }
129
130
    /**
131
     * Set field fuzzy rewrite.
132
     *
133
     * @return $this
134
     */
135
    public function setFieldFuzzyRewrite(string $field, string $fuzzyRewrite): self
136
    {
137
        return $this->setFieldParam($field, 'fuzzy_rewrite', $fuzzyRewrite);
138
    }
139
140
    /**
141
     * Set field prefix length.
142
     *
143
     * @return $this
144
     */
145
    public function setFieldPrefixLength(string $field, int $prefixLength): self
146
    {
147
        return $this->setFieldParam($field, 'prefix_length', $prefixLength);
148
    }
149
150
    /**
151
     * Set field max expansions.
152
     *
153
     * @return $this
154
     */
155
    public function setFieldMaxExpansions(string $field, int $maxExpansions): self
156
    {
157
        return $this->setFieldParam($field, 'max_expansions', $maxExpansions);
158
    }
159
160
    /**
161
     * Set zero terms query.
162
     *
163
     * If not set, default to 'none'
164
     *
165
     * @return $this
166
     */
167
    public function setFieldZeroTermsQuery(string $field, string $zeroTermQuery = self::ZERO_TERM_NONE): self
168
    {
169
        return $this->setFieldParam($field, 'zero_terms_query', $zeroTermQuery);
170
    }
171
172
    /**
173
     * Set cutoff frequency.
174
     *
175
     * @return $this
176
     */
177
    public function setFieldCutoffFrequency(string $field, float $cutoffFrequency): self
178
    {
179
        return $this->setFieldParam($field, 'cutoff_frequency', $cutoffFrequency);
180
    }
181
}
182