Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
14.29% covered (danger)
14.29%
2 / 14
LemmaField
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
20.74
14.29% covered (danger)
14.29%
2 / 14
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getMapping
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 9
 getFieldData
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
<?php
namespace Wikibase\Lexeme\Search\Elastic;
use CirrusSearch\CirrusSearch;
use SearchEngine;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\Lexeme\Domain\Model\Lexeme;
use Wikibase\Search\Elastic\Fields\TermIndexField;
/**
 * Field implementing Lexeme's lemma
 */
class LemmaField extends TermIndexField {
    public const NAME = 'lemma';
    public function __construct() {
        parent::__construct( static::NAME, \SearchIndexField::INDEX_TYPE_TEXT );
    }
    /**
     * @param SearchEngine $engine
     *
     * @return array
     */
    public function getMapping( SearchEngine $engine ) {
        // Since we need a specially tuned field, we can not use
        // standard search engine types.
        if ( !( $engine instanceof CirrusSearch ) ) {
            // For now only Cirrus/Elastic is supported
            return [];
        }
        $config = $this->getUnindexedField();
        $config['fields']['prefix'] =
            $this->getSubfield( 'prefix_asciifolding', 'near_match_asciifolding' );
        $config['fields']['near_match'] = $this->getSubfield( 'near_match' );
        $config['fields']['near_match_folded'] = $this->getSubfield( 'near_match_asciifolding' );
        // TODO: we don't seem to be using this, check if we need it?
        $config['copy_to'] = 'labels_all';
        return $config;
    }
    /**
     * @param EntityDocument $entity
     *
     * @return mixed Get the value of the field to be indexed when a page/document
     *               is indexed. This might be an array with nested data, if the field
     *               is defined with nested type or an int or string for simple field types.
     */
    public function getFieldData( EntityDocument $entity ) {
        if ( !( $entity instanceof Lexeme ) ) {
            return [];
        }
        /**
         * @var Lexeme $entity
         */
        return array_values( $entity->getLemmas()->toTextArray() );
    }
}