summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Query/FunctionScore.php
blob: b11454fb5bedc8a14c398e9664cc9613d1113557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace Elastica\Query;

use Elastica\Filter\AbstractFilter;
use Elastica\Script;

/**
 * Class FunctionScore.
 *
 * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
 */
class FunctionScore extends AbstractQuery
{
    const BOOST_MODE_MULTIPLY = 'multiply';
    const BOOST_MODE_REPLACE = 'replace';
    const BOOST_MODE_SUM = 'sum';
    const BOOST_MODE_AVERAGE = 'average';
    const BOOST_MODE_MAX = 'max';
    const BOOST_MODE_MIN = 'min';

    const SCORE_MODE_MULTIPLY = 'multiply';
    const SCORE_MODE_SUM = 'sum';
    const SCORE_MODE_AVERAGE = 'avg';
    const SCORE_MODE_FIRST = 'first';
    const SCORE_MODE_MAX = 'max';
    const SCORE_MODE_MIN = 'min';

    const DECAY_GAUSS = 'gauss';
    const DECAY_EXPONENTIAL = 'exp';
    const DECAY_LINEAR = 'linear';

    protected $_functions = array();

    /**
     * Set the child query for this function_score query.
     *
     * @param AbstractQuery $query
     *
     * @return $this
     */
    public function setQuery(AbstractQuery $query)
    {
        return $this->setParam('query', $query->toArray());
    }

    /**
     * @param AbstractFilter $filter
     *
     * @return $this
     */
    public function setFilter(AbstractFilter $filter)
    {
        return $this->setParam('filter', $filter->toArray());
    }

    /**
     * Add a function to the function_score query.
     *
     * @param string         $functionType   valid values are DECAY_* constants and script_score
     * @param array|float    $functionParams the body of the function. See documentation for proper syntax.
     * @param AbstractFilter $filter         optional filter to apply to the function
     * @param float          $weight         function weight
     *
     * @return $this
     */
    public function addFunction($functionType, $functionParams, AbstractFilter $filter = null, $weight = null)
    {
        $function = array(
            $functionType => $functionParams,
        );
        if (!is_null($filter)) {
            $function['filter'] = $filter->toArray();
        }
        if ($weight !== null) {
            $function['weight'] = $weight;
        }

        $this->_functions[] = $function;

        return $this;
    }

    /**
     * Add a script_score function to the query.
     *
     * @param Script         $script a Script object
     * @param AbstractFilter $filter an optional filter to apply to the function
     * @param float          $weight the weight of the function
     *
     * @return $this
     */
    public function addScriptScoreFunction(Script $script, AbstractFilter $filter = null, $weight = null)
    {
        return $this->addFunction('script_score', $script->toArray(), $filter, $weight);
    }

    /**
     * Add a decay function to the query.
     *
     * @param string         $function    see DECAY_* constants for valid options
     * @param string         $field       the document field on which to perform the decay function
     * @param string         $origin      the origin value for this decay function
     * @param string         $scale       a scale to define the rate of decay for this function
     * @param string         $offset      If defined, this function will only be computed for documents with a distance from the origin greater than this value
     * @param float          $decay       optionally defines how documents are scored at the distance given by the $scale parameter
     * @param float          $scaleWeight optional factor by which to multiply the score at the value provided by the $scale parameter
     * @param float          $weight      optional factor by which to multiply the score at the value provided by the $scale parameter
     * @param AbstractFilter $filter      a filter associated with this function
     *
     * @return $this
     */
    public function addDecayFunction(
        $function,
        $field,
        $origin,
        $scale,
        $offset = null,
        $decay = null,
        $weight = null,
        AbstractFilter $filter = null
    ) {
        $functionParams = array(
            $field => array(
                'origin' => $origin,
                'scale' => $scale,
            ),
        );
        if (!is_null($offset)) {
            $functionParams[$field]['offset'] = $offset;
        }
        if (!is_null($decay)) {
            $functionParams[$field]['decay'] = (float) $decay;
        }

        return $this->addFunction($function, $functionParams, $filter, $weight);
    }

    /**
     * Add a boost_factor function to the query.
     *
     * @param float          $boostFactor the boost factor value
     * @param AbstractFilter $filter      a filter associated with this function
     *
     * @deprecated
     */
    public function addBoostFactorFunction($boostFactor, AbstractFilter $filter = null)
    {
        $this->addWeightFunction($boostFactor, $filter);
    }

    /**
     * @param float          $weight the weight of the function
     * @param AbstractFilter $filter a filter associated with this function
     */
    public function addWeightFunction($weight, AbstractFilter $filter = null)
    {
        $this->addFunction('weight', $weight, $filter);
    }

    /**
     * Add a random_score function to the query.
     *
     * @param number         $seed   the seed value
     * @param AbstractFilter $filter a filter associated with this function
     * @param float          $weight an optional boost value associated with this function
     */
    public function addRandomScoreFunction($seed, AbstractFilter $filter = null, $weight = null)
    {
        $this->addFunction('random_score', array('seed' => $seed), $filter, $weight);
    }

    /**
     * Set an overall boost value for this query.
     *
     * @param float $boost
     *
     * @return $this
     */
    public function setBoost($boost)
    {
        return $this->setParam('boost', (float) $boost);
    }

    /**
     * Restrict the combined boost of the function_score query and its child query.
     *
     * @param float $maxBoost
     *
     * @return $this
     */
    public function setMaxBoost($maxBoost)
    {
        return $this->setParam('max_boost', (float) $maxBoost);
    }

    /**
     * The boost mode determines how the score of this query is combined with that of the child query.
     *
     * @param string $mode see BOOST_MODE_* constants for valid options. Default is multiply.
     *
     * @return $this
     */
    public function setBoostMode($mode)
    {
        return $this->setParam('boost_mode', $mode);
    }

    /**
     * If set, this query will return results in random order.
     *
     * @param int $seed Set a seed value to return results in the same random order for consistent pagination.
     *
     * @return $this
     */
    public function setRandomScore($seed = null)
    {
        $seedParam = new \stdClass();
        if (!is_null($seed)) {
            $seedParam->seed = $seed;
        }

        return $this->setParam('random_score', $seedParam);
    }

    /**
     * Set the score method.
     *
     * @param string $mode see SCORE_MODE_* constants for valid options. Default is multiply.
     *
     * @return $this
     */
    public function setScoreMode($mode)
    {
        return $this->setParam('score_mode', $mode);
    }

    /**
     * Set min_score option.
     *
     * @param float $minScore
     *
     * @return $this
     */
    public function setMinScore($minScore)
    {
        return $this->setParam('min_score', (float) $minScore);
    }

    /**
     * @return array
     */
    public function toArray()
    {
        if (sizeof($this->_functions)) {
            $this->setParam('functions', $this->_functions);
        }

        return parent::toArray();
    }
}