FunctionScoreQuery::addScriptScoreFunction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Query\Compound;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Represents Elasticsearch "function_score" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
21
 */
22
class FunctionScoreQuery implements BuilderInterface
23
{
24
    use ParametersTrait;
25
26
    /**
27
     * @var BuilderInterface
28
     */
29
    private $query;
30
31
    /**
32
     * @var array[]
33
     */
34
    private $functions;
35
36
    /**
37
     * @param BuilderInterface $query
38
     * @param array            $parameters
39
     */
40
    public function __construct(BuilderInterface $query, array $parameters = [])
41
    {
42
        $this->query = $query;
43
        $this->setParameters($parameters);
44
    }
45
46
    /**
47
     * Returns the query instance.
48
     *
49
     * @return BuilderInterface object
50
     */
51
    public function getQuery()
52
    {
53
        return $this->query;
54
    }
55
56
    /**
57
     * Creates field_value_factor function.
58
     *
59
     * @param string           $field
60
     * @param float            $factor
61
     * @param string           $modifier
62
     * @param BuilderInterface $query
63
     * @param mixed            $missing
64
     * @return $this
65
     */
66
    public function addFieldValueFactorFunction(
67
        $field,
68
        $factor,
69
        $modifier = 'none',
70
        BuilderInterface $query = null,
71
        $missing = null
72
    ) {
73
        $function = [
74
            'field_value_factor' => array_filter([
75
                'field' => $field,
76
                'factor' => $factor,
77
                'modifier' => $modifier,
78
                'missing' => $missing
79
            ]),
80
        ];
81
82
        $this->applyFilter($function, $query);
83
84
        $this->functions[] = $function;
85
86
        return $this;
87
    }
88
89
    /**
90
     * Modifier to apply filter to the function score function.
91
     *
92
     * @param array            $function
93
     * @param BuilderInterface $query
94
     */
95
    private function applyFilter(array &$function, BuilderInterface $query = null)
96
    {
97
        if ($query) {
98
            $function['filter'] = $query->toArray();
99
        }
100
    }
101
102
    /**
103
     * Add decay function to function score. Weight and query are optional.
104
     *
105
     * @param string           $type
106
     * @param string           $field
107
     * @param array            $function
108
     * @param array            $options
109
     * @param BuilderInterface $query
110
     * @param int              $weight
111
     *
112
     * @return $this
113
     */
114
    public function addDecayFunction(
115
        $type,
116
        $field,
117
        array $function,
118
        array $options = [],
119
        BuilderInterface $query = null,
120
        $weight = null
121
    ) {
122
        $function = array_filter(
123
            [
124
                $type => array_merge(
125
                    [$field => $function],
126
                    $options
127
                ),
128
                'weight' => $weight,
129
            ]
130
        );
131
132
        $this->applyFilter($function, $query);
133
134
        $this->functions[] = $function;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Adds function to function score without decay function. Influence search score only for specific query.
141
     *
142
     * @param float            $weight
143
     * @param BuilderInterface $query
144
     *
145
     * @return $this
146
     */
147
    public function addWeightFunction($weight, BuilderInterface $query = null)
148
    {
149
        $function = [
150
            'weight' => $weight,
151
        ];
152
153
        $this->applyFilter($function, $query);
154
155
        $this->functions[] = $function;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Adds random score function. Seed is optional.
162
     *
163
     * @param mixed            $seed
164
     * @param BuilderInterface $query
165
     *
166
     * @return $this
167
     */
168
    public function addRandomFunction($seed = null, BuilderInterface $query = null)
169
    {
170
        $function = [
171
            'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(),
172
        ];
173
174
        $this->applyFilter($function, $query);
175
176
        $this->functions[] = $function;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Adds script score function.
183
     *
184
     * @param string           $inline
185
     * @param array            $params
186
     * @param array            $options
187
     * @param BuilderInterface $query
188
     *
189
     * @return $this
190
     */
191
    public function addScriptScoreFunction(
192
        $inline,
193
        array $params = [],
194
        array $options = [],
195
        BuilderInterface $query = null
196
    ) {
197
        $function = [
198
            'script_score' => [
199
                'script' =>
200
                    array_filter(
201
                        array_merge(
202
                            [
203
                                'lang' => 'painless',
204
                                'inline' => $inline,
205
                                'params' => $params
206
                            ],
207
                            $options
208
                        )
209
                    )
210
            ],
211
        ];
212
213
        $this->applyFilter($function, $query);
214
        $this->functions[] = $function;
215
216
        return $this;
217
    }
218
219
    /**
220
     * Adds custom simple function. You can add to the array whatever you want.
221
     *
222
     * @param array $function
223
     *
224
     * @return $this
225
     */
226
    public function addSimpleFunction(array $function)
227
    {
228
        $this->functions[] = $function;
229
230
        return $this;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function toArray()
237
    {
238
        $query = [
239
            'query' => $this->query->toArray(),
240
            'functions' => $this->functions,
241
        ];
242
243
        $output = $this->processArray($query);
244
245
        return [$this->getType() => $output];
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function getType()
252
    {
253
        return 'function_score';
254
    }
255
}
256