Skip to content
Snippets Groups Projects
Commit 37b45307 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Merge pull request #247 from saimaz/patch-functionscore

Added objective function addition to function score query
parents 22927b90 8a628499
No related branches found
No related tags found
No related merge requests found
......@@ -24,6 +24,10 @@ class FunctionScoreQuery implements BuilderInterface
use DslTypeAwareTrait;
/**
* Query of filter.
*
* In Function score could be used query or filter. Use setDslType() to change type.
*
* @var BuilderInterface
*/
private $query;
......@@ -35,13 +39,11 @@ class FunctionScoreQuery implements BuilderInterface
/**
* @param BuilderInterface $query
* @param array $functions
* @param array $parameters
*/
public function __construct(BuilderInterface $query, array $functions, array $parameters = [])
public function __construct(BuilderInterface $query, array $parameters = [])
{
$this->query = $query;
$this->functions = $functions;
$this->setParameters($parameters);
$this->setDslType('query');
}
......@@ -54,6 +56,167 @@ class FunctionScoreQuery implements BuilderInterface
return 'function_score';
}
/**
* Modifier to apply filter to the function score function.
*
* @param array $function
* @param BuilderInterface $filter
*/
private function applyFilter(array &$function, BuilderInterface $filter = null)
{
if ($filter) {
$function['filter'] = [
$filter->getType() => $filter->toArray(),
];
}
}
/**
* Creates field_value_factor function.
*
* @param string $field
* @param float $factor
* @param string $modifier
* @param BuilderInterface $filter
*
* @return $this
*/
public function addFieldValueFactorFunction($field, $factor, $modifier, BuilderInterface $filter = null)
{
$function = [
'field_value_factor' => [
'field' => $field,
'factor' => $factor,
'modifier' => $modifier,
],
];
$this->applyFilter($function, $filter);
$this->functions[] = $function;
return $this;
}
/**
* Add decay function to function score. Weight and filter are optional.
*
* @param string $type
* @param string $field
* @param array $function
* @param array $options
* @param BuilderInterface $filter
*
* @return $this
*/
public function addDecayFunction(
$type,
$field,
array $function,
array $options = [],
BuilderInterface $filter = null
) {
$function = [
$type => [
$field => $function,
],
$options
];
$this->applyFilter($function, $filter);
$this->functions[] = $function;
return $this;
}
/**
* Adds function to function score without decay function. Influence search score only for specific filter.
*
* @param float $weight
* @param BuilderInterface $filter
*
* @return $this
*/
public function addWeightFunction($weight, BuilderInterface $filter = null)
{
$function = [
'weight' => $weight,
];
$this->applyFilter($function, $filter);
$this->functions[] = $function;
return $this;
}
/**
* Adds random score function. Seed is optional.
*
* @param mixed $seed
* @param BuilderInterface $filter
*
* @return $this
*/
public function addRandomFunction($seed = null, BuilderInterface $filter = null)
{
$function = [
'random_score' => $seed ? [ 'seed' => $seed ] : [],
];
$this->applyFilter($function, $filter);
$this->functions[] = $function;
return $this;
}
/**
* Adds script score function.
*
* @param string $script
* @param array $params
* @param array $options
* @param BuilderInterface $filter
*
* @return $this
*/
public function addScriptScoreFunction(
$script,
array $params = [],
array $options = [],
BuilderInterface $filter = null
) {
$function = [
'script_score' => [
'script' => $script,
'params' => $params,
$options
],
];
$this->applyFilter($function, $filter);
$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}
*/
......@@ -63,7 +226,7 @@ class FunctionScoreQuery implements BuilderInterface
strtolower($this->getDslType()) => [
$this->query->getType() => $this->query->toArray(),
],
'functions' => [$this->functions],
'functions' => $this->functions,
];
$output = $this->processArray($query);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment