Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LogMultFunctionScoreBuilder
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 append
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Search\Rescore;
4
5use CirrusSearch\SearchConfig;
6use Elastica\Query\FunctionScore;
7
8/**
9 * simple log(factor*field+2)^impact
10 * Useful to control the impact when applied in a multiplication.
11 */
12class LogMultFunctionScoreBuilder extends FunctionScoreBuilder {
13    /** @var float */
14    private $impact;
15    /** @var float */
16    private $factor;
17    /** @var string */
18    private $field;
19
20    /**
21     * @param SearchConfig $config
22     * @param float $weight
23     * @param array $profile
24     * @throws InvalidRescoreProfileException
25     */
26    public function __construct( SearchConfig $config, $weight, $profile ) {
27        parent::__construct( $config, $weight );
28        if ( isset( $profile['impact'] ) ) {
29            $this->impact = $this->getOverriddenFactor( $profile['impact'] );
30            if ( $this->impact <= 0 ) {
31                throw new InvalidRescoreProfileException( 'Param impact must be > 0' );
32            }
33        } else {
34            throw new InvalidRescoreProfileException( 'Param impact is mandatory' );
35        }
36
37        if ( isset( $profile['factor'] ) ) {
38            $this->factor = $this->getOverriddenFactor( $profile['factor'] );
39            if ( $this->factor <= 0 ) {
40                throw new InvalidRescoreProfileException( 'Param factor must be > 0' );
41            }
42        } else {
43            $this->factor = 1;
44        }
45
46        if ( isset( $profile['field'] ) ) {
47            $this->field = $profile['field'];
48        } else {
49            throw new InvalidRescoreProfileException( 'Param field is mandatory' );
50        }
51    }
52
53    public function append( FunctionScore $functionScore ) {
54        $formula = "pow(log10({$this->factor} * doc['{$this->field}'].value + 2), {$this->impact})";
55        $functionScore->addScriptScoreFunction( new \Elastica\Script\Script( $formula, null,
56            'expression' ), null, $this->weight );
57    }
58}