Elastica\Query\FunctionScore::addFunction PHP Method

addFunction() public method

Add a function to the function_score query.
public addFunction ( string $functionType, array | float $functionParams, AbstractQuery $filter = null, float $weight = null )
$functionType string valid values are DECAY_* constants and script_score
$functionParams array | float the body of the function. See documentation for proper syntax.
$filter AbstractQuery optional filter to apply to the function
$weight float function weight
    public function addFunction($functionType, $functionParams, AbstractQuery $filter = null, $weight = null)
    {
        $function = [$functionType => $functionParams];
        if (!is_null($filter)) {
            $function['filter'] = $filter;
        }
        if ($weight !== null) {
            $function['weight'] = $weight;
        }
        $this->_functions[] = $function;
        return $this;
    }

Usage Example

Example #1
0
 /**
  * @group unit
  */
 public function testAddFunctionWithLegacyFilterDeprecated()
 {
     $this->hideDeprecated();
     $existsFilter = new Exists('test');
     $this->showDeprecated();
     $query = new FunctionScore('test');
     $errorsCollector = $this->startCollectErrors();
     $query->addFunction('f', 1, $existsFilter);
     $this->finishCollectErrors();
     $errorsCollector->assertOnlyDeprecatedErrors(array('Deprecated: Elastica\\Query\\FunctionScore::addFunction passing AbstractFilter is deprecated. Pass AbstractQuery instead.'));
 }
All Usage Examples Of Elastica\Query\FunctionScore::addFunction