Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| InTitleFeature | |
100.00% |
19 / 19 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRegexHLFlavor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doApply | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getNonRegexFilterQuery | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| buildNonRegexHLFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Query; |
| 4 | |
| 5 | use CirrusSearch\CirrusConfigNames; |
| 6 | use CirrusSearch\Parser\AST\KeywordFeatureNode; |
| 7 | use CirrusSearch\Query\Builder\QueryBuildingContext; |
| 8 | use CirrusSearch\Search\Escaper; |
| 9 | use CirrusSearch\Search\Fetch\HighlightedField; |
| 10 | use CirrusSearch\Search\Filters; |
| 11 | use CirrusSearch\Search\SearchContext; |
| 12 | use CirrusSearch\SearchConfig; |
| 13 | use Elastica\Query\AbstractQuery; |
| 14 | use MediaWiki\MainConfigNames; |
| 15 | |
| 16 | /** |
| 17 | * Applies a filter against the title field in elasticsearch. When not negated |
| 18 | * the term remains in the original query as a scoring signal. The term itself |
| 19 | * is used as a QueryString query, so some advanced syntax like * and phrase |
| 20 | * matches can be used. Note that quotes in the incoming query are maintained |
| 21 | * in the generated filter. |
| 22 | * |
| 23 | * Examples: |
| 24 | * intitle:Foo |
| 25 | * intitle:Foo* |
| 26 | * intitle:"gold rush" |
| 27 | * |
| 28 | * Things that might seem like they would work, but don't. This is because the |
| 29 | * quotes are maintained in the filter and in the top level query. |
| 30 | * intitle:"foo*" |
| 31 | * intitle:"foo OR bar" |
| 32 | */ |
| 33 | class InTitleFeature extends BaseRegexFeature { |
| 34 | |
| 35 | /** |
| 36 | * @var Escaper an escaper used to sanitize queries when not used as regular expression |
| 37 | * |
| 38 | * TODO: do not rely on escaper here, this should be consistent with what the Parser does. |
| 39 | * @see Filters::intitle() |
| 40 | */ |
| 41 | private $escaper; |
| 42 | |
| 43 | public function __construct( SearchConfig $config ) { |
| 44 | parent::__construct( |
| 45 | $config, |
| 46 | [ |
| 47 | 'title' => HighlightedField::TARGET_TITLE_SNIPPET, |
| 48 | // note the redirect. prefix is excluded when in redirect scope, in that |
| 49 | // case redirects are independent documents. |
| 50 | 'redirect.title' => HighlightedField::TARGET_REDIRECT_SNIPPET |
| 51 | ] |
| 52 | ); |
| 53 | $this->escaper = new Escaper( |
| 54 | $config->get( MainConfigNames::LanguageCode ), |
| 55 | $config->get( CirrusConfigNames::AllowLeadingWildcard ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return string[] |
| 61 | */ |
| 62 | protected function getKeywords() { |
| 63 | return [ 'intitle' ]; |
| 64 | } |
| 65 | |
| 66 | protected function getRegexHLFlavor(): string { |
| 67 | return "lucene_anchored"; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param SearchContext $context |
| 72 | * @param string $key The keyword |
| 73 | * @param string $value The value attached to the keyword with quotes stripped |
| 74 | * @param string $quotedValue The original value in the search string, including quotes if used |
| 75 | * @param bool $negated Is the search negated? Not used to generate the returned AbstractQuery, |
| 76 | * that will be negated as necessary. Used for any other building/context necessary. |
| 77 | * @return array Two element array, first an AbstractQuery or null to apply to the |
| 78 | * query. Second a boolean indicating if the quotedValue should be kept in the search |
| 79 | * string. |
| 80 | */ |
| 81 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
| 82 | $filter = Filters::intitle( $context->escaper(), $quotedValue, $value !== $quotedValue, |
| 83 | $context->isRedirectScope() ); |
| 84 | |
| 85 | return [ $filter, !$negated ]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param KeywordFeatureNode $node |
| 90 | * @param QueryBuildingContext $context |
| 91 | * @return AbstractQuery|null |
| 92 | */ |
| 93 | protected function getNonRegexFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
| 94 | return Filters::intitle( $this->escaper, $node->getQuotedValue(), |
| 95 | $node->getValue() !== $node->getQuotedValue(), $context->isRedirectScope() ); |
| 96 | } |
| 97 | |
| 98 | /** @inheritDoc */ |
| 99 | public function buildNonRegexHLFields( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
| 100 | // we highlight this field a bit differently as it's part of the main query |
| 101 | return []; |
| 102 | } |
| 103 | } |