Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.68% covered (warning)
75.68%
56 / 74
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExperimentalHighlightedFieldBuilder
75.68% covered (warning)
75.68%
56 / 74
55.56% covered (warning)
55.56%
5 / 9
18.24
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
 entireValue
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
1.02
 redirectAndHeadings
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
1.01
 text
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 mainText
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
1.06
 newRegexField
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 merge
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 skipIfLastMatched
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFactories
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Search\Fetch;
4
5use CirrusSearch\CirrusConfigNames;
6use CirrusSearch\Search\SearchQuery;
7use CirrusSearch\SearchConfig;
8use MediaWiki\MainConfigNames;
9
10class ExperimentalHighlightedFieldBuilder extends BaseHighlightedField {
11    public const EXPERIMENTAL_HL_TYPE = 'experimental';
12
13    /**
14     * @param string $fieldName
15     * @param string $target
16     * @param int $priority
17     */
18    public function __construct( $fieldName, $target, $priority = self::DEFAULT_TARGET_PRIORITY ) {
19        parent::__construct( $fieldName, self::EXPERIMENTAL_HL_TYPE, $target, $priority );
20    }
21
22    public static function entireValue(): callable {
23        return static function ( SearchConfig $config, $fieldName, $target, $priority ) {
24            $self = new self( $fieldName, $target, $priority );
25            $self->matchPlainFields();
26            $self->setFragmenter( 'none' );
27            $self->setNumberOfFragments( 1 );
28            return $self;
29        };
30    }
31
32    public static function redirectAndHeadings(): callable {
33        return static function ( SearchConfig $config, $fieldName, $target, $priority ) {
34            $self = new self( $fieldName, $target, $priority );
35            $self->matchPlainFields();
36            $self->addOption( 'skip_if_last_matched', true );
37            $self->setFragmenter( 'none' );
38            $self->setOrder( 'score' );
39            $self->setNumberOfFragments( 1 );
40            return $self;
41        };
42    }
43
44    public static function text(): callable {
45        return static function ( SearchConfig $config, $fieldName, $target, $priority ) {
46            $self = new self( $fieldName, $target, $priority );
47            $self->matchPlainFields();
48            $self->addOption( 'skip_if_last_matched', true );
49            $self->setFragmenter( 'scan' );
50            $self->setNumberOfFragments( 1 );
51            $self->setFragmentSize( $config->get( CirrusConfigNames::FragmentSize ) );
52            $self->setOptions( [
53                'top_scoring' => true,
54                'boost_before' => [
55                    // Note these values are super arbitrary right now.
56                    '20' => 2,
57                    '50' => 1.8,
58                    '200' => 1.5,
59                    '1000' => 1.2,
60                ],
61                // We should set a limit on the number of fragments we try because if we
62                // don't then we'll hit really crazy documents, say 10MB of "d d".  This'll
63                // keep us from scanning more then the first couple thousand of them.
64                // Setting this too low (like 50) can bury good snippets if the search
65                // contains common words.
66                'max_fragments_scored' => 5000,
67            ] );
68            return $self;
69        };
70    }
71
72    protected static function mainText(): callable {
73        return function ( SearchConfig $config, $fieldName, $target, $priority ) {
74            $self = ( self::text() )( $config, $fieldName, $target, $priority );
75            /** @var BaseHighlightedField $self */
76            $self->setNoMatchSize( $config->get( CirrusConfigNames::FragmentSize ) );
77            return $self;
78        };
79    }
80
81    /**
82     * @param SearchConfig $config
83     * @param string $name
84     * @param string $target
85     * @param string $pattern
86     * @param bool $caseInsensitive
87     * @param int $priority
88     * @param string $regexFlavor
89     * @return self
90     */
91    public static function newRegexField(
92        SearchConfig $config,
93        $name,
94        $target,
95        $pattern,
96        $caseInsensitive,
97        $priority,
98        $regexFlavor = 'lucene'
99    ): self {
100        // TODO: verify that we actually need to have all the text() options when running a regex
101        /** @var self $self */
102        $self = ( self::text() )( $config, $name, $target, $priority );
103        $self->addOption( 'regex', [ $pattern ] );
104        $self->addOption( 'locale', $config->get( MainConfigNames::LanguageCode ) );
105        $self->addOption( 'regex_flavor', $regexFlavor );
106        $self->addOption( 'skip_query', true );
107        $self->addOption( 'regex_case_insensitive', $caseInsensitive );
108        $self->addOption( 'max_determinized_states', $config->get( CirrusConfigNames::RegexMaxDeterminizedStates ) );
109
110        if ( $name == 'source_text.plain' ) {
111            $self->setNoMatchSize( $config->get( CirrusConfigNames::FragmentSize ) );
112        }
113        return $self;
114    }
115
116    /**
117     * @inheritDoc
118     */
119    public function merge( HighlightedField $other ): HighlightedField {
120        if ( isset( $this->options['regex'] ) &&
121             $other instanceof self &&
122             isset( $other->options['regex'] ) &&
123             $this->getFieldName() === $other->getFieldName()
124        ) {
125            $this->options['regex'] = array_merge( $this->options['regex'], $other->options['regex'] );
126            $mergedInsensitivity = $this->options['regex_case_insensitive'] || $other->options['regex_case_insensitive'];
127            $this->options['regex_case_insensitive'] = $mergedInsensitivity;
128            return $this;
129        } else {
130            return parent::merge( $other );
131        }
132    }
133
134    /**
135     * @return $this
136     */
137    public function skipIfLastMatched(): BaseHighlightedField {
138        $this->addOption( 'skip_if_last_matched', true );
139        return $this;
140    }
141
142    /**
143     * @return array
144     */
145    public static function getFactories() {
146        return [
147            SearchQuery::SEARCH_TEXT => [
148                'title' => self::entireValue(),
149                'redirect.title' => self::redirectAndHeadings(),
150                'category' => self::redirectAndHeadings(),
151                'heading' => self::redirectAndHeadings(),
152                'text' => self::mainText(),
153                'source_text.plain' => self::mainText(),
154                'auxiliary_text' => self::text(),
155                'file_text' => self::text(),
156            ]
157        ];
158    }
159}