Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
15.38% |
2 / 13 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LemmaField | |
15.38% |
2 / 13 |
|
0.00% |
0 / 3 |
20.15 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMapping | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getFieldData | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 1 | <?php |
| 2 | namespace Wikibase\Lexeme\Search\Elastic; |
| 3 | |
| 4 | use CirrusSearch\CirrusSearch; |
| 5 | use MediaWiki\Search\SearchEngine; |
| 6 | use MediaWiki\Search\SearchIndexField; |
| 7 | use Wikibase\DataModel\Entity\EntityDocument; |
| 8 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
| 9 | use Wikibase\Search\Elastic\Fields\TermIndexField; |
| 10 | |
| 11 | /** |
| 12 | * Field implementing Lexeme's lemma |
| 13 | */ |
| 14 | class LemmaField extends TermIndexField { |
| 15 | |
| 16 | public const NAME = 'lemma'; |
| 17 | |
| 18 | public function __construct() { |
| 19 | parent::__construct( static::NAME, SearchIndexField::INDEX_TYPE_TEXT ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @param SearchEngine $engine |
| 24 | * |
| 25 | * @return array |
| 26 | */ |
| 27 | public function getMapping( SearchEngine $engine ) { |
| 28 | // Since we need a specially tuned field, we can not use |
| 29 | // standard search engine types. |
| 30 | if ( !( $engine instanceof CirrusSearch ) ) { |
| 31 | // For now only Cirrus/Elastic is supported |
| 32 | return []; |
| 33 | } |
| 34 | |
| 35 | $config = $this->getUnindexedField(); |
| 36 | $config['fields']['prefix'] = |
| 37 | $this->getSubfield( 'prefix_asciifolding', 'near_match_asciifolding' ); |
| 38 | $config['fields']['near_match'] = $this->getSubfield( 'near_match' ); |
| 39 | $config['fields']['near_match_folded'] = $this->getSubfield( 'near_match_asciifolding' ); |
| 40 | // TODO: we don't seem to be using this, check if we need it? |
| 41 | $config['copy_to'] = 'labels_all'; |
| 42 | |
| 43 | return $config; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param EntityDocument $entity |
| 48 | * |
| 49 | * @return mixed Get the value of the field to be indexed when a page/document |
| 50 | * is indexed. This might be an array with nested data, if the field |
| 51 | * is defined with nested type or an int or string for simple field types. |
| 52 | */ |
| 53 | public function getFieldData( EntityDocument $entity ) { |
| 54 | if ( !( $entity instanceof Lexeme ) ) { |
| 55 | return []; |
| 56 | } |
| 57 | /** |
| 58 | * @var Lexeme $entity |
| 59 | */ |
| 60 | return array_values( $entity->getLemmas()->toTextArray() ); |
| 61 | } |
| 62 | |
| 63 | } |