Completed
Push — master ( 6c9334...f3ba17 )
by Simonas
02:26
created

FunctionScoreQuery::addSimpleFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
     * {@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
        $function = [
185
            'script_score' => array_merge(
186
                [
187
                    'script' => $script,
188
                    'params' => $params,
189
                ],
190
                $options
191
            ),
192
        ];
193
194
        $this->applyFilter($function, $query);
195
196
        $this->functions[] = $function;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Adds custom simple function. You can add to the array whatever you want.
203
     *
204
     * @param array $function
205
     *
206
     * @return $this
207
     */
208
    public function addSimpleFunction(array $function)
209
    {
210
        $this->functions[] = $function;
211
212
        return $this;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function toArray()
219
    {
220
        $query = [
221
            'query' => $this->query->toArray(),
222
            'functions' => $this->functions,
223
        ];
224
225
        $output = $this->processArray($query);
226
227
        return [$this->getType() => $output];
228
    }
229
}
230