Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| KeywordIndexField | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| withDocValues | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getMapping | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Search; |
| 4 | |
| 5 | use CirrusSearch\SearchConfig; |
| 6 | use SearchIndexField; |
| 7 | |
| 8 | /** |
| 9 | * Index field representing keyword. |
| 10 | * Keywords use special analyzer. |
| 11 | * @package CirrusSearch |
| 12 | */ |
| 13 | class KeywordIndexField extends CirrusIndexField { |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $typeName = 'keyword'; |
| 18 | /** |
| 19 | * @var bool |
| 20 | */ |
| 21 | private $caseSensitiveSubfield; |
| 22 | /** @var bool true to skip doc values */ |
| 23 | private bool $withDocValues; |
| 24 | |
| 25 | /** |
| 26 | * @param string $name |
| 27 | * @param string $type |
| 28 | * @param SearchConfig $config |
| 29 | * @param bool $caseSensitiveSubfield |
| 30 | * @param bool $withDocValues set to true to enable indexing/storing doc values |
| 31 | */ |
| 32 | public function __construct( $name, $type, SearchConfig $config, bool $caseSensitiveSubfield = false, bool $withDocValues = false ) { |
| 33 | parent::__construct( $name, $type, $config ); |
| 34 | if ( $caseSensitiveSubfield ) { |
| 35 | $this->setFlag( SearchIndexField::FLAG_CASEFOLD ); |
| 36 | } |
| 37 | $this->caseSensitiveSubfield = $caseSensitiveSubfield; |
| 38 | $this->withDocValues = $withDocValues; |
| 39 | } |
| 40 | |
| 41 | public function withDocValues(): self { |
| 42 | $this->withDocValues = true; |
| 43 | return $this; |
| 44 | } |
| 45 | |
| 46 | /** @inheritDoc */ |
| 47 | public function getMapping( \SearchEngine $engine ) { |
| 48 | $config = parent::getMapping( $engine ); |
| 49 | $config['doc_values'] = $this->withDocValues; |
| 50 | $config['normalizer'] = |
| 51 | $this->checkFlag( self::FLAG_CASEFOLD ) ? 'lowercase_keyword' : 'keyword'; |
| 52 | if ( $this->caseSensitiveSubfield ) { |
| 53 | $config['fields']['keyword'] = [ |
| 54 | 'type' => 'keyword', |
| 55 | 'normalizer' => 'keyword', |
| 56 | 'doc_values' => $this->withDocValues |
| 57 | ]; |
| 58 | } |
| 59 | return $config; |
| 60 | } |
| 61 | } |