Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| EntitySchemaElasticSearchHelper | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getElasticSearchQuery | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
| getRankedSearchResults | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace EntitySchema\Wikibase\Search; |
| 6 | |
| 7 | use CirrusSearch\Search\SearchContext; |
| 8 | use Elastica\Query\AbstractQuery; |
| 9 | use EntitySchema\Domain\Model\EntitySchemaId; |
| 10 | use EntitySchema\MediaWiki\Content\EntitySchemaContent; |
| 11 | use InvalidArgumentException; |
| 12 | use MediaWiki\Title\TitleFactory; |
| 13 | use Wikibase\Lib\LanguageFallbackChainFactory; |
| 14 | use Wikibase\Repo\Api\EntitySearchException; |
| 15 | use Wikibase\Repo\Api\EntitySearchHelper; |
| 16 | use Wikibase\Search\Elastic\EntitySearchElastic; |
| 17 | use Wikibase\Search\Elastic\Query\LabelsCompletionQuery; |
| 18 | use Wikibase\Search\Elastic\WikibaseEntitySearcher; |
| 19 | |
| 20 | /** |
| 21 | * @license GPL-2.0-or-later |
| 22 | */ |
| 23 | class EntitySchemaElasticSearchHelper implements EntitySearchHelper { |
| 24 | |
| 25 | private TitleFactory $titleFactory; |
| 26 | private LanguageFallbackChainFactory $languageFallbackChainFactory; |
| 27 | private string $wikibaseConceptBaseUri; |
| 28 | private string $userLanguageCode; |
| 29 | |
| 30 | public function __construct( |
| 31 | TitleFactory $titleFactory, |
| 32 | LanguageFallbackChainFactory $languageFallbackChainFactory, |
| 33 | string $wikibaseConceptBaseUri, |
| 34 | string $userLanguageCode |
| 35 | ) { |
| 36 | $this->titleFactory = $titleFactory; |
| 37 | $this->languageFallbackChainFactory = $languageFallbackChainFactory; |
| 38 | $this->wikibaseConceptBaseUri = $wikibaseConceptBaseUri; |
| 39 | $this->userLanguageCode = $userLanguageCode; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Produce ES query that matches the arguments. |
| 44 | * |
| 45 | * @param string $text |
| 46 | * @param string $languageCode |
| 47 | * @param bool $strictLanguage |
| 48 | * @param SearchContext $context |
| 49 | * |
| 50 | * @return AbstractQuery |
| 51 | */ |
| 52 | protected function getElasticSearchQuery( |
| 53 | $text, |
| 54 | $languageCode, |
| 55 | $strictLanguage, |
| 56 | SearchContext $context |
| 57 | ) { |
| 58 | $context->setOriginalSearchTerm( $text ); |
| 59 | $profile = LabelsCompletionQuery::loadProfile( |
| 60 | $context->getConfig()->getProfileService(), |
| 61 | $this->languageFallbackChainFactory, |
| 62 | EntitySearchElastic::WIKIBASE_PREFIX_QUERY_BUILDER, |
| 63 | $context->getProfileContext(), |
| 64 | $context->getProfileContextParams(), |
| 65 | $languageCode |
| 66 | ); |
| 67 | $query = LabelsCompletionQuery::build( |
| 68 | $text, |
| 69 | $profile, |
| 70 | EntitySchemaContent::CONTENT_MODEL_ID, |
| 71 | $languageCode, |
| 72 | $strictLanguage, |
| 73 | static function ( string $text ): ?string { |
| 74 | try { |
| 75 | $id = new EntitySchemaId( $text ); |
| 76 | } catch ( InvalidArgumentException ) { |
| 77 | return null; |
| 78 | } |
| 79 | return $id->getId(); |
| 80 | } |
| 81 | ); |
| 82 | return $query; |
| 83 | } |
| 84 | |
| 85 | /** @inheritDoc */ |
| 86 | public function getRankedSearchResults( |
| 87 | $text, |
| 88 | $languageCode, |
| 89 | $entityType, |
| 90 | $limit, |
| 91 | $strictLanguage, |
| 92 | ?string $profileContext |
| 93 | ) { |
| 94 | if ( $entityType !== EntitySchemaSearchHelperFactory::ENTITY_TYPE ) { |
| 95 | return []; |
| 96 | } |
| 97 | $profileContext ??= EntitySearchElastic::CONTEXT_WIKIBASE_PREFIX; |
| 98 | $searcher = new WikibaseEntitySearcher( 0, $limit, 'wikibase_prefix', 'wikibase-prefix' ); |
| 99 | $searcher->getSearchContext()->setProfileContext( |
| 100 | $profileContext, |
| 101 | [ 'language' => $languageCode ] ); |
| 102 | $query = $this->getElasticSearchQuery( $text, $languageCode, $strictLanguage, |
| 103 | $searcher->getSearchContext() ); |
| 104 | |
| 105 | $searcher->setResultsType( new ESElasticTermResult( |
| 106 | $this->titleFactory, |
| 107 | $this->wikibaseConceptBaseUri, |
| 108 | $query instanceof LabelsCompletionQuery ? $query->getSearchLanguageCodes() : [], |
| 109 | $this->languageFallbackChainFactory->newFromLanguageCode( $this->userLanguageCode ) |
| 110 | ) ); |
| 111 | |
| 112 | $result = $searcher->performSearch( $query ); |
| 113 | |
| 114 | if ( $result->isOK() ) { |
| 115 | return $result->getValue(); |
| 116 | } else { |
| 117 | throw new EntitySearchException( $result ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } |