Completed
Push — master ( 766668...5ee3f6 )
by Mantas
08:49 queued 02:56
created

FunctionScoreQuery::applyQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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