Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (warning)
80.95%
17 / 21
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SemanticSearchQueryRoute
80.95% covered (warning)
80.95%
17 / 21
75.00% covered (warning)
75.00%
3 / 4
10.69
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 score
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
7
 getSearchEngineEntryPoint
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProfileContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Dispatch;
4
5use CirrusSearch\CirrusDebugOptions;
6use CirrusSearch\CirrusSearch;
7use CirrusSearch\Profile\SearchProfileService;
8use Wikimedia\Assert\Assert;
9
10/**
11 * Semantic SearchQuery routing functionality which produces a constant
12 * score when successful, 0.0 otherwise.
13 * Inspects CirrusDebugOptions
14 */
15class SemanticSearchQueryRoute implements SearchQueryRoute {
16    private string $searchEngineEntryPoint;
17    private CirrusDebugOptions $cirrusDebugOptions;
18    private array $namespaces;
19    private float $score;
20
21    /**
22     * @param string $searchEngineEntryPoint
23     * @param CirrusDebugOptions $cirrusDebugOptions
24     * @param int[] $namespaces
25     * @param float $score
26     */
27    public function __construct(
28        string $searchEngineEntryPoint,
29        CirrusDebugOptions $cirrusDebugOptions,
30        array $namespaces,
31        float $score
32    ) {
33        $this->searchEngineEntryPoint = $searchEngineEntryPoint;
34        $this->cirrusDebugOptions = $cirrusDebugOptions;
35        $this->namespaces = $namespaces;
36        $this->score = $score;
37    }
38
39    /**
40     * @param \CirrusSearch\Search\SearchQuery $query
41     * @return float
42     */
43    public function score( \CirrusSearch\Search\SearchQuery $query ) {
44        Assert::parameter( $query->getSearchEngineEntryPoint() === $this->searchEngineEntryPoint,
45            'query',
46            "must be {$this->searchEngineEntryPoint} but {$query->getSearchEngineEntryPoint()} given." );
47
48        if ( !$this->cirrusDebugOptions->isCirrusSemanticSearch() ) {
49            return self::REJECT_ROUTE;
50        }
51
52        if ( $this->namespaces !== [] ) {
53            $qNs = $query->getNamespaces();
54            if ( $qNs === [] ) {
55                return self::REJECT_ROUTE;
56            }
57            if ( count( array_intersect( $this->namespaces, $qNs ) ) !== count( $qNs ) ) {
58                return self::REJECT_ROUTE;
59            }
60        }
61
62        // Semantic search does not support user-selected profiles currently, only autoselect.
63        foreach ( $query->getForcedProfiles() as $profileName ) {
64            if ( $profileName !== CirrusSearch::AUTOSELECT_PROFILE ) {
65                return self::REJECT_ROUTE;
66            }
67        }
68
69        return $this->score;
70    }
71
72    /**
73     * The entry point used in the search engine:
74     * - searchText
75     * - nearMatch
76     * - completionSearch
77     *
78     * @return string
79     */
80    public function getSearchEngineEntryPoint() {
81        return $this->searchEngineEntryPoint;
82    }
83
84    /**
85     * The SearchProfile context to use when this route is chosen.
86     *
87     * @return string
88     */
89    public function getProfileContext() {
90        return SearchProfileService::CONTEXT_SEMANTIC;
91    }
92}