Completed
Push — master ( 76754f...6b6a70 )
by Simonas
03:49 queued 02:25
created

FunctionScoreQuery::getQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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