Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.83% covered (success)
97.83%
45 / 46
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
InLabelSearch
97.83% covered (success)
97.83%
45 / 46
66.67% covered (warning)
66.67%
2 / 3
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
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    private LanguageFallbackChainFactory $languageChainFactory;
20
21    private EntityIdParser $idParser;
22    private array $contentModelMap;
23    private CirrusDebugOptions $debugOptions;
24    private array $stemmingSettings;
25
26    public function __construct(
27        LanguageFallbackChainFactory $languageChainFactory,
28        EntityIdParser $idParser,
29        array $contentModelMap,
30        CirrusDebugOptions $debugOptions,
31        array $stemmingSettings
32    ) {
33        $this->languageChainFactory = $languageChainFactory;
34        $this->idParser = $idParser;
35        $this->contentModelMap = $contentModelMap;
36        $this->debugOptions = $debugOptions;
37        $this->stemmingSettings = $stemmingSettings;
38    }
39
40    /**
41     * @return TermSearchResult[]
42     *
43     * @throws EntitySearchException
44     */
45    public function search(
46        string $searchTerm,
47        string $languageCode,
48        string $entityType,
49        int $limit,
50        int $offset = 0
51    ): array {
52        $searcher = new WikibaseEntitySearcher(
53            $offset,
54            $limit,
55            'wikibase_in_label',
56            'wikibase-in-label',
57            $this->debugOptions
58        );
59        $searcher->getSearchContext()->setProfileContext(
60            EntitySearchElastic::CONTEXT_WIKIBASE_IN_LABEL,
61            [ 'language' => $languageCode ] );
62        $query = $this->getElasticSearchQuery( $searchTerm, $languageCode, $entityType, $searcher->getSearchContext() );
63
64        $searcher->setResultsType( new EntityElasticTermResult(
65            $this->idParser,
66            $query->getSearchLanguageCodes(),
67            'plain',
68            $this->languageChainFactory->newFromLanguageCode( $languageCode )
69        ) );
70
71        $result = $searcher->performSearch( $query );
72
73        if ( $result->isOK() ) {
74            $result = $result->getValue();
75        } else {
76            throw new EntitySearchException( $result );
77        }
78
79        if ( $searcher->isReturnRaw() ) {
80            $searcher->processRawReturn( $result, RequestContext::getMain()->getRequest() );
81        }
82
83        return $result;
84    }
85
86    private function getElasticSearchQuery(
87        string $text,
88        string $languageCode,
89        string $entityType,
90        SearchContext $context
91    ): InLabelQuery {
92        $context->setOriginalSearchTerm( $text );
93        $profile = InLabelQuery::loadProfile(
94            $context->getConfig()->getProfileService(),
95            $this->languageChainFactory,
96            EntitySearchElastic::WIKIBASE_IN_LABEL_QUERY_BUILDER,
97            $context->getProfileContext(),
98            $context->getProfileContextParams(),
99            $languageCode
100        );
101        return InLabelQuery::build(
102            $text,
103            $profile,
104            $this->contentModelMap[$entityType],
105            $languageCode,
106            EntitySearchUtils::entityIdParserNormalizer( $this->idParser ),
107            $this->stemmingSettings
108        );
109    }
110
111}