Completed
Pull Request — master (#188)
by
unknown
02:31
created

FunctionScoreQuery   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 215
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getType() 0 4 1
A applyFilter() 0 6 2
A addFieldValueFactorFunction() 0 16 1
A addDecayFunction() 0 20 1
A addWeightFunction() 0 12 1
A addRandomFunction() 0 12 2
B addScriptScoreFunction() 0 30 2
A addSimpleFunction() 0 6 1
A toArray() 0 11 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;
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 $lang
172
     * @param string $inline
173
     * @param array $params
174
     * @param BuilderInterface $query
175
     * @return $this
176
     */
177
    public function addScriptScoreFunction(
178
        $lang, 
179
        $inline, 
180
        array $params = [], 
181
        BuilderInterface $query = null
182
    )
183
    {
184
        $notIncludeParams = empty($params);
185
        $function = [
186
            'script_score' => [
187
                'script' => $notIncludeParams ?
188
                    [
189
                        'lang' => $lang,
190
                        'inline' => $inline,
191
                    ]
192
                    :
193
                    [
194
                        'lang' => $lang,
195
                        'inline' => $inline,
196
                        'params' => $params,
197
                    ]
198
            ]
199
        ];
200
201
        $this->applyFilter($function, $query);
202
203
        $this->functions[] = $function;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Adds custom simple function. You can add to the array whatever you want.
210
     *
211
     * @param array $function
212
     *
213
     * @return $this
214
     */
215
    public function addSimpleFunction(array $function)
216
    {
217
        $this->functions[] = $function;
218
219
        return $this;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function toArray()
226
    {
227
        $query = [
228
            'query' => $this->query->toArray(),
229
            'functions' => $this->functions,
230
        ];
231
232
        $output = $this->processArray($query);
233
234
        return [$this->getType() => $output];
235
    }
236
}
237