Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.62% covered (success)
97.62%
41 / 42
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InLabelSearch
97.62% covered (success)
97.62%
41 / 42
66.67% covered (warning)
66.67%
2 / 3
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 search
95.83% covered (success)
95.83%
23 / 24
0.00% covered (danger)
0.00%
0 / 1
3
 getElasticSearchQuery
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\Search\Elastic;
4
5use CirrusSearch\CirrusDebugOptions;
6use CirrusSearch\Search\SearchContext;
7use MediaWiki\Context\RequestContext;
8use Wikibase\DataModel\Entity\EntityIdParser;
9use Wikibase\Lib\Interactors\TermSearchResult;
10use Wikibase\Lib\LanguageFallbackChainFactory;
11use Wikibase\Repo\Api\EntitySearchException;
12use Wikibase\Search\Elastic\Query\InLabelQuery;
13
14/**
15 * @license GPL-2.0-or-later
16 */
17class InLabelSearch {
18
19    public function __construct(
20        private readonly LanguageFallbackChainFactory $languageChainFactory,
21        private readonly EntityIdParser $idParser,
22        private readonly array $contentModelMap,
23        private readonly CirrusDebugOptions $debugOptions,
24        private readonly array $stemmingSettings,
25    ) {
26    }
27
28    /**
29     * @return TermSearchResult[]
30     *
31     * @throws EntitySearchException
32     */
33    public function search(
34        string $searchTerm,
35        string $languageCode,
36        string $entityType,
37        int $limit,
38        int $offset = 0
39    ): array {
40        $searcher = new WikibaseEntitySearcher(
41            $offset,
42            $limit,
43            'wikibase_in_label',
44            'wikibase-in-label',
45            $this->debugOptions
46        );
47        $searcher->getSearchContext()->setProfileContext(
48            EntitySearchElastic::CONTEXT_WIKIBASE_IN_LABEL,
49            [ 'language' => $languageCode ] );
50        $query = $this->getElasticSearchQuery( $searchTerm, $languageCode, $entityType, $searcher->getSearchContext() );
51
52        $searcher->setResultsType( new EntityElasticTermResult(
53            $this->idParser,
54            $query->getSearchLanguageCodes(),
55            'plain',
56            $this->languageChainFactory->newFromLanguageCode( $languageCode )
57        ) );
58
59        $result = $searcher->performSearch( $query );
60
61        if ( $result->isOK() ) {
62            $result = $result->getValue();
63        } else {
64            throw new EntitySearchException( $result );
65        }
66
67        if ( $searcher->isReturnRaw() ) {
68            $searcher->processRawReturn( $result, RequestContext::getMain()->getRequest() );
69        }
70
71        return $result;
72    }
73
74    private function getElasticSearchQuery(
75        string $text,
76        string $languageCode,
77        string $entityType,
78        SearchContext $context
79    ): InLabelQuery {
80        $context->setOriginalSearchTerm( $text );
81        $profile = InLabelQuery::loadProfile(
82            $context->getConfig()->getProfileService(),
83            $this->languageChainFactory,
84            EntitySearchElastic::WIKIBASE_IN_LABEL_QUERY_BUILDER,
85            $context->getProfileContext(),
86            $context->getProfileContextParams(),
87            $languageCode
88        );
89        return InLabelQuery::build(
90            $text,
91            $profile,
92            $this->contentModelMap[$entityType],
93            $languageCode,
94            EntitySearchUtils::entityIdParserNormalizer( $this->idParser ),
95            $this->stemmingSettings
96        );
97    }
98
99}