Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
TermIndexField | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
110 | |
0.00% |
0 / 1 |
getUnindexedField | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getSubfield | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
getTokenizedSubfield | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
merge | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
getMappingField | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Wikibase\Search\Elastic\Fields; |
4 | |
5 | use CirrusSearch\CirrusSearch; |
6 | use CirrusSearch\Search\TextIndexField; |
7 | use CirrusSearch\SearchConfig; |
8 | use SearchEngine; |
9 | use SearchIndexField; |
10 | use SearchIndexFieldDefinition; |
11 | use Wikibase\Repo\Search\Fields\WikibaseIndexField; |
12 | |
13 | /** |
14 | * Generic class for fields that index terms such as labels. |
15 | * This class applies only to ElasticSearch fields currently. |
16 | * |
17 | * @license GPL-2.0-or-later |
18 | * @author Stas Malyshev |
19 | */ |
20 | abstract class TermIndexField extends SearchIndexFieldDefinition implements WikibaseIndexField { |
21 | |
22 | /** |
23 | * Produce a plain unindexed string field. |
24 | * @return array |
25 | */ |
26 | protected function getUnindexedField() { |
27 | return [ |
28 | 'type' => 'text', |
29 | 'index' => false, |
30 | 'fields' => [] |
31 | ]; |
32 | } |
33 | |
34 | /** |
35 | * Create a string field config with specific analyzer fields. |
36 | * |
37 | * @param string $analyzer |
38 | * @param string|null $search_analyzer |
39 | * |
40 | * @return array |
41 | */ |
42 | protected function getSubfield( $analyzer, $search_analyzer = null ) { |
43 | $config = [ |
44 | 'type' => 'text', |
45 | 'index_options' => 'docs', |
46 | 'analyzer' => $analyzer, |
47 | 'norms' => false, |
48 | ]; |
49 | if ( $search_analyzer ) { |
50 | $config['search_analyzer'] = $search_analyzer; |
51 | } |
52 | return $config; |
53 | } |
54 | |
55 | /** |
56 | * Create a tokenized string field config with specific analyzer fields. |
57 | * |
58 | * @param SearchConfig $config |
59 | * @param string $analyzer |
60 | * @param string|null $search_analyzer |
61 | * @return array |
62 | */ |
63 | protected function getTokenizedSubfield( SearchConfig $config, $analyzer, $search_analyzer = null ) { |
64 | $field = [ |
65 | 'type' => 'text', |
66 | 'analyzer' => $analyzer, |
67 | 'position_increment_gap' => TextIndexField::POSITION_INCREMENT_GAP, |
68 | 'similarity' => TextIndexField::getSimilarity( $config, $this->name, $analyzer ), |
69 | ]; |
70 | |
71 | if ( $search_analyzer ) { |
72 | $field['search_analyzer'] = $search_analyzer; |
73 | } |
74 | |
75 | return $field; |
76 | } |
77 | |
78 | /** |
79 | * Merge two field definitions if possible. |
80 | * |
81 | * @param SearchIndexField $that |
82 | * @return SearchIndexField|false New definition or false if not mergeable. |
83 | */ |
84 | public function merge( SearchIndexField $that ) { |
85 | // If it's the same class we're ok |
86 | if ( ( $that instanceof self ) && $this->type === $that->type ) { |
87 | return $that; |
88 | } |
89 | return false; |
90 | } |
91 | |
92 | /** |
93 | * Produce specific field mapping |
94 | * |
95 | * @param SearchEngine $engine |
96 | * @param string $name |
97 | * |
98 | * @return SearchIndexField|null |
99 | */ |
100 | public function getMappingField( SearchEngine $engine, $name ) { |
101 | if ( !( $engine instanceof CirrusSearch ) ) { |
102 | // For now only Cirrus/Elastic is supported |
103 | return null; |
104 | } |
105 | return $this; |
106 | } |
107 | |
108 | } |