Completed
Push — master ( 2259f6...245ec1 )
by Simonas
02:50
created

FunctionScoreQuery::addScriptScoreFunction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
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;
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
     * {@inheritdoc}
48
     */
49
    public function getType()
50
    {
51
        return 'function_score';
52
    }
53
54
    /**
55
     * Modifier to apply filter to the function score function.
56
     *
57
     * @param array            $function
58
     * @param BuilderInterface $query
59
     */
60
    private function applyFilter(array &$function, BuilderInterface $query = null)
61
    {
62
        if ($query) {
63
            $function['filter'] = $query->toArray();
64
        }
65
    }
66
67
    /**
68
     * Creates field_value_factor function.
69
     *
70
     * @param string           $field
71
     * @param float            $factor
72
     * @param string           $modifier
73
     * @param BuilderInterface $query
74
     *
75
     * @return $this
76
     */
77
    public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $query = null)
78
    {
79
        $function = [
80
            'field_value_factor' => [
81
                'field' => $field,
82
                'factor' => $factor,
83
                'modifier' => $modifier,
84
            ],
85
        ];
86
87
        $this->applyFilter($function, $query);
88
89
        $this->functions[] = $function;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Add decay function to function score. Weight and query are optional.
96
     *
97
     * @param string           $type
98
     * @param string           $field
99
     * @param array            $function
100
     * @param array            $options
101
     * @param BuilderInterface $query
102
     *
103
     * @return $this
104
     */
105
    public function addDecayFunction(
106
        $type,
107
        $field,
108
        array $function,
109
        array $options = [],
110
        BuilderInterface $query = null
111
    ) {
112
        $function = [
113
            $type => array_merge(
114
                [$field => $function],
115
                $options
116
            ),
117
        ];
118
119
        $this->applyFilter($function, $query);
120
121
        $this->functions[] = $function;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Adds function to function score without decay function. Influence search score only for specific query.
128
     *
129
     * @param float            $weight
130
     * @param BuilderInterface $query
131
     *
132
     * @return $this
133
     */
134
    public function addWeightFunction($weight, BuilderInterface $query = null)
135
    {
136
        $function = [
137
            'weight' => $weight,
138
        ];
139
140
        $this->applyFilter($function, $query);
141
142
        $this->functions[] = $function;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Adds random score function. Seed is optional.
149
     *
150
     * @param mixed            $seed
151
     * @param BuilderInterface $query
152
     *
153
     * @return $this
154
     */
155
    public function addRandomFunction($seed = null, BuilderInterface $query = null)
156
    {
157
        $function = [
158
            'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(),
159
        ];
160
161
        $this->applyFilter($function, $query);
162
163
        $this->functions[] = $function;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Adds script score function.
170
     *
171
     * @param string           $script
172
     * @param array            $params
173
     * @param array            $options
174
     * @param BuilderInterface $query
175
     *
176
     * @return $this
177
     */
178
    public function addScriptScoreFunction(
179
        $script,
180
        array $params = [],
181
        array $options = [],
182
        BuilderInterface $query = null
183
    ) {
184
        if (count($params) > 0) {
185
            $options = array_merge(
186
                ['params' => $params],
187
                $options
188
            );
189
        }
190
191
        $function = [
192
            'script_score' => [
193
                'script' => array_merge(
194
                    [
195
                        'lang' => 'painless',
196
                        'inline' => $script
197
                    ],
198
                    $options
199
                )
200
            ]
201
        ];
202
203
        $this->applyFilter($function, $query);
204
205
        $this->functions[] = $function;
206
207
        return $this;
208
    }
209
210
    /**
211
     * Adds custom simple function. You can add to the array whatever you want.
212
     *
213
     * @param array $function
214
     *
215
     * @return $this
216
     */
217
    public function addSimpleFunction(array $function)
218
    {
219
        $this->functions[] = $function;
220
221
        return $this;
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function toArray()
228
    {
229
        $query = [
230
            'query' => $this->query->toArray(),
231
            'functions' => $this->functions,
232
        ];
233
234
        $output = $this->processArray($query);
235
236
        return [$this->getType() => $output];
237
    }
238
}
239