Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
InTitleFeature | |
100.00% |
13 / 13 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
getKeywords | |
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 | /** |
60 | * @param SearchContext $context |
61 | * @param string $key The keyword |
62 | * @param string $value The value attached to the keyword with quotes stripped |
63 | * @param string $quotedValue The original value in the search string, including quotes if used |
64 | * @param bool $negated Is the search negated? Not used to generate the returned AbstractQuery, |
65 | * that will be negated as necessary. Used for any other building/context necessary. |
66 | * @return array Two element array, first an AbstractQuery or null to apply to the |
67 | * query. Second a boolean indicating if the quotedValue should be kept in the search |
68 | * string. |
69 | */ |
70 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
71 | $filter = Filters::intitle( $context->escaper(), $quotedValue, $value !== $quotedValue ); |
72 | |
73 | return [ $filter, !$negated ]; |
74 | } |
75 | |
76 | /** |
77 | * @param KeywordFeatureNode $node |
78 | * @param QueryBuildingContext $context |
79 | * @return AbstractQuery|null |
80 | */ |
81 | protected function getNonRegexFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
82 | return Filters::intitle( $this->escaper, $node->getQuotedValue(), $node->getValue() !== $node->getQuotedValue() ); |
83 | } |
84 | |
85 | public function buildNonRegexHLFields( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
86 | // we highlight this field a bit differently as it's part of the main query |
87 | return []; |
88 | } |
89 | } |