Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikibasePrefixSearcher
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 buildSearch
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
20
 performSearch
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 addWarning
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Wikibase\Search\Elastic;
4
5use CirrusSearch\CirrusDebugOptions;
6use CirrusSearch\Connection;
7use CirrusSearch\SearchConfig;
8use CirrusSearch\Searcher;
9use Elastica\Query;
10use Elastica\Query\AbstractQuery;
11use MediaWiki\MediaWikiServices;
12use MediaWiki\Status\Status;
13
14/**
15 * Searcher class for performing Wikibase prefix search.
16 * @see \CirrusSearch\Searcher
17 *
18 * @license GPL-2.0-or-later
19 * @author Stas Malyshev
20 */
21class WikibasePrefixSearcher extends Searcher {
22    /**
23     * @var AbstractQuery
24     */
25    private $query;
26
27    /**
28     * @param int $offset Search offset.
29     * @param int $limit Search limit.
30     * @param CirrusDebugOptions|null $options
31     */
32    public function __construct( $offset, $limit, CirrusDebugOptions $options = null ) {
33        $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'CirrusSearch' );
34        /** @var SearchConfig $config */
35        '@phan-var SearchConfig $config';
36        $connection = new Connection( $config );
37        parent::__construct( $connection, $offset, $limit, $config, null, null, false, $options );
38    }
39
40    /**
41     * Build search query object.
42     * @return \Elastica\Search
43     */
44    protected function buildSearch() {
45        $this->searchContext->addSyntaxUsed( 'wikibase_prefix', PHP_INT_MAX );
46
47        $indexSuffix = $this->connection->pickIndexSuffixForNamespaces( $this->getSearchContext()->getNamespaces() );
48        $index = $this->connection->getIndex( $this->indexBaseName, $indexSuffix );
49
50        $queryOptions = [
51            \Elastica\Search::OPTION_TIMEOUT => $this->config->getElement( 'CirrusSearchSearchShardTimeout',
52                'default' ),
53        ];
54        $searchQuery = new Query();
55        $searchQuery->setQuery( $this->query );
56        $resultsType = $this->searchContext->getResultsType();
57        $searchQuery->setSource( $resultsType->getSourceFiltering() );
58        $searchQuery->setParam( 'fields', $resultsType->getFields() );
59
60        $highlight = $this->searchContext->getHighlight( $resultsType, $this->query );
61        if ( $highlight ) {
62            $searchQuery->setHighlight( $highlight );
63        }
64        if ( $this->offset ) {
65            $searchQuery->setFrom( $this->offset );
66        }
67        if ( $this->limit ) {
68            $searchQuery->setSize( $this->limit );
69        }
70        $searchQuery->setParam( 'rescore', $this->searchContext->getRescore() );
71        // Mark wikibase prefix searches for statistics
72        $searchQuery->addParam( 'stats', 'wikibase-prefix' );
73        $this->applyDebugOptionsToQuery( $searchQuery );
74        return $index->createSearch( $searchQuery, $queryOptions );
75    }
76
77    /**
78     * Perform prefix search for Wikibase entities.
79     * @param AbstractQuery $query Search query.
80     * @return Status
81     */
82    public function performSearch( AbstractQuery $query ) {
83        $this->query = $query;
84        $status = $this->searchOne();
85
86        // TODO: this probably needs to go to Searcher API.
87        foreach ( $this->searchContext->getWarnings() as $warning ) {
88            // $warning is a parameter array
89            call_user_func_array( [ $status, 'warning' ], $warning );
90        }
91
92        return $status;
93    }
94
95    /**
96     * Add warning message about something in search.
97     * @param string $message i18n message key
98     */
99    public function addWarning( $message /*, parameters... */ ) {
100        call_user_func_array( [ $this->searchContext, 'addWarning' ], func_get_args() );
101    }
102
103}