Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| TextFieldFilterFeature | |
100.00% |
12 / 12 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCrossSearchStrategy | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doApply | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| doGetFilterQuery | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getFilterQuery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Query; |
| 4 | |
| 5 | use CirrusSearch\CrossSearchStrategy; |
| 6 | use CirrusSearch\Parser\AST\KeywordFeatureNode; |
| 7 | use CirrusSearch\Query\Builder\QueryBuildingContext; |
| 8 | use CirrusSearch\Search\SearchContext; |
| 9 | use Elastica\Query; |
| 10 | use Elastica\Query\AbstractQuery; |
| 11 | |
| 12 | /** |
| 13 | * Feature for filtering on specific text fields. Supports plain AND matching |
| 14 | * by default, and phrase matching when quoted. |
| 15 | */ |
| 16 | class TextFieldFilterFeature extends SimpleKeywordFeature implements FilterQueryFeature { |
| 17 | |
| 18 | /** @var string Full text keyword to register */ |
| 19 | private $keyword; |
| 20 | |
| 21 | /** @var string Elasticsearch field to filter against */ |
| 22 | private $field; |
| 23 | |
| 24 | /** |
| 25 | * @param string $keyword Full text keyword to register |
| 26 | * @param string $field Elasticsearch field to filter against |
| 27 | */ |
| 28 | public function __construct( $keyword, $field ) { |
| 29 | $this->keyword = $keyword; |
| 30 | $this->field = $field; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return string[] |
| 35 | */ |
| 36 | protected function getKeywords() { |
| 37 | return [ $this->keyword ]; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param KeywordFeatureNode $node |
| 42 | * @return CrossSearchStrategy |
| 43 | */ |
| 44 | public function getCrossSearchStrategy( KeywordFeatureNode $node ) { |
| 45 | return CrossSearchStrategy::allWikisStrategy(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param SearchContext $context |
| 50 | * @param string $key The keyword |
| 51 | * @param string $value The value attached to the keyword with quotes stripped |
| 52 | * @param string $quotedValue The original value in the search string, including quotes |
| 53 | * if used |
| 54 | * @param bool $negated Is the search negated? Not used to generate the returned |
| 55 | * AbstractQuery, that will be negated as necessary. Used for any other building/context |
| 56 | * necessary. |
| 57 | * @return array Two element array, first an AbstractQuery or null to apply to the |
| 58 | * query. Second a boolean indicating if the quotedValue should be kept in the search |
| 59 | * string. |
| 60 | */ |
| 61 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
| 62 | $query = $this->doGetFilterQuery( $value, $quotedValue ); |
| 63 | |
| 64 | return [ $query, false ]; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string $value |
| 69 | * @param string $quotedValue |
| 70 | * @return Query\MatchQuery|Query\MatchPhrase |
| 71 | */ |
| 72 | protected function doGetFilterQuery( $value, $quotedValue ) { |
| 73 | if ( $value !== $quotedValue ) { |
| 74 | // If used with quotes we create a more precise phrase query |
| 75 | $query = new Query\MatchPhrase( $this->field, $value ); |
| 76 | } else { |
| 77 | $query = new Query\MatchQuery( $this->field, [ 'query' => $value ] ); |
| 78 | $query->setFieldOperator( $this->field, 'AND' ); |
| 79 | } |
| 80 | |
| 81 | return $query; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param KeywordFeatureNode $node |
| 86 | * @param QueryBuildingContext $context |
| 87 | * @return AbstractQuery|null |
| 88 | */ |
| 89 | public function getFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
| 90 | return $this->doGetFilterQuery( $node->getValue(), $node->getQuotedValue() ); |
| 91 | } |
| 92 | } |