Completed
Push — develop ( 202fdb...21b6ae )
by Sam
11s
created

FunctionScoreQuery::hasBoostMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Query\Compound;
2
3
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasBoost;
4
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasQuery;
5
use Nord\Lumen\Elasticsearch\Search\Query\Traits\HasScoreMode;
6
use Nord\Lumen\Elasticsearch\Search\Scoring\Functions\AbstractScoringFunction;
7
8
/**
9
 * The function_score allows you to modify the score of documents that are retrieved by a query. This can be useful if,
10
 * for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered
11
 * set of documents.
12
 *
13
 * To use function_score, the user has to define a query and one or more functions, that compute a new score for each
14
 * document returned by the query.
15
 *
16
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
17
 */
18
class FunctionScoreQuery extends AbstractQuery
19
{
20
    use HasBoost;
21
    use HasQuery;
22
    use HasScoreMode;
23
24
    /**
25
     * @var AbstractScoringFunction[]
26
     */
27
    private $functions = [];
28
29
    /**
30
     * @var float|null
31
     */
32
    private $weight;
33
34
    /**
35
     * @var float|null
36
     */
37
    private $maxBoost;
38
39
    /**
40
     * @var string|null
41
     */
42
    private $boostMode;
43
44
    /**
45
     * @var int|float|null
46
     */
47
    private $minScore;
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function toArray()
53
    {
54
        $array = [];
55
56
        $query = $this->getQuery();
57
        if (!empty($query)) {
58
            $queryArray = $query->toArray();
59
            if (!empty($queryArray)) {
60
                $array['query'] = $queryArray;
61
            } else {
62
                $array['query'] = ['match_all' => []];
63
            }
64
        }
65
66
        $functions = [];
67
        foreach ($this->getFunctions() as $function) {
68
            $functions[] = $function->toArray();
69
        }
70
71
        if (!empty($functions)) {
72
            $array['functions'] = $functions;
73
        }
74
75
        $scoreMode = $this->getScoreMode();
76
        if (!empty($scoreMode)) {
77
            $array['score_mode'] = $scoreMode;
78
        }
79
80
        if ($this->hasWeight()) {
81
            $array['weight'] = $this->getWeight();
82
        }
83
84
        if ($this->hasBoost()) {
85
            $array['boost'] = $this->getBoost();
86
        }
87
88
        if ($this->hasMaxBoost()) {
89
            $array['max_boost'] = $this->getMaxBoost();
90
        }
91
92
        if ($this->hasBoostMode()) {
93
            $array['boost_mode'] = $this->getBoostMode();
94
        }
95
96
        if ($this->hasMinScore()) {
97
            $array['min_score'] = $this->getMinScore();
98
        }
99
100
        return ['function_score' => $array];
101
    }
102
103
    /**
104
     * @return AbstractScoringFunction[]
105
     */
106
    public function getFunctions()
107
    {
108
        return $this->functions;
109
    }
110
111
112
    /**
113
     * @param AbstractScoringFunction[] $functions
114
     * @return FunctionScoreQuery
115
     */
116
    public function setFunctions(array $functions)
117
    {
118
        $this->functions = [];
119
        foreach ($functions as $function) {
120
            $this->addFunction($function);
121
        }
122
        return $this;
123
    }
124
125
126
    /**
127
     * @param AbstractScoringFunction $function
128
     * @return FunctionScoreQuery
129
     */
130
    public function addFunction(AbstractScoringFunction $function)
131
    {
132
        $this->functions[] = $function;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return float|null
139
     */
140
    public function getWeight()
141
    {
142
        return $this->weight;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function hasWeight()
149
    {
150
        return $this->getWeight() !== null;
151
    }
152
153
    /**
154
     * @param float $weight
155
     *
156
     * @return FunctionScoreQuery
157
     */
158
    public function setWeight($weight)
159
    {
160
        $this->weight = $weight;
161
162
        return $this;
163
    }
164
165
    /**
166
     * @return float|null
167
     */
168
    public function getMaxBoost()
169
    {
170
        return $this->maxBoost;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function hasMaxBoost()
177
    {
178
        return $this->getMaxBoost() !== null;
179
    }
180
181
    /**
182
     * @param float $maxBoost
183
     *
184
     * @return FunctionScoreQuery
185
     */
186
    public function setMaxBoost($maxBoost)
187
    {
188
        $this->maxBoost = $maxBoost;
189
190
        return $this;
191
    }
192
193
    /**
194
     * @return null|string
195
     */
196
    public function getBoostMode()
197
    {
198
        return $this->boostMode;
199
    }
200
201
    /**
202
     * @return bool
203
     */
204
    public function hasBoostMode()
205
    {
206
        return $this->getBoostMode() !== null;
207
    }
208
209
    /**
210
     * @param string $boostMode
211
     *
212
     * @return FunctionScoreQuery
213
     */
214
    public function setBoostMode($boostMode)
215
    {
216
        $this->boostMode = $boostMode;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return int|float|null
223
     */
224
    public function getMinScore()
225
    {
226
        return $this->minScore;
227
    }
228
229
    /**
230
     * @return bool
231
     */
232
    public function hasMinScore()
233
    {
234
        return $this->getMinScore() !== null;
235
    }
236
237
    /**
238
     * @param int|float $minScore
239
     *
240
     * @return FunctionScoreQuery
241
     */
242
    public function setMinScore($minScore)
243
    {
244
        $this->minScore = $minScore;
245
246
        return $this;
247
    }
248
}
249