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