Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
BoostTemplatesFunctionScoreBuilder
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
2 / 2
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
1 / 1
10
 append
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Search\Rescore;
4
5use CirrusSearch\OtherIndexesUpdater;
6use CirrusSearch\SearchConfig;
7use CirrusSearch\Util;
8use Elastica\Query\FunctionScore;
9
10/**
11 * Builds a set of functions with boosted templates
12 * Uses a weight function with a filter for each template.
13 * The list of boosted templates is read from SearchContext
14 */
15class BoostTemplatesFunctionScoreBuilder extends FunctionScoreBuilder {
16
17    /**
18     * @var BoostedQueriesFunction
19     */
20    private $boostedQueries;
21
22    /**
23     * @param SearchConfig $config
24     * @param int[]|null $requestedNamespaces
25     * @param bool $localSearch
26     * @param bool $withDefaultBoosts false to disable the use of default boost templates
27     * @param float $weight
28     */
29    public function __construct( SearchConfig $config, $requestedNamespaces, $localSearch, $withDefaultBoosts, $weight ) {
30        parent::__construct( $config, $weight );
31        // Use the boosted templates from extra indexes if available
32        $queries = [];
33        $weights = [];
34        if ( $withDefaultBoosts ) {
35            $boostTemplates = Util::getDefaultBoostTemplates( $config );
36            if ( $boostTemplates ) {
37                foreach ( $boostTemplates as $name => $boostWeight ) {
38                    $match = new \Elastica\Query\MatchQuery();
39                    $match->setFieldQuery( 'template', $name );
40                    $weights[] = $boostWeight * $this->weight;
41                    $queries[] = $match;
42                }
43            }
44        }
45
46        $otherIndices = [];
47        if ( $requestedNamespaces && !$localSearch ) {
48            $otherIndices = OtherIndexesUpdater::getExtraIndexesForNamespaces(
49                $config,
50                $requestedNamespaces
51            );
52        }
53
54        $extraIndexBoostTemplates = [];
55        foreach ( $otherIndices as $extraIndex ) {
56            [ $wiki, $boostTemplates ] = $extraIndex->getBoosts();
57            if ( $boostTemplates ) {
58                $extraIndexBoostTemplates[$wiki] = $boostTemplates;
59            }
60        }
61
62        foreach ( $extraIndexBoostTemplates as $wiki => $boostTemplates ) {
63            foreach ( $boostTemplates as $name => $boostWeight ) {
64                $bool = new \Elastica\Query\BoolQuery();
65                $bool->addMust( ( new \Elastica\Query\MatchQuery() )->setFieldQuery( 'wiki', $wiki ) );
66                $bool->addMust( ( new \Elastica\Query\MatchQuery() )->setFieldQuery( 'template',
67                    $name ) );
68                $weights[] = $boostWeight * $this->weight;
69                $queries[] = $bool;
70            }
71        }
72        $this->boostedQueries = new BoostedQueriesFunction( $queries, $weights );
73    }
74
75    public function append( FunctionScore $functionScore ) {
76        $this->boostedQueries->append( $functionScore );
77    }
78}