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