Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| TermBoostScoreBuilder | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| append | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Search\Rescore; |
| 4 | |
| 5 | use CirrusSearch\SearchConfig; |
| 6 | use Elastica\Query\FunctionScore; |
| 7 | use Elastica\Query\MatchQuery; |
| 8 | |
| 9 | /** |
| 10 | * Boost score when certain field is matched with certain term. |
| 11 | * Config: |
| 12 | * [ 'field_name' => ['match1' => WEIGHT1, ...], ...] |
| 13 | * @package CirrusSearch\Search |
| 14 | */ |
| 15 | class TermBoostScoreBuilder extends FunctionScoreBuilder { |
| 16 | /** @var BoostedQueriesFunction */ |
| 17 | private $boostedQueries; |
| 18 | |
| 19 | /** |
| 20 | * @param SearchConfig $config |
| 21 | * @param float $weight |
| 22 | * @param array $profile |
| 23 | */ |
| 24 | public function __construct( $config, $weight, $profile ) { |
| 25 | parent::__construct( $config, $weight ); |
| 26 | $queries = []; |
| 27 | $weights = []; |
| 28 | foreach ( $profile as $field => $matches ) { |
| 29 | foreach ( $matches as $match => $matchWeight ) { |
| 30 | $queries[] = new MatchQuery( $field, $match ); |
| 31 | $weights[] = $matchWeight * $this->weight; |
| 32 | } |
| 33 | } |
| 34 | $this->boostedQueries = new BoostedQueriesFunction( $queries, $weights ); |
| 35 | } |
| 36 | |
| 37 | public function append( FunctionScore $functionScore ) { |
| 38 | $this->boostedQueries->append( $functionScore ); |
| 39 | } |
| 40 | } |