Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
KeywordIndexField
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getMapping
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace CirrusSearch\Search;
4
5use CirrusSearch\SearchConfig;
6use SearchIndexField;
7
8/**
9 * Index field representing keyword.
10 * Keywords use special analyzer.
11 * @package CirrusSearch
12 */
13class KeywordIndexField extends CirrusIndexField {
14    /**
15     * Using text type here since it's better for our purposes than native
16     * keyword type.
17     * @var string
18     */
19    protected $typeName = 'text';
20    /**
21     * @var bool
22     */
23    private $caseSensitiveSubfield;
24
25    public function __construct( $name, $type, SearchConfig $config, bool $caseSensitiveSubfield = false ) {
26        parent::__construct( $name, $type, $config );
27        if ( $caseSensitiveSubfield ) {
28            $this->setFlag( SearchIndexField::FLAG_CASEFOLD );
29        }
30        $this->caseSensitiveSubfield = $caseSensitiveSubfield;
31    }
32
33    /**
34     * Maximum number of characters allowed in keyword terms.
35     */
36    private const KEYWORD_IGNORE_ABOVE = 5000;
37
38    public function getMapping( \SearchEngine $engine ) {
39        $config = parent::getMapping( $engine );
40        $config['analyzer'] =
41            $this->checkFlag( self::FLAG_CASEFOLD ) ? 'lowercase_keyword' : 'keyword';
42        $config += [
43            'norms' => false,
44            // Omit the length norm because there is only even one token
45            'index_options' => 'docs',
46        ];
47        if ( $this->caseSensitiveSubfield ) {
48            $config['fields']['keyword'] = [
49                'type' => 'text',
50                'analyzer' => 'keyword',
51                'index_options' => 'docs',
52                'norms' => false,
53            ];
54        }
55        return $config;
56    }
57}