Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
33.33% |
11 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FormsField | |
33.33% |
11 / 33 |
|
0.00% |
0 / 3 |
16.67 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMapping | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
6 | |||
| getFieldData | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
3.01 | |||
| 1 | <?php |
| 2 | namespace Wikibase\Lexeme\Search\Elastic; |
| 3 | |
| 4 | use CirrusSearch\CirrusSearch; |
| 5 | use CirrusSearch\Search\KeywordIndexField; |
| 6 | use MediaWiki\Search\SearchEngine; |
| 7 | use MediaWiki\Search\SearchIndexField; |
| 8 | use Wikibase\DataModel\Entity\EntityDocument; |
| 9 | use Wikibase\DataModel\Entity\ItemId; |
| 10 | use Wikibase\Lexeme\Domain\Model\Lexeme; |
| 11 | use Wikibase\Search\Elastic\Fields\TermIndexField; |
| 12 | |
| 13 | /** |
| 14 | * Field for Lexeme Forms |
| 15 | */ |
| 16 | class FormsField extends TermIndexField { |
| 17 | |
| 18 | public const NAME = 'lexeme_forms'; |
| 19 | |
| 20 | public function __construct() { |
| 21 | parent::__construct( static::NAME, SearchIndexField::INDEX_TYPE_TEXT ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @param SearchEngine $engine |
| 26 | * @return array |
| 27 | */ |
| 28 | public function getMapping( SearchEngine $engine ) { |
| 29 | // Since we need a specially tuned field, we can not use |
| 30 | // standard search engine types. |
| 31 | if ( !( $engine instanceof CirrusSearch ) ) { |
| 32 | // For now only Cirrus/Elastic is supported |
| 33 | return []; |
| 34 | } |
| 35 | |
| 36 | $reprConfig = $this->getUnindexedField(); |
| 37 | |
| 38 | $reprConfig['fields']['prefix'] = |
| 39 | $this->getSubfield( 'prefix_asciifolding', 'near_match_asciifolding' ); |
| 40 | $reprConfig['fields']['near_match'] = $this->getSubfield( 'near_match' ); |
| 41 | $reprConfig['fields']['near_match_folded'] = $this->getSubfield( 'near_match_asciifolding' ); |
| 42 | // TODO: we don't seem to be using this, check if we need it? |
| 43 | $reprConfig['copy_to'] = 'labels_all'; |
| 44 | |
| 45 | $keyword = new KeywordIndexField( $this->getName(), SearchIndexField::INDEX_TYPE_KEYWORD, |
| 46 | $engine->getConfig() ); |
| 47 | $keyword->setFlag( self::FLAG_CASEFOLD ); |
| 48 | $keywordMapping = $keyword->getMapping( $engine ); |
| 49 | |
| 50 | $config = [ |
| 51 | 'type' => 'object', |
| 52 | 'properties' => [ |
| 53 | 'id' => $keywordMapping, |
| 54 | 'representation' => $reprConfig, |
| 55 | ] |
| 56 | ]; |
| 57 | |
| 58 | return $config; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param EntityDocument $entity |
| 63 | * |
| 64 | * @return mixed Get the value of the field to be indexed when a page/document |
| 65 | * is indexed. This might be an array with nested data, if the field |
| 66 | * is defined with nested type or an int or string for simple field types. |
| 67 | */ |
| 68 | public function getFieldData( EntityDocument $entity ) { |
| 69 | if ( !( $entity instanceof Lexeme ) ) { |
| 70 | return []; |
| 71 | } |
| 72 | /** |
| 73 | * @var Lexeme $entity |
| 74 | */ |
| 75 | $data = []; |
| 76 | foreach ( $entity->getForms()->toArray() as $form ) { |
| 77 | $data[] = [ |
| 78 | "id" => $form->getId()->getSerialization(), |
| 79 | "representation" => array_values( $form->getRepresentations()->toTextArray() ), |
| 80 | "features" => array_map( static function ( ItemId $item ) { |
| 81 | return $item->getSerialization(); |
| 82 | }, $form->getGrammaticalFeatures() ) |
| 83 | ]; |
| 84 | } |
| 85 | return $data; |
| 86 | } |
| 87 | |
| 88 | } |