Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Query\Compound;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Represents Elasticsearch "function_score" query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
*/
class FunctionScoreQuery implements BuilderInterface
{
use ParametersTrait;
/**
* @var BuilderInterface
*/
private $query;
/**
* @var array[]
*/
private $functions;
/**
* @param BuilderInterface $query
* @param array $parameters
*/
public function __construct(BuilderInterface $query, array $parameters = [])
{
$this->query = $query;
$this->setParameters($parameters);
}
/**
* Creates field_value_factor function.
*
* @param string $field
* @param float $factor
* @param string $modifier
* @param BuilderInterface $query
Luiz Guilherme Fonseca Rosa
committed
* @param mixed $missing
Luiz Guilherme Fonseca Rosa
committed
public function addFieldValueFactorFunction($field, $factor, $modifier = 'none', BuilderInterface $query = null, $missing = null)
Luiz Guilherme Fonseca Rosa
committed
$function = array_filter([
'field_value_factor' => [
'field' => $field,
'factor' => $factor,
'modifier' => $modifier,
Luiz Guilherme Fonseca Rosa
committed
'missing' => $missing
Luiz Guilherme Fonseca Rosa
committed
], function ($item) {
return $item !== null;
});
$this->applyFilter($function, $query);
$this->functions[] = $function;
return $this;
}
/**
* Modifier to apply filter to the function score function.
*
* @param array $function
* @param BuilderInterface $query
*/
private function applyFilter(array &$function, BuilderInterface $query = null)
{
if ($query) {
$function['filter'] = $query->toArray();
}
}
/**
* Add decay function to function score. Weight and query are optional.
*
* @param string $type
* @param string $field
* @param array $function
* @param array $options
* @param BuilderInterface $query
* @param int $weight
*
* @return $this
*/
public function addDecayFunction(
$type,
$field,
array $function,
array $options = [],
BuilderInterface $query = null,
$weight = null
$function = array_filter(
[
$type => array_merge(
[$field => $function],
$options
),
'weight' => $weight,
]
);
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
$this->applyFilter($function, $query);
$this->functions[] = $function;
return $this;
}
/**
* Adds function to function score without decay function. Influence search score only for specific query.
*
* @param float $weight
* @param BuilderInterface $query
*
* @return $this
*/
public function addWeightFunction($weight, BuilderInterface $query = null)
{
$function = [
'weight' => $weight,
];
$this->applyFilter($function, $query);
$this->functions[] = $function;
return $this;
}
/**
* Adds random score function. Seed is optional.
*
* @param mixed $seed
* @param BuilderInterface $query
*
* @return $this
*/
public function addRandomFunction($seed = null, BuilderInterface $query = null)
{
$function = [
'random_score' => $seed ? [ 'seed' => $seed ] : new \stdClass(),
];
$this->applyFilter($function, $query);
$this->functions[] = $function;
return $this;
}
/**
* Adds script score function.
*
* @param array $params
* @param array $options
* @param BuilderInterface $query
*
* @return $this
*/
public function addScriptScoreFunction(
array $params = [],
array $options = [],
BuilderInterface $query = null
) {
$function = [
'script_score' => [
'script' =>
array_filter(
array_merge(
[
'lang' => 'painless',
'inline' => $inline,
'params' => $params
],
$options
)
)
],
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
];
$this->applyFilter($function, $query);
$this->functions[] = $function;
return $this;
}
/**
* Adds custom simple function. You can add to the array whatever you want.
*
* @param array $function
*
* @return $this
*/
public function addSimpleFunction(array $function)
{
$this->functions[] = $function;
return $this;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$query = [
'query' => $this->query->toArray(),
'functions' => $this->functions,
];
$output = $this->processArray($query);
return [$this->getType() => $output];
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'function_score';
}